go-dateparser icon indicating copy to clipboard operation
go-dateparser copied to clipboard

How to distinguish `2024.09.05` and `2024.09.05 00:00:00`?

Open prokher opened this issue 2 years ago • 5 comments

First, thank you for the very useful library.

I cannot get, how to check whether the user entered time 00:00:00 or just specified a date. In both cases, I get 2024.09.05 00:00:00 and Period set to Day. What am I doing wrong?

prokher avatar Sep 05 '24 16:09 prokher

Since it's a port of the python library could you check whether the python library produces the same output? We still might want to address this but it would be great to kno first whether we created a regression during porting or whether this affects the original as well.

markusmobius avatar Sep 05 '24 16:09 markusmobius

@markusmobius, thank you for quick response. I will check and let you know.

prokher avatar Sep 05 '24 17:09 prokher

Well, in the Python library I've found I can use RETURN_TIME_AS_PERIOD which, according to the documentation, "returns time as period in date object, if time component is present in date string". And it seems to work.

import dateparser 
ddp = dateparser.DateDataParser(settings={'RETURN_TIME_AS_PERIOD': True})
ddp.get_date_data('2024.09.05')
# Output: DateData(date_obj=datetime.datetime(2024, 9, 5, 0, 0), period='day', locale='en')
ddp.get_date_data('2024.09.05 00:00:00')
# Output: DateData(date_obj=datetime.datetime(2024, 9, 5, 0, 0), period='time', locale='en')

In the Go implementation I also tried ReturnTimeAsPeriod: True which seems to work. At least it returns Hour as Period when time is provided, and Day when there is no time specification. It looks fine for me. Honestly I would expect to have Second as Period when I provide 2024.09.05 00:00:00, but for my case it is not critical since I only need to determine whether there was time specified or not.

prokher avatar Sep 05 '24 17:09 prokher

Thank you for checking @prokher ! I will leave the issue open for now so that we can check whether we can reduce this discrepancy between the python and go versions in the future.

markusmobius avatar Sep 05 '24 19:09 markusmobius

Honestly I would expect to have Second as Period when I provide 2024.09.05 00:00:00, but for my case it is not critical since I only need to determine whether there was time specified or not.

With 43bf69a, now go-dateparser can detect the period up to minute and second:

var dt date.Date
cfg := &dateparser.Configuration{ReturnTimeAsPeriod: true}

dt, _ = dateparser.Parse(cfg, "2024.09.05")
// output => locale: "en", period: "day", parse result: "2024-09-05 00:00:00 +0000 UTC"

dt, _ = dateparser.Parse(cfg, "2024.09.05 11 AM")
// output => locale: "en", period: "hour", parse result: "2024-09-05 11:00:00 +0000 UTC"

dt, _ = dateparser.Parse(cfg, "2024.09.05 11:00 AM")
// output => locale: "en", period: "minute", parse result: "2024-09-05 11:00:00 +0000 UTC"

dt, _ = dateparser.Parse(cfg, "2024.09.05 11:00:00 AM")
// output => locale: "en", period: "second", parse result: "2024-09-05 11:00:00 +0000 UTC"

I'll close this issue now, feel free to reopen if needed.

RadhiFadlillah avatar Apr 29 '25 12:04 RadhiFadlillah