[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I format date using timezone?

Posted on 2010-08-26
4
Medium Priority
?
423 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 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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…
Suggested Courses
Course of the Month19 days, 8 hours left to enroll

872 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