Datetime Utc Python

broken image


  1. Datetime Format Python
  2. Convert Datetime To Utc Python
  3. Datetime Python Utc Timestamp
  4. Get Utc Time Python
  5. Datetime Python Utc String

How do I convert a datetime string in local time to a string in UTC time?

I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future.

Note that for Python 3.2 onwards, the datetime module contains datetime.timezone. The documentation for datetime.utcnow says: An aware current UTC datetime can. The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation. Aware and Naive Objects ¶. Datetime.utcfromtimestamp (timestamp) returns a naive datetime object (not an aware one). Aware ones are timezone aware, and naive are not. You want an aware one if you want to convert between timezones (e.g. Between UTC and local time). If you have a naive datetime representing UTC, use datetime.replace(tzinfo=timezone.utc) to make it aware, at which point you can use datetime.timetuple. Toordinal ( ) ¶ Return the proleptic Gregorian ordinal of the date.

Clarification: For example, if I have 2008-09-17 14:02:00 in my local timezone (+10), I'd like to generate a string with the equivalent UTC time: 2008-09-17 04:02:00.

Also, from http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/, note that in general this isn't possible as with DST and other issues there is no unique conversion from local time to UTC time.

Answers:

First, parse the string into a naive datetime object. This is an instance of datetime.datetime with no attached timezone information. See documentation for datetime.strptime for information on parsing the date string.

Use the pytz module, which comes with a full list of time zones + UTC. Figure out what the local timezone is, construct a timezone object from it, and manipulate and attach it to the naive datetime.

Finally, use datetime.astimezone() method to convert the datetime to UTC.

Source code, using local timezone 'America/Los_Angeles', for the string '2001-2-3 10:11:12':

From there, you can use the strftime() method to format the UTC datetime as needed:

Answers:

The datetime module's utcnow() function can be used to obtain the current UTC time.

As the link mentioned above by Tom: http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/ says:

UTC is a timezone without daylight saving time and still a timezone
without configuration changes in the past.

Always measure and store time in UTC.

If you need to record where the time was taken, store that separately.
Do not store the local time + timezone information!

NOTE – If any of your data is in a region that uses DST, use pytz and take a look at John Millikin's answer.

If you want to obtain the UTC time from a given string and your lucky enough to be in a region in the world that either doesn't use DST, or you have data that is only offset from UTC without DST applied:

–> using local time as the basis for the offset value:

–> Or, from a known offset, using datetime.timedelta():

Answers:

Thanks @rofly, the full conversion from string to string is as follows:

My summary of the time/calendar functions:

time.strptime
string –> tuple (no timezone applied, so matches string)

time.mktime
local time tuple –> seconds since epoch (always local time)

time.gmtime
seconds since epoch –> tuple in UTC

and

calendar.timegm
tuple in UTC –> seconds since epoch

time.localtime
seconds since epoch –> tuple in local timezone

Datetime Format Python

Answers:

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

Example usage from bd808: If your source is a datetime.datetime object t, call as:

Answers:

Convert Datetime To Utc Python

Here's a summary of common Python time conversions

  • struct_time (UTC) → POSIX:
    calendar.timegm(struct_time)
  • Naïve datetime (local) → POSIX:
    calendar.timegm(stz.localize(dt, is_dst=None).utctimetuple())
  • Naïve datetime (UTC) → POSIX:
    calendar.timegm(dt.utctimetuple())
  • Aware datetime → POSIX:
    calendar.timegm(dt.utctimetuple())
  • POSIX → struct_time (UTC):
    time.gmtime(t)
  • Naïve datetime (local) → struct_time (UTC):
    stz.localize(dt, is_dst=None).utctimetuple()
  • Naïve datetime (UTC) → struct_time (UTC):
    dt.utctimetuple()
  • Aware datetime → struct_time (UTC):
    dt.utctimetuple()
  • POSIX → Naïve datetime (local):
    datetime.fromtimestamp(t, None)
  • struct_time (UTC) → Naïve datetime (local):
    datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
  • Naïve datetime (UTC) → Naïve datetime (local):
    dt.replace(tzinfo=UTC).astimezone(tz).replace(tzinfo=None)
  • Aware datetime → Naïve datetime (local):
    dt.astimezone(tz).replace(tzinfo=None)
  • POSIX → Naïve datetime (UTC):
    datetime.utcfromtimestamp(t)
  • struct_time (UTC) → Naïve datetime (UTC):
    datetime.datetime(struct_time[:6])
  • Naïve datetime (local) → Naïve datetime (UTC):
    stz.localize(dt, is_dst=None).astimezone(UTC).replace(tzinfo=None)
  • Aware datetime → Naïve datetime (UTC):
    dt.astimezone(UTC).replace(tzinfo=None)
  • POSIX → Aware datetime:
    datetime.fromtimestamp(t, tz)
  • struct_time (UTC) → Aware datetime:
    datetime.datetime(struct_time[:6], tzinfo=UTC).astimezone(tz)
  • Naïve datetime (local) → Aware datetime:
    stz.localize(dt, is_dst=None)
  • Naïve datetime (UTC) → Aware datetime:
    dt.replace(tzinfo=UTC)

Source: taaviburns.ca

Answers:

One more example with pytz, but includes localize(), which saved my day.

Answers:

I'm having good luck with dateutil (which is widely recommended on SO for other related questions):

(Code was derived from this answer to Convert UTC datetime string to local datetime)

Python
Answers:
Questions:
Answers:
Questions:

You can do it with:

Datetime Utc Python
Answers:
Questions:
Answers:
Questions:

You can do it with:

Answers:

How about –

if seconds is None then it converts the local time to UTC time else converts the passed in time to UTC.

Answers:

Datetime Python Utc Timestamp

For getting around day-light saving, etc.

None of the above answers particularly helped me. The code below works for GMT.

Answers:

Get Utc Time Python

Using http://crsmithdev.com/arrow/

This library makes life easy 🙂

Datetime Python Utc String

Answers:

Tags: laravelpython, time





broken image