mint icon indicating copy to clipboard operation
mint copied to clipboard

Time.now returns wrong date / timezone issues

Open Namek opened this issue 3 years ago • 3 comments

Current implementation of Time.today is working somewhat wrong. I use Mint ver tagged as 15.2.

My current time is 1st March 00:40 and I get 28th Feb from calling this function. This is counter-intuitive because Time.now is different (returns 1st March)

The implementation is (Mint):

  fun today : Time {
    atBeginningOfDay(now())
  }

  fun now : Time {
    `new Date()`
  }

  fun atBeginningOfDay (time : Time) : Time {
    `
    (() => {
      const time = new Date(+#{time});
      time.setUTCHours(0, 0, 0, 0);
      return time;
    })()
    `
  }

which in practice is (JS):

new Date(new Date().setUTCHours(0, 0, 0, 0));

The Date constructor creates object with local timezone and then we change it to UTC timezone with setUTCHours. I believe it should be setHours called instead. Otherwise Time.today returns a date with time 01:00 instead of time 00:00.

I would make a PR although I am not sure of a direction of the rest of the functions in the Time module. For example there is Time.local function on repo that is not available in v15.2. And that function also looks not correctly implemented because it's based on Time.now which already returns local time, as stated above.

Namek avatar Mar 01 '22 00:03 Namek

Hi :wave:

The Time module has been rewritten for the next version, for details check the PR: #526 and the documentation: https://github.com/mint-lang/mint/blob/master/documentation/Core/Time.md

In short, all Time is without time-zone information, so basically UTC. This is a best practice since the different people can be in different time zones, and with a UTC time we can format them in any time-zone (or language).

If you need to local time Time.local can be used.

gdotdesign avatar Mar 01 '22 05:03 gdotdesign

Hi again, sorry so late but I got COVID during that time.

Well, I am not sure what the state of it is right NOW (29 days later) but back then I already played with the merged PR API you mentioned.

Time is without time-zone information

I agree it should be like that.

Time.local

If you need to local time Time.local can be used.

According to the https://www.mint-lang.com/api/modules/Time it does not exist.

Time.today

However, the general issue is that it is all messed up about local timezones between various functions and I am not sure how it should be changed. So let's focus on this issue I originally mentioned:

Time.today returns a date with time 01:00 instead of time 00:00.

It shouldn't work like that, right?

Namek avatar Mar 30 '22 11:03 Namek

The time related changes are still not released, but will be soon in 0.16.0.

These are the functions to get a Time in master:

  • Time.now - returns the current UTC time
  • Time.local - returns the current time offset by clients time-zone
  • Time.today - returns the time beggining of today in UTC

The confusion might be that Time.now just returns new Date which in JavaScript land has the time zone offset, but when formatting in Mint it will actually use the UTC parts of it (Time.hour for example) so as far as Mint concerned it's in UTC.


Time.today will always return the the UTC version of the given date, so for example if Time.now is 2022-04-01 00:30 then Time.today will be 2022-04-01 00:00, but if you are in a time zone which is one hour behind then Time.local will be 2022-03-31 23:30 and then Time.today will be 2022-03-31 00:00 UTC.

gdotdesign avatar Apr 01 '22 06:04 gdotdesign

The refactor is now out so closing this.

gdotdesign avatar Apr 26 '23 13:04 gdotdesign