Skip to content

Commit

Permalink
Format time like isoformat() would
Browse files Browse the repository at this point in the history
In many applications millisecond precision is not enough to understand
what's going on, let's stick to microsecond-precision like with
isoformat(). Additionally the separators are changed to match those of
isoformat() (which is used to format other timestamps here).
  • Loading branch information
Jakub Stasiak authored and urbaniak committed Oct 15, 2016
1 parent 8ed1464 commit f36973b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cee_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def jsonhandler(self, obj):
def format(self, log_record):
record = OrderedDict()

record['time'] = self.formatTime(log_record)
record['time'] = datetime.utcfromtimestamp(log_record.created)

record['msg'] = log_record.getMessage()
record['pid'] = log_record.process
record['tid'] = log_record.thread
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
iso8601==0.1.11
pyflakes==1.2.3
pytest==3.0.1
pytest-flake8==0.6
Expand Down
10 changes: 10 additions & 0 deletions test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import logging

import iso8601

from cee_formatter import CEEFormatter

try:
Expand Down Expand Up @@ -36,3 +38,11 @@ def test_datetime_format():
json_dict = json.loads(json_value)

assert json_dict['d'] == date.isoformat()

# We want to make sure that the time is in full ISO8601 format so
# we don't lose too much precision (it'll be microseconds).
# This assertion can generate a false positive when timestamp has
# a zero microsecond part but I think we can live with it.
serialized_timestamp = json_dict['time']
timestamp = iso8601.parse_date(serialized_timestamp).replace(tzinfo=None)
assert serialized_timestamp == timestamp.isoformat()

0 comments on commit f36973b

Please sign in to comment.