assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Implement the Date standard library

Open LiaoPeng opened this issue 5 years ago • 13 comments

Implement the date standard library

Finished:

  • [x] getFullYear, getMonth, getDate, getDay, getHours, getMinutes, getSeconds, getMilliseconds
  • [x] setFullYear, setMonth, setDate, setHours, setMinutes, setSeconds, setMilliseconds
  • [x] get UTC timestamp
  • [x] enhancement the effective
  • [x] getTimezoneOffset

LiaoPeng avatar Dec 04 '18 06:12 LiaoPeng

One thing I am not sure of is that each Date instance has a new reference to BaseCalendar. Is this actually necessary? Looks like BaseCalendar could also be implemented in a more global/functional style so it doesn't have to allocate that much.

dcodeIO avatar Dec 07 '18 13:12 dcodeIO

It's not necessary. I would remove the BaseCalendar.

LiaoPeng avatar Dec 10 '18 08:12 LiaoPeng

I have a question how to get the local TimeZone. It blocked me.

LiaoPeng avatar Dec 10 '18 10:12 LiaoPeng

@MaxGraey The method getTimezoneOffset Return the timezone difference between UTC and Local Time. Both Date.utc and Date.now return the UTC time. I am not sure that's ok.

LiaoPeng avatar Dec 10 '18 12:12 LiaoPeng

It's a shame that I haven't reviewed this more thoroughly. Looking at it again I think that this went stale in my head because it needs so many managed objects to represent a single date, and a more C-like implementation with data living on the date objects felt more efficient. In general I think that this is a good approach as it passes all the tests, so wondering if you are still interested in continuing on this PR?

dcodeIO avatar May 16 '20 22:05 dcodeIO

@dcodeIO I'm very interested in this PR. The implementation of the date library referred the implementation of java date. If you think a more C-like implementation are more efficient. I would try it.

LiaoPeng avatar Jul 01 '20 13:07 LiaoPeng

That would be fantastic, yeah. To elaborate a bit on the C-likeness, there currently are instances of CalendarDate and TimeZone required to represent a Date, so there's quite a bit of managed objects to reference count and collect. Ideally I think that having a std/util/calendar.ts exposing plain functions to work with let's say i64 values representing these would be more in-line with other stdlib internals, so that we have just a single allocation per Date instance to keep track of. Do you think it'd be possible to transform the existing code into this direction?

dcodeIO avatar Jul 01 '20 13:07 dcodeIO

Also it will be ideal if you organize the code in such a way that it could be reused for Temporal proposal (https://github.com/tc39/proposal-temporal/tree/main/polyfill/lib) in future

MaxGraey avatar Jul 01 '20 13:07 MaxGraey

Oh, wasn't aware of this one. Interesting.

dcodeIO avatar Jul 01 '20 14:07 dcodeIO

@LiaoPeng Ah! Thank you very much for opening this! :smile: This is a ton of awesome work, it is super appreciated! :smile:

While you work with @dcodeIO and @MaxGraey on building this out as a c-style API, and if you think it would take a while. I'd be interested in pulling this Date implementation into it's own library that could be npm install'd? I know it's not the most optimal for performance currently, but I think it'd be useful to a few folks in the communitty in the meantime :smile:

That is of course, if you wouldn't mind me doing so (giving this PR and you full credit @LiaoPeng ) ? Or we could start it together? Whichever works!

Thanks y'all! :smile:

torch2424 avatar Feb 10 '21 01:02 torch2424

@torch2424 Hey,it would take a while. Your advice is good, just do it. Now I push new commit rebased the current master branch.

LiaoPeng avatar Feb 13 '21 12:02 LiaoPeng

@LiaoPeng Ah! Thank you for rebasing! :smile: :pray:

Your advice is good, just do it.

So just to confirm, you're okay with me using the code in the PR on a torch2424/as-date repo? (Pointing back to you and this PR of course) :smile:

torch2424 avatar Feb 16 '21 23:02 torch2424

@torch2424 I am okey,

LiaoPeng avatar Feb 18 '21 15:02 LiaoPeng

Various Date functions have been implemented meanwhile, avoiding intermediary allocations.

dcodeIO avatar Sep 27 '22 13:09 dcodeIO