Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to UTC Timestamp

# parses some string into that format.
datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S")

# gets the seconds from the above date.
timestamp1 = time.mktime(datetime1.timetuple())

# adds milliseconds to the above seconds.
timeInMillis = int(timestamp1) * 1000

How do I (at any point in that code) turn the date into UTC format? I've been ploughing through the API for what seems like a century and cannot find anything that I can get working. Can anyone help? It's currently turning it into Eastern time i believe (however I'm in GMT but want UTC).

EDIT: I gave the answer to the guy with the closest to what I finally found out.

datetime1 = datetime.strptime(somestring, someformat)
timeInSeconds = calendar.timegm(datetime1.utctimetuple())
timeInMillis = timeInSeconds * 1000

:)

like image 717
Federer Avatar asked Oct 20 '09 14:10

Federer


People also ask

How do you convert timestamp to UTC time?

You can just create UTC Timestamps with the full date. You can use the . getTime() function of the Date object. You will get the milliseconds since 1970/01/01 and then divide it with 1000 to get the seconds.

What is UTC time stamp?

By convention, meteorologists use just one time zone: Universal Time, Coordinated (UTC). They also use the twenty four hour clock (where 0000 = midnight UTC). The date/time stamp on each forecast image represents the time at which the forecast is valid, measured in UTC.

Is timestamp always UTC?

A few things you should know about Unix timestamps:Unix timestamps are always based on UTC (otherwise known as GMT). It is illogical to think of a Unix timestamp as being in any particular time zone. Unix timestamps do not account for leap seconds.


3 Answers

datetime.utcfromtimestamp is probably what you're looking for:

>>> timestamp1 = time.mktime(datetime.now().timetuple())
>>> timestamp1
1256049553.0
>>> datetime.utcfromtimestamp(timestamp1)
datetime.datetime(2009, 10, 20, 14, 39, 13)
like image 157
SilentGhost Avatar answered Sep 23 '22 19:09

SilentGhost


def getDateAndTime(seconds=None):
 """
  Converts seconds since the Epoch to a time tuple expressing UTC.
  When 'seconds' is not passed in, convert the current time instead.
  :Parameters:
      - `seconds`: time in seconds from the epoch.
  :Return:
      Time in UTC format.
"""
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(seconds))`

This converts local time to UTC

time.mktime(time.localtime(calendar.timegm(utc_time)))

http://feihonghsu.blogspot.com/2008/02/converting-from-local-time-to-utc.html

If converting a struct_time to seconds-since-the-epoch is done using mktime, this conversion is in local timezone. There's no way to tell it to use any specific timezone, not even just UTC. The standard 'time' package always assumes that a time is in your local timezone.

like image 28
user193287 Avatar answered Sep 25 '22 19:09

user193287


I think you can use the utcoffset() method:

utc_time = datetime1 - datetime1.utcoffset()

The docs give an example of this using the astimezone() method here.

Additionally, if you're going to be dealing with timezones, you might want to look into the PyTZ library which has lots of helpful tools for converting datetime's into various timezones (including between EST and UTC)

With PyTZ:

from datetime import datetime
import pytz

utc = pytz.utc
eastern = pytz.timezone('US/Eastern')

# Using datetime1 from the question
datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S")

# First, tell Python what timezone that string was in (you said Eastern)
eastern_time = eastern.localize(datetime1)

# Then convert it from Eastern to UTC
utc_time = eastern_time.astimezone(utc)
like image 43
JJ Geewax Avatar answered Sep 22 '22 19:09

JJ Geewax