1010class DateHelper :
1111 @staticmethod
1212 def get_hdx_date (
13- date : Union [datetime , str ], ignore_timeinfo : bool , max = False
13+ date : Union [datetime , str ],
14+ ignore_timeinfo : bool ,
15+ include_microseconds = False ,
16+ max = False ,
1417 ):
1518 """Get an HDX date as a string from a datetime.datetime object.
1619
1720 Args:
1821 date (Union[datetime, str]): Date as datetime or string
1922 ignore_timeinfo (bool): Ignore time and time zone of date. Defaults to True.
23+ include_microseconds (bool): Include microsconds. Defaults to False.
2024
2125 Returns:
2226 str: HDX date as a string
@@ -27,26 +31,34 @@ def get_hdx_date(
2731 timezone_handling = 3
2832
2933 if isinstance (date , str ):
30- date = parse_date (date , timezone_handling = timezone_handling )
34+ date = parse_date (
35+ date ,
36+ timezone_handling = timezone_handling ,
37+ include_microseconds = include_microseconds ,
38+ )
3139
3240 if ignore_timeinfo :
3341 if max :
3442 date = date .replace (
3543 hour = 23 ,
3644 minute = 59 ,
3745 second = 59 ,
38- microsecond = 0 ,
39- tzinfo = None ,
46+ microsecond = 999999 ,
4047 )
4148 else :
4249 date = date .replace (
43- hour = 0 , minute = 0 , second = 0 , microsecond = 0 , tzinfo = None
50+ hour = 0 ,
51+ minute = 0 ,
52+ second = 0 ,
53+ microsecond = 0 ,
4454 )
4555 else :
46- date = date .astimezone (timezone .utc ).replace (
47- microsecond = 0 , tzinfo = None
48- )
49- return date .isoformat ()
56+ date = date .astimezone (timezone .utc )
57+ if include_microseconds :
58+ timespec = "microseconds"
59+ else :
60+ timespec = "seconds"
61+ return date .replace (tzinfo = None ).isoformat (timespec = timespec )
5062
5163 @staticmethod
5264 def get_reference_period_info (
@@ -93,8 +105,8 @@ def get_reference_period_info(
93105 result ["startdate" ] = startdate
94106 result ["enddate" ] = enddate
95107 if date_format is None :
96- startdate_str = startdate .isoformat ()
97- enddate_str = enddate .isoformat ()
108+ startdate_str = startdate .isoformat (timespec = "seconds" )
109+ enddate_str = enddate .isoformat (timespec = "seconds" )
98110 else :
99111 startdate_str = startdate .strftime (date_format )
100112 enddate_str = enddate .strftime (date_format )
@@ -123,14 +135,23 @@ def get_hdx_reference_period(
123135 Returns:
124136 str: HDX reference period
125137 """
126- startdate = cls .get_hdx_date (startdate , ignore_timeinfo )
138+ startdate = cls .get_hdx_date (
139+ startdate ,
140+ ignore_timeinfo = ignore_timeinfo ,
141+ include_microseconds = False ,
142+ )
127143 if ongoing :
128144 enddate = "*"
129145 else :
130146 if not enddate :
131147 enddate = startdate
132148 else :
133- enddate = cls .get_hdx_date (enddate , ignore_timeinfo , max = True )
149+ enddate = cls .get_hdx_date (
150+ enddate ,
151+ ignore_timeinfo = ignore_timeinfo ,
152+ include_microseconds = False ,
153+ max = True ,
154+ )
134155 return f"[{ startdate } TO { enddate } ]"
135156
136157 @classmethod
0 commit comments