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

Configure the timezone that is assumed by parsing methods #273

Open
Azmisov opened this issue Jan 25, 2018 · 5 comments
Open

Configure the timezone that is assumed by parsing methods #273

Azmisov opened this issue Jan 25, 2018 · 5 comments

Comments

@Azmisov
Copy link

Azmisov commented Jan 25, 2018

When a user doesn't provide a timezone in a string, I would like to assume GMT/UTC timezone. Currently, if a timezone is not provided, it is assuming my local computer's timezone. For example (my computer is set to MST timezone):

let gmt = Date.parse("1/1/2017 10:45 GMT");
let mst = Date.parse("1/1/2017 3:45 MST");
let assume = Date.parse("1/1/2017 3:45);

console.log(+gmt == +mst); // true
console.log(+assume == +mst); // true, since my computer is in MST timezone

Can we add an argument to Date.parse to supply the UTC offset to assume, rather than using the computer's local timezone? This is the behavior I would expect:

let mst = Date.parse("1/1/2017 3:45 MST", "+0000");
let explicit = Date.parse("1/1/2017 10:45", "+0000");

console.log(+explicit == +mst); // should be true
@abritinthebay
Copy link
Owner

Easiest way to do that is to add the offset to the string (if one doesn't exist). Dates in JS are always local to the host computer. This will be true even if you set the offset (it'll return the date in the local timezone just modified so it's accurate if it was the time you wanted.

So without a major overhaul of JS date parsing the solution is "append the tz to your data"

@Azmisov
Copy link
Author

Azmisov commented Jan 25, 2018

Any suggestions on detecting whether an offset exists on the string already? I can't append it unconditionally, since DateJS parser seems to use that, instead of any previously defined offsets. Seems like I'm going to have to write my own complicated date parser, just to do that.

But doesn't DateJS's parser extract the offset and apply it already? Seems like it would be very simple just to add an override to that. That wouldn't be a major overhaul at all. Just something like:

Date.parse = function(str, assumed_offset){
    ...
    let offset = DateJSParser.parsed_offset || assumed_offset;
    if (offset)
        applyOffset(offset, ...);
    ...

@abritinthebay
Copy link
Owner

abritinthebay commented Jan 26, 2018

You're asking for a chain modifier - so chain modify it yourself?

Date.parse(string).addHour(offsetAmount) does exactly what you're requesting above.

@Azmisov
Copy link
Author

Azmisov commented Jan 26, 2018

But I want to add the offset only conditionally. Currently, there is no API to detect if the parser applied a user-defined offset. Example:

let has_user_offset= Date.parse("1/1/17 3:00 MST");
let no_user_offset = Date.parse("1/1/17 3:00");

How would I detect that the parser has added a 7 hour offset to has_user_offset and not to no_user_offset?

I mean, if you guys don't want to implement this that's fine. Its just a suggestion to help improve the library. I suppose I'll just have a separate select box for specifying a timezone, and then maybe use MomentJS's UTC parsing mode.

@abritinthebay
Copy link
Owner

It's not going to be a priority either way. What you're describing with MomentJS can be done with existing DateJS functions btw so I refer you to my previous solution.

That said - if you absolutely want it then PRs are welcome.

@abritinthebay abritinthebay reopened this Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants