Set timezone within node?

15185 views
Skip to first unread message

Egor Egorov

unread,
Oct 10, 2010, 6:09:36 PM10/10/10
to nodejs
Hello.

How do I explicitly set a timezone for a node application? Right now I
remember to always set TZ environment variable before running the app,
but this is not handy. (My app needs to run in a specific timezone
only, no matter where it is executed).

Ben Noordhuis

unread,
Oct 10, 2010, 6:43:12 PM10/10/10
to nod...@googlegroups.com

You can't right now.

Ideally, you should be able to set process.env.TZ = 'Europe/Amsterdam'
at startup and have it do the Right Thing but writes to process.env
don't update the real environment. This is trivially easy to fix but I
suppose no one has gotten around to it yet.

A workaround you can use is to restart the process with
child_process.spawn() and pass in a custom environment.

Ben Noordhuis

unread,
Oct 10, 2010, 8:02:37 PM10/10/10
to nod...@googlegroups.com
Couldn't sleep so I went and fixed it.

There is one minor difference with the original process.env in that
envvars without a '=' are not filtered. The original did so but I
believe that is actually a bug (or information hiding at the very
least).

0001-Make-writes-to-process.env-update-the-real-environme.patch

Oleg "Sannis" Efimov

unread,
Oct 10, 2010, 8:17:57 PM10/10/10
to nodejs
Nice to have this in node :-)
>  0001-Make-writes-to-process.env-update-the-real-environme.patch
> 3KViewDownload

Tane Piper

unread,
Oct 11, 2010, 4:28:52 AM10/11/10
to nodej...@googlegroups.com, nodejs
I've forwarded this to the node-dev list with the patch +1 from me

--
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en.

0001-Make-writes-to-process.env-update-the-real-environme.patch

r...@tinyclouds.org

unread,
Oct 11, 2010, 2:02:00 PM10/11/10
to nod...@googlegroups.com

I'm down with this, but it should have a test as it doesn't appear to
solve the problem:

ryan@mac1234:~/projects/node% ./node
> d = new Date()
Mon, 11 Oct 2010 17:59:41 GMT
> d.toLocaleTimeString()
'10:59:41'
> process.env.TZ = 'Europe/Amsterdam'
'Europe/Amsterdam'
> d.toLocaleTimeString()
'10:59:41'

There are also some style issues - long line, bad indent.

Ben Noordhuis

unread,
Oct 11, 2010, 2:24:17 PM10/11/10
to nod...@googlegroups.com
On Mon, Oct 11, 2010 at 20:02, <r...@tinyclouds.org> wrote:
> I'm down with this, but it should have a test as it doesn't appear to
> solve the problem:

TZ should be set before you call any date-related functions. I'll mail
the test tomorrow.

> There are also some style issues - long line, bad indent.

Sorry about that, I blame Eclipse. Attached is a patch with the style
issues fixed.

Are there any coding guidelines? Line length, bracketing, that kind of thing?

0001-Make-writes-to-process.env-update-the-real-environme.patch

r...@tinyclouds.org

unread,
Oct 11, 2010, 2:30:05 PM10/11/10
to nod...@googlegroups.com
On Mon, Oct 11, 2010 at 11:24 AM, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
> On Mon, Oct 11, 2010 at 20:02,  <r...@tinyclouds.org> wrote:
>> I'm down with this, but it should have a test as it doesn't appear to
>> solve the problem:
>
> TZ should be set before you call any date-related functions. I'll mail
> the test tomorrow.

Oh, right - tzset() won't be called again. Okay.

>> There are also some style issues - long line, bad indent.
>
> Sorry about that, I blame Eclipse. Attached is a patch with the style
> issues fixed.
>
> Are there any coding guidelines? Line length, bracketing, that kind of thing?

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Oleg "Sannis" Efimov

unread,
Oct 11, 2010, 5:06:06 PM10/11/10
to nodejs

Ben Noordhuis

unread,
Oct 15, 2010, 10:02:04 AM10/15/10
to nod...@googlegroups.com
Thanks for the pointers, guys.

Attached is a patch that includes tests and fixes a bug caused by a
rather interesting and not terribly well-documented feature of V8's
named property handler support[1].

As Martha Stewart would say in that inimitable way of hers: "Unit
testing. It's a good thing."

[1] Deletion of properties didn't work. As it turns out, the query
callback needs to return an empty handle for deleted properties. That
is, Handle<Integer>() and not Handle<Integer>(Integer::New(0)).

0001-Make-writes-to-process.env-update-the-real-environme.patch

Isaac Schlueter

unread,
Oct 15, 2010, 2:42:45 PM10/15/10
to nod...@googlegroups.com
Oh my goodness, yes.

+1

--i

Bruno Jouhier

unread,
Oct 16, 2010, 6:45:59 AM10/16/10
to nodejs
In web app scenarios, it would be nice if timezone was not a global
setting but could instead be set by applications when they dispatch a
request. The application would get the timezone from a cookie or a
session variable and would set it explicitly before dispatching the
request.

The idea is that times that are included in server generated messages
should be formatted in the user's timezone rather than in the server's
timezone.

We would also need a mechanism to propagate the timezone setting (as
well as the locale) along the chain of callbacks, so that the user
gets a consistent formatting of date and times. See my post from 2
days ago: http://groups.google.com/group/nodejs/browse_thread/thread/a7e63a5e4da719b2

Bruno

Isaac Schlueter

unread,
Oct 16, 2010, 12:14:01 PM10/16/10
to nod...@googlegroups.com
Bruno:

In that case, you could just set the TZ right before getting the Date
string. Or, you could send a Date.now() epoch timestamp, and then
have a Date object on the client side turn that into a localized time
string. There are many ways to solve your problem, but I think
maintaining a per-request environment is an overly expensive approach.

This patch is still very helpful. process.env should be a real
interface to the environment, not just a snapshot.

--i

ry

unread,
Nov 3, 2010, 3:25:58 PM11/3/10
to nodejs
>  0001-Make-writes-to-process.env-update-the-real-environme.patch
> 6KViewDownload


thanks. Landed in b4def48
Reply all
Reply to author
Forward
0 new messages