Python Time processing
# _*_ coding: utf-8 _*_import timeimport calendarimport datetime# Three Time forms Print ("Time stamp:", time.time ()) in the Times module # timestamp print ("Local time:", time.localtime ()) # struct_time type of local time print ("Utc time:", Time.gmtime ()) # struct_time type of UTC time # time module, conversion between three time forms Time_stamp = time.time () # timestamp local_time = time.localtime (time_stamp) # timestamp to struct_time type local time Utc_time = time.gmtime (time_stamp) # time stamp to Struct_time type UTC time Time_stamp_1 = time.mktime (Local_time) # struct_time local time-to-time stamp of type time_stamp_2 =  CALENDAR.TIMEGM (utc_time) # struct_ The time-type UTC timestamp of the print (time_stamp, time_stamp_1, time_stamp_2) # time module, the conversion between three time forms and the string print ( Time.ctime (time_stamp)) # timestamp to String ( local time string) print (Time.asctime (local_time)) # struct_ local time-to-string print of type Time.asctime (utc_time) # struct_time type of UTC time to string print (Time.strftime ("%y-%m-%d, %h:%m:%s, %w", local_time)) # struct_time type local time-to-string: Custom Format print (Time.strftime ("%y-%m-%d, %h:%m:%s, %w ", utc_time)) &Nbsp; # struct_time type of UTC time-to-string: custom Format Struct_time = time.strptime (" 2016-11-15, 15:32:12, 2 ", "%y-%m-%d, %h:%m:%s, %w ") # string to Struct_time type # datetime the usage of the DateTime class in the module a_datetime_local = Datetime.datetime.now () # get local time for Datetime.datetime type a_datetime_utc = Datetime.datetime.utcnow () # get the Datetime.datetime type of UTC time Print (A_datetime_local.strftime ("% Y-%m-%d, %h:%m:%s, %w ")) # datetime.datetime type to string print (A_ Datetime_utc.strftime ("%y-%m-%d, %h:%m:%s, %w")) # datetime.datetime type to String a_datetime = datetime.datetime.strptime ("2016-11-15, 15:32:12, 2", "%y-%m-%d, %h:%m:%s, %w" ) # convert string to datetime.datetime format # datetime.datetime class and timestamp, conversion between struct_time types Time_ Stamp = a_datetime_local.timestamp () # DateTime type turn timestamp print (time_stamp) A_datetime_local = datetime.datetime.fromtimestamp (Time.time ()) # timestamp to datetime.datetime type local time a_datetime_utc = Datetime.datetime.utcfromtimestamp (Time.time ()) # Timestamp to Datetime.datetime type UTC time Print (A_DATETIME_LOCAL, A_DATETIME_UTC) print (A_datetime_local.timetuple ()) # DateTime type goto struct_time type print (A_datetime_utc.utctimetuple ()) &nbsP; # datetime Type Turn struct_ Time Type
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1881909
Python Time processing