tags:

views:

396

answers:

4

Currently my method involves creating a context processor which is then included sitewide. However, I notice I have problems accounting for daylight saving time changes. I live outside of the U.S. BTW and my server is in the U.S. but its time zone is set to mine. However since we don't account for daylight saving time, it's always one hour ahead of my local time.

Any idea how to account for this? I have used timedelta -1 in the past but somehow I don't feel this is the right solution.

+1  A: 

If the server is set to your timezone and you want to display time in your timezone, you should just be able to use something like:

{% now M d, Y h-ia %}

To give something like: Jan 23, 2009 9:21a.m.

See http://docs.djangoproject.com/en/dev/ref/templates/builtins/

Travis Jensen
thank you this helped also, as for some reason i hadn't noticed the Now template filter
Rasiel
A: 

It sounds like, while the time is set to your time, the timezone isn't.

If this is a Linux server, the timezone is controlled by the file /etc/localtime

This is actually a symbolic link to one of the timezone files in /usr/share/zoneinfo . The default is probably something like /usr/share/zoneinfo/America/New_York, but you can change it to /usr/share/zoneinfo/America/Belize * by doing the following:

ln -sf /usr/share/zoneinfo/America/Belize /etc/localtime

The location will be slightly different if you're using a different *nix OS. For example, Solaris has timezone files in /usr/share/lib/zoneinfo

This is assuming you have root access on the machine, as you have access to change the system clock.

*Your location is listed as Belize in your profile, in case you wondered where I got that.

R. Bemrose
as noted above, django doesn't use the server time, but thanks nonetheless
Rasiel
I had deleted this answer after upvoting the other answer, but then I thought "Maybe this will be useful to someone anyway" and undeleted it.
R. Bemrose
A: 

If you are only concerned with displaying the date and time on the client side, you could just use Javascript, which will use the date and time of the client computer.

There may be reasons you don't want to do this, but I thought I'd throw it out there anyway.

Grant
thanks but no, i want to display the time in my local country.
Rasiel
+2  A: 

The time-zone you have set on the server shouldn't matter from within your Django app. Django will return the time based on what your TIME_ZONE variable is set to in your settings.py, not what you have set on the server. For example, one server may serve multiple Django-powered sites, each with a separate time-zone setting.

See: http://docs.djangoproject.com/en/dev/ref/settings/#time-zone.

Just make sure you set your TIME_ZONE variable to the Zoneinfo name (ie. America/Belize) instead of just GMT-6, because that won't account for daylight savings. Using the Zoneinfo name, Django will return the daylight-savings aware version of the time. So set that up right, and you should be able to get the time-zone you want in your Django app.

{% now h:ia %}

That'll return the time in a format like 9:30am.

Mike Richards
In this case, his time zone should probably be America/Belize
R. Bemrose
Ah, right. I'll change it to that :)
Mike Richards
perfect! worked wonderfully, and i think this is the way it should be... isn't django sweet?
Rasiel
plus stackoveflow is sweet also.. got me my answer real fast!
Rasiel