Python Learning Note 9: Standard library DateTime (Time packet, datetime package)

Source: Internet
Author: User
Tags date1 timedelta

One time Package
Sleep ([float time]) delays a number of seconds in floating-point numbers


The time package is based on the C language library function (functions).
Python's interpreter is generally written in C, and some Python functions also call the C-language library function directly.


Time.time () Get current time, return float type, Unit: sec
Time.clock () on UNIX systems, it returns "process time", which is a floating-point number (timestamp) in seconds.


In Windows, the first call returns the actual time that the process was executed. The second subsequent call is the execution time from the first call to the current one.


Time.gmtime () returns UTC time in Struct_time format
Time.localtime ([float time]) The number of references is a UTC and returns the Time.struct_time type
Returns the local time in the Struct_time format, depending on the system environment, when no participation is taken
Time.mktime ([float time]) converts a timestamp into the struct_time of a UTC time zone (0 o'clock Zone).
Assuming the seconds parameter is not entered, the current time is the conversion criterion


Two datetime packages
DateTime can be understood as both a date and a time component.


Date refers to the day (equivalent to the calendar) of the month and date, which is the detailed time (equivalent to a watch) in 24 hours of the day, minutes and seconds.
You can manage these two separately (The Datetime.date class. Datetime.time Class), can also be combined (Datetime.datetime Class).


1 Date Class
Class Properties:
A datetime module defines two constants: DateTime. Minyear and Datetime.maxyear, respectively, representing the minimum and maximum year that datetime can represent.
Of Minyear = 1,maxyear = 9999.


Datetime.date: A class that represents a date. Frequently used properties are year[minyear, Maxyear], month[1,12], day[1,31];
Datetime.time: A class that represents time.

Frequently used properties are hour, minute, second, microsecond;
Datetime.datetime: Represents the date and time.


Datetime.timedelta: Represents a time interval, which is the length between two points in time.




Instance properties:
Date.year, Date.month, Date.day: Years, months, and days;
Date.replace (year, Month, day): Generates a new Date object, Replaces the attributes in the original object with the year, month, and day specified by the number of references.
Date.timetuple (): Returns the corresponding Time.struct_time object for the date.
Date.toordinal (): Returns the Gregorian calendar date corresponding to the date;
Date.weekday (): Returns weekday. The assumption is Monday, which returns 0; the assumption is Week 2. Returns 1, and so on;
Data.isoweekday (): Returns weekday, assuming Monday. return 1; Suppose it is Week 2. Returns 2, and so on.
Date.isocalendar (): Returns the format as (Year,month. A tuple of day)
Date.isoformat (): Returns a string formatted as ' YYYY-MM-DD '.
Date.strftime (FMT): Defines the formatted string itself.


Overloaded operation:
Date2 = date1 + timedelta  # date plus an interval that returns a new Date object (Timedelta will be described below, representing the time interval)
Date2 = date1- Timedelta   # date interval, returns a new Date object
Timedelta = date1-date2   # Two date subtraction, returns a time interval object
Date1 < Date2  # Two Date to compare the


2 Time Class
Class Property:
Time.min, the Time.max:time class can represent the minimum and maximum times.

Of Time.min = time (0, 0, 0, 0). Time.max = Time (23, 59, 59, 999999);
Time.resolution: The smallest unit of time, usually 1 microseconds;
Instance properties:
Time.hour, Time.minute, Time.second, Time.microsecond: Hours, minutes, seconds, microseconds.
Time.tzinfo: time zone information;
Time.replace ([hour[, minute[, second[, microsecond[, Tzinfo]]]): Creates a new Time object. Replaces the attribute in the original object with the time, minute, second, and microsecond specified by the parameter (the original object remains unchanged);
Time.isoformat (): A string representation of the return type, such as "HH:MM:SS" format.
Time.strftime (FMT): Returns its own definition of a formatted string.


3 datetime class
DateTime is a combination of date and time, containing all the information of date and time. Its constructor is for example the following:
Datetime.datetime (year, month, day, hour, minute, second, microsecond, tzinfo), the meaning of each parameter is the same as in the constructor for date, time.




Class Properties:
The minimum and maximum values that can be expressed by datetime.min and Datetime.max:datetime;
Datetime.resolution:datetime the smallest unit.
Datetime.today (): Returns a DateTime object representing the current local time;
DateTime.Now ([TZ]): Returns a DateTime object that represents the current local time, assuming that the parameter TZ is provided. Gets the local time of the time zone referred to by the TZ parameter;
Datetime.utcnow (): Returns a DateTime object for the current UTC time;
Datetime.fromtimestamp (timestamp[, TZ]): Creates a DateTime object based on the time timestamp. Parameter TZ Specifies the time zone information;
Datetime.utcfromtimestamp (timestamp): Creates a DateTime object based on the time timestamp.
Datetime.combine (date, time): Creates a DateTime object based on date and time;
Datetime.strptime (date_string, format): Converts a format string to a DateTime object;
Instance properties:
Datetime.year, month, day, hour, minute, second, microsecond, Tzinfo:
Datetime.date (): Gets the Date object.
Datetime.time (): Gets the time object.
Datetime.replace ([year[, month[, day[, hour[, minute[, second[, microsecond[, Tzinfo] []]] []]):
Datetime.timetuple ()
Datetime.utctimetuple ()
Datetime.toordinal ()
Datetime.weekday ()
Datetime.isocalendar ()
Datetime.isoformat ([Sep])
Datetime.ctime (): Returns a C-format string of datetime equivalent to Time.ctime (Time.mktime (Dt.timetuple ()));
Datetime. Strftime (format)
Like date, it is also possible to compare two DateTime objects, or subtract to return a time interval object. Or a date time plus an interval to return a new DateTime object.






Three-format string
datetime, date, and time all provide the strftime () method, which receives a format string that outputs a string representation of the DateTime.


Here is the meaning of the format character:


%a: Shorthand for the week. As Wednesday for web
%A: Full write of the week. As in Wednesday for Wednesday
%b: Shorthand for the month.

If April is Apr
%B: Full write of the month.

As in April for April
%c: string representation of DateTime. (Example: 04/07/10 10:43:39)
%d: The number of days in the month (the day of the week)
%f: microseconds (range [0,999999])
%H: Hours (24-hour, [0, 23])
%I: Hour (12-hour). [0, 11])
%j: Number of days in the year [001,366] (the day of the current year)
%m: Month ([01,12])
%M: Minutes ([00,59])
%p:am or PM
%s: Seconds (the range is [00,61]. Why not [00, 59], reference Python manual ~_~)
%u: Week in the Year of the Week of the year), Sunday as the first day of the week
%w: Today in this week's days, the range is [0, 6],6 represents Sunday
%W: Week of the Year (the Week of the year), Monday as the first day of the week
%x: Date string (for example: 04/07/10)
%x: Time string (for example: 10:43:39)
%y:2 the year represented by a number
%y:4 the year represented by a number
%z: The interval from UTC time (assumed to be local time. Returns an empty string)
%Z: Time zone name (assuming local time, returns an empty string)
Percent:%


There is a lot of content and no code is available.

Python Learning Note 9: Standard library DateTime (Time packet, datetime package)

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.