Time and DateTime Module Explanation
The time module in Python provides various functions for working with dates and times. This explanation covers the main formats, functions, and examples of using the time module.
Time Format Conversion
The time module provides three main formats for working with dates and times:
- Timestamp: A timestamp represents the number of seconds since January 1, 1970, 00:00:00. This format is useful for storing and comparing dates and times.
- Format Time: Format time is a human-readable representation of a date and time. This format includes a fixed format and custom formats.
- Struct Time: A struct time is a tuple of nine elements that represents a date and time. This format is useful for working with dates and times in Python.
Importing the Time Module
To use the time module, you need to import it at the beginning of your Python script:
import time
Time Tuple
A time tuple is a struct time that includes nine elements:
tm_year: The year (integer)tm_mon: The month (integer, 1-12)tm_mday: The day of the month (integer, 1-31)tm_hour: The hour (integer, 0-23)tm_min: The minute (integer, 0-59)tm_sec: The second (integer, 0-59)tm_wday: The day of the week (integer, 0-6, where 0 represents Sunday)tm_yday: The day of the year (integer, 1-366)tm_isdst: Whether it is daylight saving time (integer, -1, 0, or 1)
Time Common Functions
The time module provides several common functions for working with dates and times:
time.time(): Returns the current time as a timestamp.
>>> time.time()
1,506,421,280.1430917
time.localtime([secs]): Converts a timestamp to a struct time.
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=26, tm_hour=18, tm_min=14, tm_sec=51, tm_wday=1, tm_yday=269, tm_isdst=0)
time.mktime(t): Converts a struct time to a timestamp.
>>> time.mktime(time.localtime())
1506476811.0
time.sleep(secs): Postpones the execution of a thread for a specified time.
>>> time.sleep(10)
time.strftime(format[,t]): Converts a struct time to a format string.
>>> time.strftime('%Y-%m-%d', time.localtime())
'2017-09-26'
DateTime Module
The datetime module provides a more comprehensive set of functions for working with dates and times. It is built on top of the time module and provides additional features such as date and time arithmetic.
Importing the DateTime Module
To use the datetime module, you need to import it at the beginning of your Python script:
import datetime
DateTime Functions
The datetime module provides several functions for working with dates and times:
datetime.datetime.now(): Returns the current date and time.
>>> datetime.datetime.now()
datetime.datetime(2017, 9, 26, 18, 14, 51, 510000)
datetime.datetime.now() + timedelta(days=3): Returns the date and time three days from now.
>>> datetime.datetime.now() + datetime.timedelta(days=3)
datetime.datetime(2017, 9, 29, 18, 14, 51, 510000)
datetime.datetime.now() + timedelta(hours=-3): Returns the date and time three hours ago.
>>> datetime.datetime.now() + datetime.timedelta(hours=-3)
datetime.datetime(2017, 9, 26, 15, 14, 51, 510000)
datetime.datetime.now() + timedelta(minutes=30, hours=3): Returns the date and time three and a half hours from now.
>>> datetime.datetime.now() + datetime.timedelta(minutes=30, hours=3)
datetime.datetime(2017, 9, 26, 21, 44, 51, 510000)
Examples
Here are some examples of using the time and datetime modules:
# Get the current time
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(datetime.datetime.now())
# Get the time three days from now
print(datetime.datetime.now() + datetime.timedelta(days=3))
# Get the time three hours ago
print(datetime.datetime.now() + datetime.timedelta(hours=-3))
# Get the time three and a half hours from now
print(datetime.datetime.now() + datetime.timedelta(minutes=30, hours=3))
Note: The time module is a built-in module in Python, and the datetime module is a standard library module that is included with Python.