Solved

Python - how to convert UTC time string to local time

Posted on 2016-07-29
4
72 Views
Last Modified: 2016-08-01
I am getting this string from an API.
EX: 1469836074

If I go to this website and put the UTC string into it:
http://www.epochconverter.com/

Then I get human readable form:
GMT: Fri, 29 Jul 2016 23:47:54 GMT
Your time zone: 7/29/2016, 4:47:54 PM GMT-7:00 DST

What python library would take INPUT: 1469836074 and give output as something like:
GMT: Fri, 29 Jul 2016 23:47:54 GMT
Your time zone: 7/29/2016, 4:47:54 PM GMT-7:00 DST
or something like that.....

Thank you!
0
Comment
Question by:alexmac05
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 500 total points
ID: 41735424
Well, you can either use

Import time

or

import calendar

The number in question is the number of seconds since 12:00:00 am January 1, 1970 UTC.  You would need to apply the local time offset to derive the time the number represents locally.

You can use
print time.strftime("%Z - %Y/%m/%d, %H:%M:%S", time.localtime(time.time()))

Open in new window

and replace time.time() with the epoch you wish to convert.

%Z is the local timezone as configured on the system.
0
 

Author Comment

by:alexmac05
ID: 41735851
Thank you!

This worked.
print time.strftime("%Z - %Y/%m/%d, %H:%M:%S", time.localtime(float(utcValue)))

where utcValue was a string = '1469836074'

this gave me the desired result of PDT - 2016/07/29, 16:47:54

Thank you thank you
0
 

Author Closing Comment

by:alexmac05
ID: 41735852
thank you
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
ID: 41737652
Always happy to help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now