In Python, there are three processing libraries for time, dates, and calendar, in which DateTime has datetime.date, Datetime.time, and datetime.datetime three classes. Time can be divided into time stamp, local time and UTC time (World Standard Time). Does that sound a little messy? So what's the difference between them? What's the connection? And how to convert it?
Time Module
In the time module, there are three types of representations:
A timestamp, typically a Unix timestamp, that is the number of seconds from 1970 onwards to the present.
local time Struct_time form: a named tuple of length 11, the first is the year, the second is the month ....
Struct_time form of UTC time: A named tuple of length 11, similar to the previous one, except for UTC time
The latter two are of the same type, the difference being that one is local time (localtime) and one is UTC time.
Various time forms of access:
Print ("Time stamp:", Time.time ()) # timestamp: 1479193181.829338
print ("local time:", Time.localtime ()) # Struct_time type of local time
time.struct_time (tm_year=2016, tm_mon=11, tm_mday=15, tm_hour=14, tm_min=59, tm_sec=41, tm_ Wday=1, tm_yday=320, tm_isdst=0)
print ("UTC time:", Time.gmtime ()) # struct_time type UTC times
time.struct_ Time (tm_year=2016, tm_mon=11, tm_mday=15, tm_hour=6, tm_min=59, tm_sec=41, Tm_wday=1, tm_yday=320, tm_isdst=0)
As can be seen here, local time (Beijing time) and UTC time Difference 8 hours.
Conversions in various time forms:
Time_stamp = Time.time () # timestamp
local_time = Time.localtime (time_stamp) # timestamp to Struct_time local time of type
utc_ Time = Time.gmtime (time_stamp) # Timestamp to Struct_time type of UTC time
time_stamp_1 = Time.mktime (local_time) # struct_ Time type local timestamp
time_stamp_2 = Calendar.timegm (utc_time) # struct_time type UTC timestamp
print (Time_stamp, time _stamp_1, time_stamp_2)
Conversions between various time forms and strings:
Print (Time.ctime (time_stamp)) # Timestamp spin string (local time string)
print (Time.asctime (local_time)) # struct_ Time type local times string
print (Time.asctime (utc_time)) # struct_time type UTC time String
# struct_ local time-by-type string: Custom format
print (Time.strftime ("%y-%m-%d,%h:%m:%s,%w", Local_time))
# struct_ Time-type UTC Timing string: Custom format
print (Time.strftime ("%y-%m-%d,%h:%m:%s,%w", utc_time))
Struct_time = Time.strptime ("2016-11-15, 15:32:12, 2", "%y-%m-%d,%h:%m:%s,%w") # string Turn struct_time type
DateTime module
Next look at the DateTime module. The module contains 4 main classes:
Datetime.time: Time class, contains only time, minutes, seconds, microseconds and other information.
Datetime.date: Date class, containing only date information, such as year, month, day, week, and so on.
Datetime.datetime: Date Time class that contains all the information for both.
Datetime.timedelta: Time-date Difference value class, used to represent the difference between two DateTime.
The basic usage of each class is relatively simple, suggest to see Official Document: DateTime module
This is mainly about the use of the Datetime.datetime class, the other two uses similar, only a slight difference:
a_datetime_local = Datetime.datetime.now () # Gets the local time of the datetime.datetime type
A_DATETIME_UTC = Datetime.datetime.utcnow () # Get Datetime.datetime type UTC time
# datetime.datetime type spin string
print (a_datetime_ Local.strftime ("%y-%m-%d,%h:%m:%s,%w"))
# datetime.datetime type spin string
print (A_datetime_utc.strftime ("%y-%m- %d,%h:%m:%s,%w "))
A_datetime = Datetime.datetime.strptime (" 2016-11-15, 15:32:12, 2 ","%y-%m-%d,%h:%m:%s,%w ") # string Turn
Datetime.datetime format
Conversion of datetime type and timestamp, struct_time type
Between Datetime.datetime and timestamps, Struct_time can also be converted to each other:
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 of local time
A_DATETIME_UTC = Datetime.datetime.utcfromtimestamp (Time.time ()) # Timestamp to Datetime.datetime type of UTC time
print (a_datetime_local, A_DATETIME_UTC)
print (A_datetime_local.timetuple ()) # datetime type to Struct_time type
print (a_datetime_ Utc.utctimetuple ()) # datetime type Struct_time type
This article focuses on the basic usages of various types of time and date, as well as the ways in which they are converted, and how they interact with the strings.
The code has been uploaded to Github:https://github.com/xianhu/learnpython.
Site Backup Download Address: Http://xz.jb51.net:81/201611/yuanma/LearnPython (jb51.net). zip