Python date and time
Python programs can handle dates and times in many ways, and converting date formats is a common function.
Python provides a time and calendar modules that can be used to format dates and times.
The time interval is a floating point decimal in seconds.
Each timestamp is expressed as how much time has passed since midnight (epoch) on January 1, 1970.
Python’s time module has many functions that can convert common date formats. For example, the function time.time() is used to obtain the current timestamp, as shown in the following example:
# -*- coding: UTF-8 -*-
import time; #Introduce time module
ticks = time.time()
print "The current timestamp is:", ticks
The above example output result:
The timestamp unit is most suitable for date operations. But dates before 1970 cannot be expressed in this way. Dates that are too far away won't work either, UNIX and Windows are only supported until 2038.
What is a time tuple?
Many Python functions use one element to assemble 9 groups of numbers. Processing time:
Serial number | Field | Value |
---|---|---|
0 | 4 digit year | 2008 |
1 | Month | 1 to 12 |
Day | 1 to 31 | |
Hours | 0 to 23 | |
Minutes | 0 to 59 | |
Seconds | 0 to 61 (60 or 61 is a leap second) | |
The day of the week | 0 to 6 (0 is Monday) | |
The day of the year | 1 to 366 (Julian calendar) | |
Daylight Saving Time | -1, 0, 1, -1 is used to determine whether The flag for daylight saving time |
Attribute | Value | |
---|---|---|
tm_year | 2008 | |
tm_mon | 1 to 12 | |
tm_mday | 1 to 31 | |
tm_hour | 0 to 23 | |
tm_min | 0 to 59 | |
tm_sec | 0 to 61 (60 or 61 is a leap second) | |
tm_wday | 0 to 6 (0 is Monday ) | |
tm_yday | 1 to 366 (Julian calendar) | |
tm_isdst | -1, 0, 1, -1 is the flag to determine whether it is daylight saving time |
Serial number | Function and description |
---|---|
1 | time.altzone Returns the offset of the daylight saving time area west of Greenwich Number of seconds. Negative values are returned if the area is east of Greenwich (such as Western Europe, including the UK). Available only in regions where daylight saving time is enabled. |
2 | time.asctime([tupletime]) Accepts a time tuple and returns a readable form of "Tue Dec 11 18:07: 14 2008" (Tuesday, December 11, 2008 18:07:14) is a 24-character string. |
3 | time.clock( ) Returns the current CPU time in seconds calculated as a floating point number. It is used to measure the time consumption of different programs and is more useful than time.time(). |
4 | time.ctime([secs]) The function is equivalent to asctime(localtime(secs)). No parameters are given, which is equivalent to asctime() |
5 | time.gmtime([secs]) Receives the timeout (the number of floating point seconds elapsed since the 1970 epoch) and returns Greenwich astronomical time The time tuple under t. Note: t.tm_isdst is always 0 |
6 | time.localtime([secs]) Receive time (floating point seconds elapsed after 1970 epoch number) and returns the time tuple t in local time (t.tm_isdst can be 0 or 1, depending on whether the local time is daylight saving time). |
7 | time.mktime(tupletime) Accepts a time tuple and returns the timeout (the number of floating point seconds elapsed since epoch 1970). |
8 | time.sleep(secs) Delay the running of the calling thread, secs refers to the number of seconds. |
9 | time.strftime(fmt[,tupletime]) Receives a time tuple and returns the local time represented as a readable string, The format is determined by fmt. |
10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') According to The format of fmt parses a time string into a time tuple. |
11 | time.time( ) Returns the timestamp of the current time (the number of floating-point seconds elapsed since epoch 1970). |
12 | time.tzset() Reinitialize time-related settings according to the environment variable TZ. |
The Time module contains the following two very important attributes:
Serial Number | Attribute And description |
---|---|
1 | time.timezone The property time.timezone is the local time zone (without daylight saving time) distance from Greenwich Offset seconds (>0, Americas; <=0 most of Europe, Asia, Africa). |
2 | time.tzname The attribute time.tzname contains a pair of strings that vary depending on the situation, namely The local time zone name with and without daylight saving time. |
Calendar module
The functions of this module are all calendar-related, such as printing the character calendar of a certain month.
Monday is the default first day of the week, and Sunday is the default last day. To change the settings, you need to call the calendar.setfirstweekday() function. The module contains the following built-in functions:
Serial number | Function and description |
---|---|
1 | calendar.calendar(year,w=2,l=1,c=6) Returns a year calendar in multi-line string format, with 3 months per line, and the interval is c. Daily width interval is w characters. The length of each line is 21* W+18+2* C. l is the number of lines per week. |
2 | calendar.firstweekday( ) Returns the setting of the current weekly starting day. By default, 0 is returned when the caendar module is first loaded, which is Monday. |
3 | calendar.isleap(year) returns True if it is a leap year, otherwise false. |
4 | calendar.leapdays(y1,y2) Returns the total number of leap years between Y1 and Y2. |
5 | calendar.month(year,month,w=2,l=1) Returns a multi-line string The calendar format is year and month, with two rows of titles and one row for each week. Daily width interval is w characters. The length of each line is 7* w+6. l is the number of lines per week. |
6 | calendar.monthcalendar(year,month) Returns a single-level nested list of integers. Each sublist holds an integer representing a week. Dates outside Year, month, and month are all set to 0; days within the range are represented by the day of the month, starting from 1. |
7 | calendar.monthrange(year,month) Returns two integers. The first is the date code of the day of the week of the month, and the second is the day code of the month. Days range from 0 (Monday) to 6 (Sunday); months range from 1 to 12. |
8 | calendar.prcal(year,w=2,l=1,c=6) is equivalent to print calendar .calendar(year,w,l,c). |
9 | ##calendar.prmonth(year,month,w=2,l=1) Equivalent to print calendar.calendar(year, w, l, c). |
calendar.setfirstweekday(weekday)Set the starting day code of each week. 0 (Monday) to 6 (Sunday). | |
calendar.timegm(tupletime)The opposite of time.gmtime: accepts a time tuple form and returns the time The time of day (the number of floating point seconds elapsed since the 1970 epoch). | |
calendar.weekday(year,month,day)Returns the date code of the given date. 0 (Monday) to 6 (Sunday). The months are 1 (January) to 12 (December). |
Other related modules and functionsIn Python, other modules for processing dates and times include:
- datetime module
- pytz module
- dateutil module