samsonjs / strftime

strftime for JavaScript

Home Page:http://samhuri.net/projects/strftime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Way to return times in a specific time zone

Fauntleroy opened this issue · comments

I need to be able to return strings in a specific time zone, regardless of the timezone the current runtime is in. For instance, if I run this script and the local time zone is EST, but the provided time zone is PST, I'd like to render it as if it were PST.

How do you want to pass in the time zone? I'm thinking of adding a function that returns a wrapped strftime that applies an offset before calling strftime. Something like:

var strftimePST = strftime.forTimeZone(-420); // tz in minutes from UTC

My first thought was to pass another argument, like so:

var datetime_pst = strftime( format, date, timezone );

Where timezone is a number that represents the offset, or the abbreviation of a time zone (if there's a good standard to follow for that). I'm pretty sure other implementations of strftime have a way of setting the time zone explicitly like this, and I think it's similar to what you suggested above. My main concern is being able to set the time zone every time I run strftime... so long as that's possible I'm not super picky about how exactly it's implemented.

That being said, some sort of time zone knowledge would be ideal, as some time zones, especially in the US, periodically change their behavior throughout the year.

There is already an optional argument to use UTC, so perhaps it can become
an options argument. Doing a numeric offset is easy, I can definitely do
that. I'll look into time zone name to offset mappings as well.

-s
On Oct 5, 2013 7:58 PM, "Timothy Kempf" notifications@github.com wrote:

My first thought was to pass another argument, like so:

var datetime_pst = strftime( format, date, timezone );

Where timezone is a number that represents the offset, or the
abbreviation of a time zone (if there's a good standard to follow for
that). I'm pretty sure other implementations of strftime have a way of
setting the time zone explicitly like this, and I think it's similar to
what you suggested above. My main concern is being able to set the time
zone every time I run strftime... so long as that's possible I'm not
super picky about how exactly it's implemented.

That being said, some sort of time zone knowledge would be ideal, as some
time zones, especially in the US, periodically change their behavior
throughout the year.


Reply to this email directly or view it on GitHubhttps://github.com//issues/28#issuecomment-25761369
.

Hmm, yeah we would have to map the timezone offset into a timezone code. That would include handling daylight savings correctly and keeping up to date when DST changes in various places, which is tricky. I haven't found a way to change the timezone of a date in JavaScript yet. This is a real problem though. I'm definitely open to suggestions.

I drew up an API for passing in timezone offsets as minutes from GMT in the timezones branch. I won't merge it just yet though. Let me know if the API works for you (aside from %Z being incorrect).

var strftimeTZ = require('strftime').strftimeTZ
console.log(strftimeTZ('%B %d, %y %H:%M:%S', new Date(1307472705067), -420))
// => June 07, 11 11:51:45
console.log(strftimeTZ('%F %T', new Date(1307472705067), 120))
// => 2011-06-07 20:51:45

I find it a bit odd that it's in strftimeTZ instead of the base method, but it's not really a problem, as it seems that would work. Timezones would really put this over the top, but I know that it's quite an issue to tackle with time zone issues being as complicated as they are.

I agree it's odd, and I apologize. It would be nice to accept an options object but I don't want to break backwards compatibility just yet.

Getting rid of daylight savings is my dream. It will never happen 😢

@samsonjs it's been a while so I figured I'd check up and see what the status of this is. It looks like you've got this working with time offsets, which is good enough for the short term. I'd love to see it merged into master and released! ☺️

Hey Fauntleroy, sorry about that! I just forgot about it. Merged & released on npm as v0.7.0. In the future I can rework the API to be better. I'll open a separate issue for that. For now I am punting on the timezone name issue.