Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python - how to convert UTC time string to local time

Posted on 2016-07-29
4
Medium Priority
?
769 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 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…
Suggested Courses

916 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