Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I format date using timezone?

Posted on 2010-08-26
4
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
markaaa earned 800 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 29

Assisted Solution

by:pepr
pepr earned 1200 total points
ID: 33539229
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 1200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…
Suggested Courses

609 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