openrvdas.logger.utils.timestamp
Tools for generating numeric timestamps, converting from timestamps into strings and parsing time strings into numeric timestamps.
Use UTC as default timezone.
timestamp(time_str=None, time_zone=timezone.utc, time_format=TIME_FORMAT)
Return numeric timestamp for a passed time_str. If no time_str is passed, return timestamp for now.
time_str(timestamp=None, time_zone=timezone.utc, time_format=TIME_FORMAT)
Given a timestamp, return a string representing that time. If no timestamp is given, return the string for now.
date_str(timestamp=None, time_zone=timezone.utc, time_format=TIME_FORMAT)
Given a timestamp, return a string representing the date of that time. If no timestamp is given, return the string for today.
TODO: read date/time format from some central settings file.
1#!/usr/bin/env python3 2 3"""Tools for generating numeric timestamps, converting from timestamps 4into strings and parsing time strings into numeric timestamps. 5 6Use UTC as default timezone. 7 8timestamp(time_str=None, time_zone=timezone.utc, time_format=TIME_FORMAT) 9 10 Return numeric timestamp for a passed time_str. If no time_str is 11 passed, return timestamp for now. 12 13time_str(timestamp=None, time_zone=timezone.utc, time_format=TIME_FORMAT) 14 15 Given a timestamp, return a string representing that time. If no 16 timestamp is given, return the string for now. 17 18date_str(timestamp=None, time_zone=timezone.utc, time_format=TIME_FORMAT) 19 20 Given a timestamp, return a string representing the date of that 21 time. If no timestamp is given, return the string for today. 22 23TODO: read date/time format from some central settings file. 24 25""" 26from datetime import datetime, timezone 27 28TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ' # ISO 8601 29LOGGING_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S' # ISO 8601 30 31DATE_FORMAT = '%Y-%m-%d' # ISO 8601 32 33# As used on the NBP 34# DATE_FORMAT = '%y+%j' # Julian 35# TIME_FORMAT = '%y+%j:%H:%M:%S.%f' # Julian 36 37# DATE_FORMAT = '%Y-%m-%d' # Gregorian 38# TIME_FORMAT = '%Y-%m-%d:%H:%M:%S.%f' # Gregorian 39 40################################################################################ 41 42 43def datetime_obj(time_str=None, time_zone=timezone.utc, time_format=TIME_FORMAT): 44 """Return datetime object for a passed time_str. If no time_str is 45 passed, return datetime object for now.""" 46 if time_str is None: 47 return datetime.now(time_zone) 48 49 # If they've given us a time string to convert. Set timezone as necessary. 50 return datetime.strptime(time_str, time_format).replace(tzinfo=time_zone) 51 52################################################################################ 53 54 55def datetime_obj_from_timestamp(timestamp, time_zone=timezone.utc): 56 """Return datetime object for a passed timestamp.""" 57 58 # If they've given us a time string to convert. Set timezone as necessary. 59 return datetime.fromtimestamp(timestamp, tz=time_zone) 60 61################################################################################ 62 63 64def timestamp(time_str=None, time_zone=timezone.utc, time_format=TIME_FORMAT): 65 """Return numeric timestamp for a passed time_str. If no time_str is 66 passed, return timestamp for now.""" 67 return datetime_obj(time_str, time_zone, time_format).timestamp() 68 69################################################################################ 70 71 72def time_str(timestamp=None, time_zone=timezone.utc, time_format=TIME_FORMAT): 73 """Given a timestamp, return a string representing that time. If no 74 timestamp is given, return the string for now.""" 75 if timestamp is None: 76 timestamp = datetime.now(time_zone).timestamp() 77 return datetime.fromtimestamp(timestamp, time_zone).strftime(time_format) 78 79################################################################################ 80 81 82def date_str(timestamp=None, time_zone=timezone.utc, date_format=DATE_FORMAT): 83 """Given a timestamp, return a string representing that date. If no 84 timestamp is given, return the string for today.""" 85 return time_str(timestamp, time_zone, date_format)
44def datetime_obj(time_str=None, time_zone=timezone.utc, time_format=TIME_FORMAT): 45 """Return datetime object for a passed time_str. If no time_str is 46 passed, return datetime object for now.""" 47 if time_str is None: 48 return datetime.now(time_zone) 49 50 # If they've given us a time string to convert. Set timezone as necessary. 51 return datetime.strptime(time_str, time_format).replace(tzinfo=time_zone)
Return datetime object for a passed time_str. If no time_str is passed, return datetime object for now.
56def datetime_obj_from_timestamp(timestamp, time_zone=timezone.utc): 57 """Return datetime object for a passed timestamp.""" 58 59 # If they've given us a time string to convert. Set timezone as necessary. 60 return datetime.fromtimestamp(timestamp, tz=time_zone)
Return datetime object for a passed timestamp.
65def timestamp(time_str=None, time_zone=timezone.utc, time_format=TIME_FORMAT): 66 """Return numeric timestamp for a passed time_str. If no time_str is 67 passed, return timestamp for now.""" 68 return datetime_obj(time_str, time_zone, time_format).timestamp()
Return numeric timestamp for a passed time_str. If no time_str is passed, return timestamp for now.
73def time_str(timestamp=None, time_zone=timezone.utc, time_format=TIME_FORMAT): 74 """Given a timestamp, return a string representing that time. If no 75 timestamp is given, return the string for now.""" 76 if timestamp is None: 77 timestamp = datetime.now(time_zone).timestamp() 78 return datetime.fromtimestamp(timestamp, time_zone).strftime(time_format)
Given a timestamp, return a string representing that time. If no timestamp is given, return the string for now.
83def date_str(timestamp=None, time_zone=timezone.utc, date_format=DATE_FORMAT): 84 """Given a timestamp, return a string representing that date. If no 85 timestamp is given, return the string for today.""" 86 return time_str(timestamp, time_zone, date_format)
Given a timestamp, return a string representing that date. If no timestamp is given, return the string for today.