Luxon: Introduce toUnix() and fromUnix()

Created on 23 May 2018  路  10Comments  路  Source: moment/luxon

Should we introduce the following functions?

// ts is UNIX timestamp in second
DateTime.fromMillis(ts * 1000) === DateTime.fromUnix(ts)
Math.round(dt.toMillis() / 1000) === dt.toUnix()

Most helpful comment

yes, we did eventually add those, since this was a popular request

All 10 comments

No, I'd prefer not to. I know it's a feature of Moment, but I want to keep conveniences like that out of Luxon.

@icambron May I know why?

Because for the caller, it's just the addition of / 1000 or * 1000, but for Luxon, it's file size, testing, and maintenance. Yes, that's all small, but so is the cost for the caller, and for Luxon there are lots of things at this level of convenience, and including them all would bloat Luxon without creating much value.

You can always define your own helpers for this. It's even safe to monkey patch, because it only uses public APIs:

DateTime.prototype.toUnix= () => ....

@icambron Thx very much for the explanation

@icambron what's the proper way to create a DateTime object from a unix timestamp with current API?

Never mind I just found it digging in Google (fromMillis).

In the whole https://moment.github.io/luxon/docs/manual/parsing.html there's no mention to "unix" or "timestamp" words! xD

@damianobarbati so what is the way?

It's fromMillis and toMillis as you said, https://codesandbox.io/s/lyj0qp979l

Isn't this just DateTime.fromSeconds and DateTime.prototype.toSeconds?

yes, we did eventually add those, since this was a popular request

Was this page helpful?
0 / 5 - 0 ratings