Solved

Python - how to convert UTC time string to local time

Posted on 2016-07-29
4
151 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Is my Java RTS game window portable to Python? 7 99
any impressive Python code for a child? 3 127
Using bootstrap with french text 13 95
Python 3.5.2 - Script Help 3 66
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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 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 …

808 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