99
1010class DateHelper :
1111 @staticmethod
12- def get_date_info (
13- hdx_date : Dict ,
12+ def get_hdx_date (
13+ date : Union [datetime , str ], ignore_timeinfo : bool , max = False
14+ ):
15+ """Get an HDX date as a string from a datetime.datetime object.
16+
17+ Args:
18+ date (Union[datetime, str]): Date as datetime or string
19+ ignore_timeinfo (bool): Ignore time and time zone of date. Defaults to True.
20+
21+ Returns:
22+ str: HDX date as a string
23+ """
24+ if ignore_timeinfo :
25+ timezone_handling = 0
26+ else :
27+ timezone_handling = 3
28+
29+ if isinstance (date , str ):
30+ date = parse_date (date , timezone_handling = timezone_handling )
31+
32+ if ignore_timeinfo :
33+ if max :
34+ date = date .replace (
35+ hour = 23 ,
36+ minute = 59 ,
37+ second = 59 ,
38+ microsecond = 0 ,
39+ tzinfo = None ,
40+ )
41+ else :
42+ date = date .replace (
43+ hour = 0 , minute = 0 , second = 0 , microsecond = 0 , tzinfo = None
44+ )
45+ else :
46+ date = date .astimezone (timezone .utc ).replace (
47+ microsecond = 0 , tzinfo = None
48+ )
49+ return date .isoformat ()
50+
51+ @staticmethod
52+ def get_reference_period_info (
53+ hdx_reference_period : Dict ,
1454 date_format : Optional [str ] = None ,
1555 today : datetime = now_utc (),
1656 ) -> Dict :
17- """Get date as datetimes and strings in specified format. If no format is
18- supplied, the ISO 8601 format is used. Returns a dictionary containing keys
19- startdate (start date as datetime), enddate (end date as datetime),
20- startdate_str (start date as string), enddate_str (end date as string) and
21- ongoing (whether the end date is a rolls forward every day).
57+ """Get reference period as datetimes and strings in specified format.
58+ If no format is supplied, the ISO 8601 format is used. Returns a
59+ dictionary containing keys startdate (start date as datetime), enddate
60+ (end date as datetime), startdate_str (start date as string),
61+ enddate_str (enddate as string) and ongoing (whether the end date rolls
62+ forward every day).
2263
2364 Args:
24- hdx_date (str): Input date
65+ hdx_reference_period (str): Input reference period
2566 date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
2667 today (datetime): Date to use for today. Defaults to now_utc().
2768
2869 Returns:
2970 Dict: Dictionary of date information
3071 """
3172 result = dict ()
32- if hdx_date :
33- if hdx_date [0 ] == "[" and hdx_date [- 1 ] == "]" :
34- hdx_date = hdx_date [1 :- 1 ]
35- dataset_dates = hdx_date .split (" TO " )
73+ if hdx_reference_period :
74+ if (
75+ hdx_reference_period [0 ] == "["
76+ and hdx_reference_period [- 1 ] == "]"
77+ ):
78+ hdx_reference_period = hdx_reference_period [1 :- 1 ]
79+ dataset_dates = hdx_reference_period .split (" TO " )
3680 startdate = parse_date (dataset_dates [0 ])
3781 enddate = dataset_dates [1 ]
3882 if enddate == "*" :
@@ -59,15 +103,16 @@ def get_date_info(
59103 result ["ongoing" ] = ongoing
60104 return result
61105
62- @staticmethod
63- def get_hdx_date (
106+ @classmethod
107+ def get_hdx_reference_period (
108+ cls ,
64109 startdate : Union [datetime , str ],
65110 enddate : Union [datetime , str , None ] = None ,
66111 ongoing : bool = False ,
67112 ignore_timeinfo : bool = True ,
68113 ) -> str :
69- """Get an HDX date from either datetime.datetime objects or strings with option
70- to set ongoing.
114+ """Get an HDX reference period from either datetime.datetime objects or
115+ strings with option to set ongoing.
71116
72117 Args:
73118 startdate (Union[datetime, str]): Dataset start date
@@ -76,65 +121,33 @@ def get_hdx_date(
76121 ignore_timeinfo (bool): Ignore time and time zone of date. Defaults to True.
77122
78123 Returns:
79- str: HDX date
124+ str: HDX reference period
80125 """
81- if ignore_timeinfo :
82- timezone_handling = 0
83- else :
84- timezone_handling = 3
85-
86- def get_date_str (dt , max = False ):
87- if ignore_timeinfo :
88- if max :
89- dt = dt .replace (
90- hour = 23 ,
91- minute = 59 ,
92- second = 59 ,
93- microsecond = 0 ,
94- tzinfo = None ,
95- )
96- else :
97- dt = dt .replace (
98- hour = 0 , minute = 0 , second = 0 , microsecond = 0 , tzinfo = None
99- )
100- else :
101- dt = dt .astimezone (timezone .utc ).replace (
102- microsecond = 0 , tzinfo = None
103- )
104- return dt .isoformat ()
105-
106- if isinstance (startdate , str ):
107- startdate = parse_date (
108- startdate , timezone_handling = timezone_handling
109- )
110- startdate = get_date_str (startdate )
126+ startdate = cls .get_hdx_date (startdate , ignore_timeinfo )
111127 if ongoing :
112128 enddate = "*"
113129 else :
114130 if not enddate :
115131 enddate = startdate
116132 else :
117- if isinstance (enddate , str ):
118- enddate = parse_date (
119- enddate , timezone_handling = timezone_handling
120- )
121- enddate = get_date_str (enddate , max = True )
133+ enddate = cls .get_hdx_date (enddate , ignore_timeinfo , max = True )
122134 return f"[{ startdate } TO { enddate } ]"
123135
124136 @classmethod
125- def get_hdx_date_from_years (
137+ def get_hdx_reference_period_from_years (
126138 cls ,
127139 startyear : Union [str , int , Iterable ],
128140 endyear : Union [str , int , None ] = None ,
129141 ) -> Tuple [str , List [int ]]:
130- """Get an HDX date from an iterable of years or a start and end year.
142+ """Get an HDX reference period from an iterable of years or a start and
143+ end year.
131144
132145 Args:
133146 startyear (Union[str, int, Iterable]): Start year given as string or int or range in an iterable
134147 endyear (Union[str, int, None]): End year given as string or int
135148
136149 Returns:
137- Tuple[str,List[int]]: (HDX date , the start and end year if supplied or sorted list of years)
150+ Tuple[str,List[int]]: (HDX reference period , the start and end year if supplied or sorted list of years)
138151 """
139152 retval = list ()
140153 if isinstance (startyear , str ):
@@ -152,6 +165,8 @@ def get_hdx_date_from_years(
152165 startdate = datetime (startyear , 1 , 1 , tzinfo = timezone .utc )
153166 enddate = datetime (endyear , 12 , 31 , 23 , 59 , 59 , tzinfo = timezone .utc )
154167 return (
155- cls .get_hdx_date (startdate , enddate , ignore_timeinfo = False ),
168+ cls .get_hdx_reference_period (
169+ startdate , enddate , ignore_timeinfo = False
170+ ),
156171 retval ,
157172 )
0 commit comments