In Python, the difference between the time module and the datetime module is that pythondatetime

Source: Internet
Author: User
Tags timedelta

In Python, the difference between the time module and the datetime module is that pythondatetime

Python provides a variety of processing methods for time and date, mainly in the time and datetime modules. Today, let's take a look at the differences and connections between the two modules.

Time
In the Python document, time is classified in Generic Operating System Services. In other words, time provides functions that are closer to the Operating System. The read-through documentation shows that the time module is centered around Unix Timestamp.

This module mainly includes a class struct_time, and several other functions and related constants. Note that most functions in this module call functions of the same name in the C library of the platform. Therefore, note that some functions are platform-related, it may have different effects on different platforms. Another point is that because it is based on Unix Timestamp, the range of dates that can be expressed is limited to 1970-2038. If the code you write needs to process a date out of the range described above, it may be better to use the datetime module. It is difficult to explain the document. Let's take a look at how to use it:

In [1]: import timeIn [2]: time.time()Out[2]: 1414332433.345712In [3]: timestamp = time.time()In [4]: time.gmtime(timestamp)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]: time.ctime(timestamp)Out[7]: 'Sun Oct 26 22:07:13 2014'In [8]: time.asctime(struct_time)Out[8]: 'Sun Oct 26 22:07:13 2014'In [9]: time.mktime(struct_time)Out[9]: 1414332433.0In [10]: time.strftime("%a, %d %b %Y %H:%M:%S +0000", struct_time)Out[10]: 'Sun, 26 Oct 2014 22:07:13 +0000'In [11]: time.strptime("30 Nov 00", "%d %b %y")Out[11]: time.struct_time(tm_year=2000, 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 not serious. You may need to pay attention to the time zone.

Datetime
Datetime is much more advanced than time. It can be understood that datetime is encapsulated based on time and provides more practical functions. The datetime module contains several classes. The specific relationships are as follows:

Object

  • Timedelta # mainly used for calculating the time span
  • Tzinfo # Time Zone
  • Time # focus only on time
  • Date # only focus on dates
  • Datetime # both time and date are available

The name is a detour. in practical use, datetime is used. datetime and datetime. timedelta, the other two datetime. date and datetime. actual use of time and datetime. datetime does not have much difference. The following describes how to use datetime. datetime. You can use datetime. datetime. now () to obtain the datetime. datetime instance of the current time. For a datetime. datetime instance, there are mainly the following attributes and common methods, which can be understood by name. There should be no major problem:

  • Datetime. year
  • Datetime. month
  • Datetime. day
  • Datetime. hour
  • Datetime. minute
  • Datetime. second
  • Datetime. microsecond
  • Datetime. tzinfo
Datetime. date () # Return date object datetime. time () # return time object datetime. replace (name = value) # The attributes described above are read-only. You need this method to change datetime. timetuple () # return time. struct_time object dattime. strftime (format) # format the output according to the format

...
In addition to the methods of the Instance itself, the class also provides a lot of useful methods:

  • Datetime. today () a # current time, localtime
  • Datetime. now ([tz]) # default localtime
  • Datetime. utcnow () # UTC time
  • Datetime. fromtimestamp (timestamp [, tz]) # Build an object by Unix Timestamp
  • Datetime. strptime (date_string, format) # parses strings in a given time format

...

Note that many time zone-related functions are omitted above. For more information, see. For date calculation, it is relatively simple to use timedelta:

In [1]: import datetimeIn [2]: time_now = datetime.datetime.now()In [3]: time_nowOut[3]: datetime.datetime(2014, 10, 27, 21, 46, 16, 657523)In [4]: delta1 = datetime.timedelta(hours=25)In [5]: print(time_now + delta1)2014-10-28 22:46:16.657523In [6]: print(time_now - delta1)2014-10-26 20:46:16.657523

Even two datetime objects can be directly subtracted to obtain a timedelta object. If you need to calculate the business day, you can use the business_calendar library, which can be used without additional dependencies.

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.