Python provides a variety of processing methods for time and date, mainly in the two modules of the Times and DateTime. Today, a little comb the two modules in the use of some of the differences and links.
Time
In the Python documentation, time is categorized in generic Operating system services, in other words, it provides functionality that is closer to the operating system level. Read through the documentation, the time module is built around Unix Timestamp.
The module mainly consists of a class struct_time, other functions and related constants. It is important to note that most functions in this module call the function of the same name on the Platform C library, so pay special attention to some functions that are platform-related and may have different effects on different platforms. Another point is that because it is based on Unix Timestamp, the date range that it can express is limited to 1970-2038, and if you write code that needs to process dates outside of the previously mentioned range, you might want to consider using a datetime module better. The document explanation is laborious, the concrete look how uses:
In [1]: Import Timein [2]: Time.time () out[2]: 1414332433.345712In [3]: timestamp = Time.time () in [4]: Time.gmtime (Timestam p) out[4]: Time.struct_time (tm_year=2014, tm_mon=10, tm_mday=26, tm_hour=14, tm_min=7, tm_sec=13, tm_wday=6, tm_yday= 299, tm_isdst=0) in [5]: Time.localtime (timestamp) out[5]: Time.struct_time (tm_year=2014, tm_mon=10, tm_mday=26, tm_ Hour=22, Tm_min=7, tm_sec=13, tm_wday=6, tm_yday=299, tm_isdst=0) in [6]: Struct_time = time.localtime (timestamp) in [7]: t Ime.ctime (timestamp) out[7]: ' Sun Oct 22:07:13 ' in [8]: Time.asctime (struct_time) out[8]: ' Sun Oct 22:07:13 ' I n [9]: Time.mktime (struct_time) out[9]: 1414332433.0In [ten]: Time.strftime ("%a,%d%b%Y%h:%m:%s +0000", Struct_time) out[ []: ' Sun, 22:07:13 +0000 ' in [All]: Time.strptime ("Nov", "%d%b%y") out[11]: Time.struct_time (tm_year=200 0, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, Tm_isdst=-1)
The problem is small, and you may sometimes need to take note of the time zone used.
Datetime
DateTime is a bit more advanced than time, and it can be understood that datetime is encapsulated based on time and provides more useful functions. Several classes are included in the DateTime module, with the following relationships:
Object
- Timedelta # primarily used to calculate time spans
- Tzinfo # time Zone related
- Time # only focuses on times
- Date # focus on dates only
- DateTime # has both time and date
Name comparison around the mouth, in practical practical, used more is datetime.datetime and Datetime.timedelta, the other two datetime.date and datetime.time actual use and There is no big difference between datetime.datetime. The following mainly talk about the use of datetime.datetime. Use Datetime.datetime.now () to get an datetime.datetime instance of the current moment. For a datetime.datetime instance, there will be the following properties and common methods, see the name can be understood, there should be no big problem:
- Datetime.year
- DateTime.Month
- DateTime.Day
- Datetime.hour
- Datetime.minute
- Datetime.second
- Datetime.microsecond
- Datetime.tzinfo
Datetime.date () # Returns the Date Object Datetime.time () # Returns the Time Object Datetime.replace (Name=value) # Before the individual properties are read-only, this method is required to change the date Time.timetuple () # returns Time.struct_time object Dattime.strftime (format) # formatted output according to format
...
In addition to the methods that the instance itself has, the class itself provides a number of useful methods:
- Datetime.today () A # current time, localtime
- DateTime.Now ([TZ]) # Current time default localtime
- Datetime.utcnow () # UTC time
- Datetime.fromtimestamp (timestamp[, TZ]) # Build objects from Unix timestamp
- Datetime.strptime (date_string, format) # Parsing a string for a given time format
...
Note that there are many time zone-related functions omitted above, so please check the documentation if you need to use them. For date calculations, using Timedelta is also relatively straightforward:
In [1]: Import Datetimein [2]: Time_now = Datetime.datetime.now () in [3]: Time_nowout[3]: Datetime.datetime (2014, 10, 27, 2 1, 657523, +, +) in [4]: Delta1 = Datetime.timedelta (hours=25) in [5]: Print (Time_now + delta1) 2014-10-28 22:46:16.657523i n [6]: Print (time_now-delta1) 2014-10-26 20:46:16.657523
Even two DateTime objects are directly subtracted to get a Timedelta object. If there is a need to calculate working days, you can use the Business_calendar library, which does not require additional dependencies.