Solved

How can I format date using timezone?

Posted on 2010-08-26
4
382 Views
Last Modified: 2012-05-10
Hi,

How can I format date using timezone using the epoch time? Using python 2.5
i.e.
t = time.gmtime(sec)
print time.strftime("%b %d %Y %H:%M:%S", t)
returns Aug 26 2010 16:53:49, which is GMT time

t = time.localtime(sec)
print time.strftime("%b %d %Y %H:%M:%S", t)
returns Aug 26 2010 9:53:49, which is PST

How can I get this in Mountain timezone, any other timezone the user passes in?

Also, where are the list of timezone constants in Python?

Thanks
Jamie

0
Comment
Question by:jamie_lynn
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
markaaa earned 200 total points
ID: 33534372
The Python standard library doesn't contain timezone information and doesn't have good timezone support. You will need to use a package such as pytz and there are still some platform specific issues. You can get pytz here and they show examples of usage. http://pytz.sourceforge.net/
0
 

Author Comment

by:jamie_lynn
ID: 33538699
Hi markaaa,

If my localtime is UTC,
In the standard library, is there way determine if daylight savings for Pacific? with this info, i can calculate the offset from GMT.

Thanks
Jamie
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 300 total points
ID: 33539229
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 300 total points
ID: 33540321
As markaaa suggested, you probably should download the pytz (http://sourceforge.net/projects/pytz/).  After installation ('python setup.py install' from the directory created from the unzipped archive).

Try the snippet below. It generates the files with timezones, and it shows the same UTC time naturally for different timezones.  It shows the following on my machine:

C:\tmp\___python\jamie_lynn\Q_26432050>a.py
Generating 'all_timezones.txt'
Generating 'common_timezones.txt'
Generating 'US_timezones.txt'
UTC 2010-08-27 09:52:36 UTC+0000
Australia/Sydney 2010-08-27 19:52:36 EST+1000
Pacific/Auckland 2010-08-27 21:52:36 NZST+1200
import datetime
import pytz
import time

fname = 'all_timezones.txt'
print 'Generating', repr(fname)
f = open(fname, 'w')
for z in pytz.all_timezones:
    f.write(repr(z) + '\n')  # repr() to wrap in quotes
f.close()

fname = 'common_timezones.txt'
print 'Generating', repr(fname)
f = open(fname, 'w')
for z in pytz.common_timezones:
    f.write(repr(z) + '\n')
f.close()

fname = 'US_timezones.txt'
print 'Generating', repr(fname)
f = open(fname, 'w')
for z in pytz.country_timezones('US'):
    f.write(repr(z) + '\n')
f.close()



fmt = '%Y-%m-%d %H:%M:%S %Z%z'

utc_dt = pytz.utc.localize(datetime.datetime.utcnow())
print 'UTC', utc_dt.strftime(fmt)

au_tz = pytz.timezone('Australia/Sydney')
au_dt = au_tz.normalize(utc_dt.astimezone(au_tz))
print 'Australia/Sydney', au_dt.strftime(fmt)

pa_tz = pytz.timezone('Pacific/Auckland')
pa_dt = pa_tz.normalize(utc_dt.astimezone(pa_tz))
print 'Pacific/Auckland', pa_dt.strftime(fmt)

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now