sisyphsu / dateparser

dateparser is a smart and high-performance date parser library, it supports hundreds of different formats, nearly all format that we may used. And this is also a showcase for "retree" algorithm.
MIT License
95 stars 23 forks source link

update localDateTime converter default timeZone #27

Closed geniusjoe closed 2 years ago

geniusjoe commented 2 years ago

Do UNIX timestamps change across time zones?

No, UNIX timestamps are based on Universal Coordinated Time[1] and have no concept of timezone, everyone, everywhere in the world has the same notion of time references to Greenwich originally. It’s a monotonically increasing (binary) integer value, that does not look particularly useful to most people.

As mentioned above, the unix timestamp does not have timeZone concept. So we need to set a demand timeZone when converting from unix timestamp to java localDateTime type. Example below:

LocalDate date =
    Instant.ofEpochMilli(longValue).atZone(ZoneId.systemDefault()).toLocalDate();

When it comes to dateparser, we do not need to set timezone when using basic usage mode. And a unix timestamp will be converted to localDateTime using UTC+0 timezone currently. Because the localDateTime is designed for storing local time, I think it is better to convert the timestamp to localDateTime with local system timezone.

geniusjoe commented 2 years ago

TestDemo:

    @Test
    public void testDemo() {
        final DateParser dp = DateParser.newBuilder().build();
        System.out.println(String.format("current timezone = %s", ZoneId.systemDefault()));
        System.out.println();

        String date = "2022-09-09T09:30:57-00:00";
        System.out.println(String.format("parserDate          [%s] result [%s]", date, dp.parseDate(date).toString()));
        System.out.println(String.format("parseDateTime       [%s] result [%s]", date, dp.parseDateTime(date).toString()));
        System.out.println(String.format("parseOffsetDateTime [%s] result [%s]", date, dp.parseOffsetDateTime(date).toString()));
        System.out.println();

        //  convert date to unix timestamp format
        date = "1662715857";
        System.out.println(String.format("parserDate          [%s] result [%s]", date, dp.parseDate(date).toString()));
        System.out.println(String.format("parseDateTime       [%s] result [%s]", date, dp.parseDateTime(date).toString()));
        System.out.println(String.format("parseOffsetDateTime [%s] result [%s]", date, dp.parseOffsetDateTime(date).toString()));
    }

result before converting to default timezone:

current timezone = Asia/Shanghai

parserDate          [2022-09-09T09:30:57-00:00] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime       [2022-09-09T09:30:57-00:00] result [2022-09-09T17:30:57]
parseOffsetDateTime [2022-09-09T09:30:57-00:00] result [2022-09-09T09:30:57Z]

parserDate          [1662715857] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime       [1662715857] result [2022-09-09T09:30:57]       <-------- notice here
parseOffsetDateTime [1662715857] result [2022-09-09T09:30:57Z]

result after converting to default timezone:

current timezone = Asia/Shanghai

parserDate          [2022-09-09T09:30:57-00:00] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime       [2022-09-09T09:30:57-00:00] result [2022-09-09T17:30:57]
parseOffsetDateTime [2022-09-09T09:30:57-00:00] result [2022-09-09T09:30:57Z]

parserDate          [1662715857] result [Fri Sep 09 17:30:57 CST 2022]
parseDateTime       [1662715857] result [2022-09-09T17:30:57]       <-------- notice here
parseOffsetDateTime [1662715857] result [2022-09-09T09:30:57Z]
geniusjoe commented 2 years ago

@sisyphsu Hello, would you mind reviewing this pr ? 😁

sisyphsu commented 2 years ago

Thanks for your PR, using default timezone is more reasonable.

sisyphsu commented 2 years ago

This PR has been merged into new version 1.0.11~