Python's Time and Datetime Libraries
Note |
---|
title | Ignition's System Functions vs Python's LIbrariesMany components in Ignition that contain a Date property actually expect a Java calendar object. Creating a datetime object using Python's built-in libraries and passing them to a date property on a component will result in an exception.
As a result, it Creating Dates - Python's Time Library
The time library can be use to return dates as well as time. Times are created as a tuple of integers. The integers represent the following values: year, month, day of the month, hour, minute, second, weekday, day of the year, daylight savings time)
Check out Python's time library documentation for more information.
Code Block |
---|
language | py |
---|
title | Python - Python Library - time |
---|
|
import time
# Finds the current local time. The time is returned as a tuple of integers.
myTime = time.localtime()
# Print the time into a 24-character string with the following format: Sun Nov 20 12:00:00 2017
print time.asctime(myTime)
# Alternatively, we can reformat the time in a custom manner, then print it
print time.strftime('%H:%M:%S %b %d %Y', myTime) |
Creating Dates - Python's Datetime Library
Python's datetime library offers a bit more flexibility since arithmetic can easily be applied. Additional information on the datetime library can be found in Python's official documentation.
Note Notice the double use of 'datetime' in the example below. This is because the 'datetime' library has a class named 'datetime.'
Code Block |
---|
language | py |
---|
title | Python - Python Library - datetime |
---|
|
import datetime
# Returns the current datetime.
print datetime.datetime.now() |
However, we can clean up the above by importing in the datetime class from the library:
Code Block |
---|
language | py |
---|
title | Python - Python Library - datetime |
---|
|
# Imports the class named 'datetime' from the 'datetime' library, so we don't have to state it twice.
from datetime import datetime
# Returns the current datetime.
print datetime.now() |
If you need to create a specific datetime, instead of just using the current, you can pass in the values directly when creating an instance of datetime:
Code Block |
---|
language | py |
---|
title | Python - Python Library - Creating a New Time |
---|
|
from datetime import datetime
# Prints out the following datetime: 2018-01-02 03:04:05.000006
print datetime(2018,1,2,3,4,5,6) |
Finding the difference between two datetime objects can easily be accomplished by using the '-' character
Code Block |
---|
language | py |
---|
title | Python - Python Library - Date Difference |
---|
|
from datetime import datetime
rightNow = datetime.now()
someTime = datetime(2018,1,1,1,1,1,1)
# Find the difference between the two dates.
print someTime - rightNow |
Date Arithmetic
With Python's built-in libraries, the timedelta class provides the simplest way to perform arithmetic on a date: It simply creates an object that effectively represents a duration. The duration can then be applied to a datetime.
Code Block |
---|
language | py |
---|
title | Python - Python Library - Date Arithmetic |
---|
|
# We're including the timedelta class here
from datetime import datetime, timedelta
rightNow = datetime.now()
# Creating a timedelta object, and setting the hours to 8
offset = timedelta(hours = 8)
# Print the current time, and then print the time minus the offset.
print rightNow
print rightNow - offset |