Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localtime option #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Features

- ``--timestamp`` Prints the creation timestamp of each event.
- ``--ingestion-time`` Prints the ingestion time of each event.
- ``--localtime`` (or ``-l``) Use local time instead of UTC time when printing timestamps.


Example
Expand Down
6 changes: 6 additions & 0 deletions awslogs/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def add_date_range_arguments(parser, default_start='5m'):
dest='output_timestamp_enabled',
help="Add creation timestamp to the output")

get_parser.add_argument("-l",
"--localtime",
action='store_true',
dest='output_localtime_enabled',
help="Use local time zone and format instead of UTC time in timestamps")

get_parser.add_argument("--ingestion-time",
action='store_true',
dest='output_ingestion_time_enabled',
Expand Down
21 changes: 17 additions & 4 deletions awslogs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
import errno
from datetime import datetime, timedelta
import locale
import pytz
from collections import deque

import boto3
Expand All @@ -14,7 +16,7 @@

from termcolor import colored
from dateutil.parser import parse
from dateutil.tz import tzutc
from dateutil.tz import tzutc, tzlocal

from . import exceptions

Expand All @@ -26,7 +28,13 @@
}


def milis2iso(milis):
def milis2iso(milis, localtime):
if localtime:
return (
pytz.utc.localize(datetime.utcfromtimestamp(milis/1000.0))
.astimezone(tzlocal())
.strftime("%x %X:%f")[:-3]
)
res = datetime.utcfromtimestamp(milis/1000.0).isoformat()
return (res + ".000")[:23] + 'Z'

Expand Down Expand Up @@ -73,6 +81,9 @@ def __init__(self, **kwargs):
self.output_stream_enabled = kwargs.get('output_stream_enabled')
self.output_group_enabled = kwargs.get('output_group_enabled')
self.output_timestamp_enabled = kwargs.get('output_timestamp_enabled')
self.output_localtime_enabled = kwargs.get('output_localtime_enabled')
if self.output_localtime_enabled:
locale.setlocale(locale.LC_TIME,'')
self.output_ingestion_time_enabled = kwargs.get(
'output_ingestion_time_enabled')
self.start = self.parse_datetime(kwargs.get('start'))
Expand Down Expand Up @@ -189,14 +200,16 @@ def consumer():
if self.output_timestamp_enabled:
output.append(
self.color(
milis2iso(event['timestamp']),
milis2iso(event['timestamp'],
self.output_localtime_enabled),
'yellow'
)
)
if self.output_ingestion_time_enabled:
output.append(
self.color(
milis2iso(event['ingestionTime']),
milis2iso(event['ingestionTime'],
self.output_localtime_enabled),
'blue'
)
)
Expand Down