UTC time conversion for Python

Source: Internet
Author: User

UTC time conversion, and the final result is UTC time.

In simple terms:

Time stamp (timestamp) conversion, UTC display Time (datetime), using Time.gmtime (timestamp).

Displays the time (datetime) conversion, UTC timestamp (timestamp), using CALENDAR.TIMEGM (Datetime.timetuple ()).

Attention:

VC under the corresponding interface is gmtime and _mkgmtime.

Code:

#-*-coding:gb2312-*-# UTC time conversion, resulting in UTC time: # timestamp (timestamp) conversion, UTC display Time (datetime) # Display time (datetime) conversion, UTC Time stamp (timestamp) # UTC, coordinated world time, also known as the world of unification, can be considered time zone of 0. Import timeimport Datetimeimport calendaradatetime = Datetime.datetime (1970, 1, 1, 0, 0, 1) atimestamp = # get time zone offset print "Ti Me.timezone: ", time.timezone# gets the display time (DateTime) based on the custom time. Print "datetime:", Adatetimeprint "Timetuple:", Adatetime.timetuple () print "Time.strptime:", Time.strptime ("1970-1-1 0:1:1 ","%y-%m-%d%h:%m:%s ") # Gets the UTC display Time (datetime) based on the timestamp (timestamp). That is: Timestamp (timestamp) conversion, display time (datetime). Print "Time.gmtime:timestamp (%s)->datetime (%s)"% (Atimestamp, Time.gmtime (atimestamp)) print " Datetime.datetime.utcfromtimestamp:timestamp (%s)->datetime (%s) "% (Atimestamp, Datetime.datetime.utcfromtimestamp (Atimestamp) # gets the UTC timestamp (timestamp) based on the display Time (datetime). That is: Display time (datetime) Conversion-time stamp (timestamp). Print "Calendar.timegm:datetime (%s)->timestamp (%s)"% (Adatetime.timetuple (), CALENDAR.TIMEGM ( Adatetime.timetuple())) dt = Time.gmtime (atimestamp-time.timezone) # Time.mktime conversion time is with time zone, so you need to subtract time zone slack print "Time.mktime:datetime (%s)- >timestamp (%s) "% (DT, time.mktime (DT))

Output:

Time.timezone:  -28800datetime:  1970-01-01 00:00:01timetuple:  time.struct_time (tm_year=1970, Tm_mon=1 , Tm_mday=1, tm_hour=0, tm_min=0, Tm_sec=1, tm_wday=3, Tm_yday=1, Tm_isdst=-1) Time.strptime:  time.struct_time (tm_ year=1970, Tm_mon=1, Tm_mday=1, tm_hour=0, Tm_min=1, Tm_sec=1, tm_wday=3, Tm_yday=1, Tm_isdst=-1) time.gmtime:timestamp (1)->datetime (Time.struct_time (tm_year=1970, Tm_mon=1, Tm_mday=1, tm_hour=0, tm_min=0, Tm_sec=1, tm_wday=3, tm_ Yday=1, tm_isdst=0)) Datetime.datetime.utcfromtimestamp:timestamp (1)->datetime (1970-01-01-00:00:01) Calendar.timegm:datetime (Time.struct_time (tm_year=1970, Tm_mon=1, Tm_mday=1, tm_hour=0, tm_min=0, Tm_sec=1, tm_wday= 3, Tm_yday=1, Tm_isdst=-1))->timestamp (1) time.mktime:datetime (Time.struct_time (tm_year=1970, Tm_mon=1, tm_mday= 1, Tm_hour=8, tm_min=0, Tm_sec=1, tm_wday=3, Tm_yday=1, tm_isdst=0))->timestamp (1.0)


UTC time conversion for Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.