I am working with a feature to generate a duration once a date(can be incomplete) is specified by the user.
- if it is a complete date (xxxx-xx-xx), the period should be xxxx-xx-xx 0:00 to xxxx-xx-xx 23:59.
- if it is an incomplete date only with year and month xxxx-xx, the period should be xxxx-xx-01 0:00 to xxxx-xx-31 23:59.
- if it is an incomplete date only with year xxxx, the period should be xxxx-01-01 0:00 to xxxx-12-31 23:59.
user inputs can be in any form.
I am using this super cool dateutil.parse which is working smoothly in capturing date information. It can also capture incomplete dates and set what missing to a default value. Ex:
out_date = dparser.parse("2021",default=datetime.datetime(2021, 1, 1))
#out 2021-01-01 00:00:00
out_date = dparser.parse("2021 june",default=datetime.datetime(2021, 1, 1))
#out 2021-06-01 00:00:00
out_date = dparser.parse("2021 sep 17",default=datetime.datetime(2021, 1, 1))
#out 2021-09-17 00:00:00
As my concern is to return a period, I want to differentiate between inputs "2021" and "2021-01-01" where the output will be the same with the above method.
Basically, I want is to capture the missing values so that I can process those as I wish. Ex:
input = "2021" > output = (2021, false, false)
input = "2021 jan" > output = (2021, 01, false)
input = "2021 jan 15" > output = (2021, 01, 15)
Any help would be highly appreciated.
PS: inputs can be out of structure such as "2021/01/01", "January 15, 2021", "January 2021"