8000 Add support for creation with unix timestamp in seconds by vroy · Pull Request #213 · moment/moment · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for creation with unix timestamp in seconds #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2012
Merged

Add support for creation with unix timestamp in seconds #213

merged 4 commits into from
Mar 30, 2012

Conversation

vroy
Copy link
Contributor
@vroy vroy commented Mar 21, 2012

This is something that we wanted to avoid doing *1000 on every single UTC timestamp we receive (in seconds).

Figured I would suggest it as I think it makes a nicer API.

I can look into writing tests/docs for it first if you want.

@vroy
Copy link
Contributor Author
vroy commented Mar 21, 2012

Now that I look at the unix timestamp docs again, I guess it could be confusing. Let me know what you think.

@vroy
Copy link
Contributor Author
vroy commented Mar 29, 2012

Any feedback on this? If it doesn't fit in Moment, I can just close this pull request and keep the patch on our side.

@timrwood
Copy link
Member

Yeah, now that I look into it, I guess the docs are a bit of a misnomer. It should be something like 'Milliseconds since the Unix Epoch'.

I think I also need to make it much clearer that javascript measures time in milliseconds since the epoch, as that may be a common trip-up.

Also, I'll probably make the getter function named 'unix' instead of 'unixValueOf', as I only use 'valueOf' so that comparision operators can work (moment(1) > moment(0))

timrwood added a commit that referenced this pull request Mar 30, 2012
Add support for creation with unix timestamp in seconds
@timrwood timrwood merged commit 1f4ac69 into moment:master Mar 30, 2012
timrwood added a commit that referenced this pull request Mar 30, 2012
@timrwood
Copy link
Member

I also switched to flooring instead of rounding, as that's probably more accurate to how unix timestamps are calculated.

@vroy
Copy link
Contributor Author
vroy commented Mar 30, 2012

Fair enough. Thanks! :)

@slindberg
Copy link

I am dealing exclusively in unix timestamps (seconds) for the project I'm working on, so this is a welcomed addition. However I also deal exclusively in UTC timestamps, so the creation of non-localized moments still requires Moment.utc(timestamp * 1000). I can't think of a decent name for a creation function using UTC + unix timestamp in seconds, but provided it fits in the API, it would also be most welcomed.

@timrwood
Copy link
Member

@slindberg, you could use moment.unix(timestamp).utc().

moment.utc(timestamp) is functionally equivalent to moment(timestamp).utc().

The only other options I could think of are something like moment.utc.unix(timestamp) or moment.unix(timestamp, 'utc'), and would just add unneeded complexity.

@slindberg
Copy link

@timrwood, I mistakenly assumed there was overhead involved in creating a localized Moment instance that could be bypassed using the utc() creation function. I agree that any option for creating a UTC Moment using a unix timestamp is ugly and unnecessary. I will use your suggestion, and thanks for the quick reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0