Skip to content
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

Bug when parsing unix timestamp and using default time zone #338

Open
maggiepint opened this issue Apr 30, 2016 · 5 comments
Open

Bug when parsing unix timestamp and using default time zone #338

maggiepint opened this issue Apr 30, 2016 · 5 comments
Labels

Comments

@maggiepint
Copy link
Member

As reported in moment/moment#3158 there is an issue where parsing an 'X' token will not work correctly with a default timezone.

moment.tz.setDefault("America/Chicago");

moment("1461906597", "X").isValid(); // false

This is an issue with the way updateOffset handles default time zones.

        if (mom._z === undefined) {
            if (zone && needsOffset(mom) && !mom._isUTC) {
                mom._d = moment.utc(mom._a)._d;
                mom.utc().add(zone.parse(mom), 'minutes');
            }
            mom._z = zone;
        }

This bit of code will attempt to take the initial _a array from the moment config, and use it to construct a new UTC moment. With a timestamp, _a is an array with a bunch of undefined values, ergo this doesn't work.

@westy92
Copy link

westy92 commented May 19, 2016

I added a unit test for this to my fork of the repo: westy92@dde7030.

@mityukov
Copy link

Sorry, is there a solution for this issue at the moment (besides of the unit test added)?

Or, is there any other way to apply kind of "default" timezone to all subsequent moment().format() calls?

@mattjohnsonpint
Copy link
Contributor

The workaround for now is to pass the timestamp as a number instead of a string.

moment.unix(1461906597) // seconds
moment(1461906597123)   // milliseconds

If you have a string, then use + or parseInt to convert to number.

@mityukov
Copy link

mityukov commented Jul 27, 2017 via email

@mattjohnsonpint
Copy link
Contributor

It's a better form to use anyway, and yes - it will work with the default time zone. The bug is only that the default timezone isn't working with x or X string formats.

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

No branches or pull requests

4 participants