Python time, DateTime, string, timestamp mutual conversion __python

Source: Internet
Author: User
Tags time zones local time
########################## # Python Time Conversion # ##########################
--------------------------A. The type of time representation--------------------------
The time that may be involved in PY is four types of 1. Time String 2. DateTime tuple (DateTime obj) 3. Time tuple (Time obj) 4. Timestamp
1. Time StringString is the simplest way to represent time, as the following code generates string---------------------------------------------->>> time.ctime () ' Mon Dec 21:02:55 '----------------------------------------------or simpler to generate a string------------------------- ---------------------time_string = ' 2000-01-02 03:04:05 '----------------------------------------------
2. DateTime tuple (DateTime obj)DateTime tuple is Datetime.datetime object type---------------------------------------------->>> DateTime.Now () Datetime.datetime (----------------------------------------------, 3, 139715) of the same
3. Time tuple (Time obj)Time tuple is a Time.struct_time object type----------------------------------------------time.struct_time (tm_year=2008, tm_m on=11, tm_mday=10, tm_hour=17, tm_min=53, tm_sec=59, tm_wday=0, tm_yday=315, Tm_isdst=-1)---------------------------- ------------------
4. TimestampTimestamp type: Number of seconds since January 1, 1970 (00:00:00 GMT)---------------------------------------------->>> time.time () 13 55749338.05917----------------------------------------------
--------------------------Two. Conversions between types--------------------------

1. String conversion to otherInitialization: Date_str = "2008-11-10 17:53:59" 1.1 String--> datetime obj import: Import datetime DATETIME.DATETIME.STR Ptime (string, format) eg---------------------------------------------->>> dt_obj = datetime.datetime.st Rptime (Date_str, "%y-%m-%d%h:%m:%s") >>> dt_obj Datetime.datetime (2008, A, A, q)-------- --------------------------------------
1.2 string--> time objImport: Import time Time.strptime (string, format) eg----------------------------------------------#time模块有类似 Strptime () function in datetime >>> DATE_STR = "2008-11-10 17:53:59" >>> t_obj = Time.strptime (Date_str, "%y-%m-%d%h:%m:%s") >>> t_obj time.struct_time (tm_year=2008, tm_mon=11, tm_mday=10, tm_hour=17, tm_min=5 3, tm_sec=59, tm_wday=0, tm_yday=315, Tm_isdst=-1)----------------------------------------------
2. DateTime obj converted to otherDateTime obj is converted to other types and is initialized with datetime functions: Dt_obj = Datetime.datetime (2008, all, all) 2.1 dt obj--> string ----------------------------------------------date_str = Dt_obj.strftime ("%y-%m-%d%h:%m:%s")-------------- --------------------------------2.2 dt obj--> time obj----------------------------------------------time_tu ple = Dt_obj.timetuple ()----------------------------------------------
3. Time obj converted to otherInitialization: Time_tuple = (2008, one, 2, 317, 0) 3.1 time obj--> string------------------------------- ---------------date_str = Time.strftime ("%y-%m-%d%h:%m:%s", time_tuple)---------------------------------------- ------3.2 Time obj--> datetime obj----------------------------------------------datetime.datetime (*time_tup Le[0:6])----------------------------------------------3.3 time obj--> timestamp---------------------------- ------------------ts = time.mktime (time_tuple)----------------------------------------------
4. Timestamp conversion to other Initialization:     timestamp = 1226527167.595983--!! -Note that the following two types use the local time zone 4.1 timestamp--> dt obj     --------------------------------------------- -    dt_obj = datetime.fromtimestamp (timestamp)     --------------- -------------------------------4.2 timestamp--> time obj     ------------------------------ ----------------    time_tuple = time.localtime (timestamp)     ---------- ------------------------------------ --!! --The following two ways are related to time zones, comparing standard time zone time and local time zone Time 4.3 using UTC--> DT obj     -------------------------------------- --------     #本地时区时间     >>> Datetime.datetime.fromtimestamp (tm)     datetime.datetime (401881, MB)      #标准时区时间   (in)    >>> Datetime.datetime.utcfromtimestamp (tm)    &nbsP;datetime.datetime     ---------------------------------------(in), (401881) -------4.4 using UTC--> Time obj     ----------------------------------------------      #本地时区时间     >>> Time.localtime (tm)     time.struct_ Time (tm_year=2012, tm_mon=12, tm_mday=17, tm_hour=23, tm_min=39, tm_sec=58, tm_wday=0, tm_yday=352, tm_isdst=0)  & nbsp;   #标准时区时间     >>> Time.gmtime (tm)     time.struct _time (tm_year=2012, tm_mon=12, tm_mday=17, tm_hour=15, tm_min=39, tm_sec=58, tm_wday=0, tm_yday=352, tm_isdst=0)
Related Article

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.