Solved

How can I format date using timezone?

Posted on 2010-08-26
4
369 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 300 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now