openc2lib.types.data.datetime
1import datetime 2 3class DateTime: 4 """ OpenC2 Date-Time 5 6 This is used to represents dates and times according to Sec. 3.4.2.2. 7 According to OpenC2 specification, this is the time in milliseconds from the epoch. 8 Be careful that the `timedate` functions work with float timestamps expressed 9 in seconds from the epoch, hence conversion is needed. 10 """ 11 def __init__(self, timestamp=None): 12 """ Initialize Date-Time 13 14 The instance is initialized with the provided timestamp, or to the current time if no 15 argument is given. The timestamp is expressed in milliseconds 16 from the epoch, according to the Language Specification. 17 :param timestamp: The timestamp to initialize the instance. 18 """ 19 self.update(timestamp) 20 21 def __str__(self): 22 return str(self.time) 23 24 def __int__(self): 25 return self.time 26 27 def update(self, timestamp=None): 28 """ Change Date-Time 29 30 Change the timestamp beard by the instance. The timestamp is expressed in milliseconds 31 from the epoch. If no `timestamp` is given, sets to the current time. 32 :param timestamp: The timestamp to initialize the instance. 33 """ 34 if timestamp == None: 35 # datetime.timestamp() returns a float in seconds 36 self.time = int(datetime.datetime.now(datetime.timezone.utc).timestamp()*1000) 37 else: 38 self.time = timestamp 39 40 # RFC 7231 41 def httpdate(self, timestamp=None): 42 """ Format to HTTP headers 43 44 Formats the timestamp according to the requirements of HTTP headers (RFC 7231). 45 Use either the `timestamp`, if provided, or the current time. 46 :param timestamp: The timestamp to format, expressed in milliseconds from the epoch. 47 :return RFC 7231 representation of the `timestamp`. 48 """ 49 50 if timestamp is None: 51 timestamp = self.time 52 53 return datetime.datetime.fromtimestamp(timestamp/1000).strftime('%a, %d %b %Y %H:%M:%S %Z')
4class DateTime: 5 """ OpenC2 Date-Time 6 7 This is used to represents dates and times according to Sec. 3.4.2.2. 8 According to OpenC2 specification, this is the time in milliseconds from the epoch. 9 Be careful that the `timedate` functions work with float timestamps expressed 10 in seconds from the epoch, hence conversion is needed. 11 """ 12 def __init__(self, timestamp=None): 13 """ Initialize Date-Time 14 15 The instance is initialized with the provided timestamp, or to the current time if no 16 argument is given. The timestamp is expressed in milliseconds 17 from the epoch, according to the Language Specification. 18 :param timestamp: The timestamp to initialize the instance. 19 """ 20 self.update(timestamp) 21 22 def __str__(self): 23 return str(self.time) 24 25 def __int__(self): 26 return self.time 27 28 def update(self, timestamp=None): 29 """ Change Date-Time 30 31 Change the timestamp beard by the instance. The timestamp is expressed in milliseconds 32 from the epoch. If no `timestamp` is given, sets to the current time. 33 :param timestamp: The timestamp to initialize the instance. 34 """ 35 if timestamp == None: 36 # datetime.timestamp() returns a float in seconds 37 self.time = int(datetime.datetime.now(datetime.timezone.utc).timestamp()*1000) 38 else: 39 self.time = timestamp 40 41 # RFC 7231 42 def httpdate(self, timestamp=None): 43 """ Format to HTTP headers 44 45 Formats the timestamp according to the requirements of HTTP headers (RFC 7231). 46 Use either the `timestamp`, if provided, or the current time. 47 :param timestamp: The timestamp to format, expressed in milliseconds from the epoch. 48 :return RFC 7231 representation of the `timestamp`. 49 """ 50 51 if timestamp is None: 52 timestamp = self.time 53 54 return datetime.datetime.fromtimestamp(timestamp/1000).strftime('%a, %d %b %Y %H:%M:%S %Z')
OpenC2 Date-Time
This is used to represents dates and times according to Sec. 3.4.2.2.
According to OpenC2 specification, this is the time in milliseconds from the epoch.
Be careful that the timedate functions work with float timestamps expressed
in seconds from the epoch, hence conversion is needed.
12 def __init__(self, timestamp=None): 13 """ Initialize Date-Time 14 15 The instance is initialized with the provided timestamp, or to the current time if no 16 argument is given. The timestamp is expressed in milliseconds 17 from the epoch, according to the Language Specification. 18 :param timestamp: The timestamp to initialize the instance. 19 """ 20 self.update(timestamp)
Initialize Date-Time
The instance is initialized with the provided timestamp, or to the current time if no argument is given. The timestamp is expressed in milliseconds from the epoch, according to the Language Specification.
Parameters
- timestamp: The timestamp to initialize the instance.
28 def update(self, timestamp=None): 29 """ Change Date-Time 30 31 Change the timestamp beard by the instance. The timestamp is expressed in milliseconds 32 from the epoch. If no `timestamp` is given, sets to the current time. 33 :param timestamp: The timestamp to initialize the instance. 34 """ 35 if timestamp == None: 36 # datetime.timestamp() returns a float in seconds 37 self.time = int(datetime.datetime.now(datetime.timezone.utc).timestamp()*1000) 38 else: 39 self.time = timestamp
Change Date-Time
Change the timestamp beard by the instance. The timestamp is expressed in milliseconds
from the epoch. If no timestamp is given, sets to the current time.
Parameters
- timestamp: The timestamp to initialize the instance.
42 def httpdate(self, timestamp=None): 43 """ Format to HTTP headers 44 45 Formats the timestamp according to the requirements of HTTP headers (RFC 7231). 46 Use either the `timestamp`, if provided, or the current time. 47 :param timestamp: The timestamp to format, expressed in milliseconds from the epoch. 48 :return RFC 7231 representation of the `timestamp`. 49 """ 50 51 if timestamp is None: 52 timestamp = self.time 53 54 return datetime.datetime.fromtimestamp(timestamp/1000).strftime('%a, %d %b %Y %H:%M:%S %Z')
Format to HTTP headers
Formats the timestamp according to the requirements of HTTP headers (RFC 7231).
Use either the timestamp, if provided, or the current time.
Parameters
- timestamp: The timestamp to format, expressed in milliseconds from the epoch.
:return RFC 7231 representation of the
timestamp.