Python: Time and Datetime Libraries
By ChatGPT, proofread by AMO
Note: In the following post, the responses are those of ChatGPT after being informed of a basic script.
Alright, to display the variable lastFiscalYearEnd
with a different color when the month is not December, we can use the Colorama
library. Make sure to install this library if you haven’t already by using the command pip install colorama
.
Next, we will modify the code to check if the month of lastFiscalYearEnd
is December or not, and based on that, we will use Colorama
to display the date with a different color.
Here is the adjusted code:
import time
from colorama import Fore, Style
# Fiscal year closing date
lastFiscalYearEnd = time.localtime(info["lastFiscalYearEnd"])
nextFiscalYearEnd = time.strftime('%d/%m/%Y', time.localtime(info["nextFiscalYearEnd"]))
# Check the month
if lastFiscalYearEnd.tm_mon == 12:
lastFiscalYearEnd_str = time.strftime('%d/%m/%Y', lastFiscalYearEnd)
else:
lastFiscalYearEnd_str = Fore.RED + time.strftime('%d/%m/%Y', lastFiscalYearEnd) + Style.RESET_ALL
print("Previous fiscal year end: " + lastFiscalYearEnd_str + "\tNext fiscal year end: " + nextFiscalYearEnd)
In this code, we use time.localtime() to get a struct_time object from the date lastFiscalYearEnd. Then, we check if the month (represented by lastFiscalYearEnd.tm_mon) is equal to 12 (December) or not. If it is, we simply display the date without color. Otherwise, we use Fore.RED from Colorama to display the date in red.
Make sure to import Fore and Style from Colorama to use color styles. And if the month is not December, we use Style.RESET_ALL to reset the color to default after displaying the date, so that the next text is displayed normally.
Ensure you have Colorama installed and run the code to see the color difference for the date when the month is not December.
’time’ Library
Certainly! In Python, the time module provides functions for working with dates and times. When you use the time.localtime() function, it returns a struct_time object that contains various information about the date and time.
The .tm_mon member of the struct_time object represents the month as an integer. Here are the main attributes of the struct_time object:
.tm_year: The current year (e.g., 2023). .tm_mon: The current month as an integer (values from 1 to 12, where 1 represents January and 12 represents December). .tm_mday: The day of the month (values from 1 to 31). .tm_hour: The hour of the day (values from 0 to 23). .tm_min: The minutes of the current hour (values from 0 to 59). .tm_sec: The seconds of the current hour (values from 0 to 61). In the adjusted code, we use lastFiscalYearEnd.tm_mon to get the month from the struct_time object corresponding to the date lastFiscalYearEnd. Then, we compare this value with 12 to check if the month is December or not.
If lastFiscalYearEnd.tm_mon is equal to 12, it means the month is December, and we display the date without any additional color. If lastFiscalYearEnd.tm_mon is not equal to 12, it means the month is not December, and we use Colorama to display the date in red (or any other color of your choice) to distinguish it.
In summary, .tm_mon is an attribute of the struct_time object that allows us to access the month of a specific date.
‘datetime’ Library
Alright! We can apply the same principle of using Colorama to highlight the lastDividendDate if more than 18 months have passed since that date. For this, we will calculate the difference between the current date and lastDividendDate, then check if this difference is greater than 18 months.
Here is the adjusted code:
import time
from datetime import datetime
from colorama import Fore, Style
# Conversion of the date to datetime object
lastDividendDate_str = time.strftime('%d/%m/%Y', time.localtime(info["lastDividendDate"]))
lastDividendDate = datetime.strptime(lastDividendDate_str, '%d/%m/%Y')
# Calculation of the difference between the current date and lastDividendDate
current_date = datetime.now()
time_difference = current_date - lastDividendDate
# Check if more than 18 months have passed
if time_difference.days > 18 * 30: # Assuming an average month has 30 days
lastDividendDate_str = Fore.RED + lastDividendDate_str + Style.RESET_ALL
print("Last dividend version: " + lastDividendDate_str)
In this code, we use the datetime library to manipulate dates. First, we convert the date lastDividendDate_str (in the “day/month/year” format) to a datetime object using datetime.strptime(). Then, we get the current date using datetime.now().
By subtracting lastDividendDate from the current date, we get a timedelta object, which represents the difference between the two dates as a duration.
We then multiply the number of months (18) that we want to compare by 30 (assuming on average each month has 30 days) to get the number of days equivalent to 18 months.
Finally, we check if the time difference in days (time_difference.days) is greater than 18 months. If it is, we use Fore.RED to display the date in red, indicating that more than 18 months have passed since the last dividend date.
Don’t forget to import Fore and Style from Colorama to use color styles. With this code, you should now be able to highlight the lastDividendDate when more than 18 months have passed since that date.
Working in Seconds
Absolutely! You’re right; we can avoid re-converting lastDividendDate to a datetime object if we directly use the value stored in info[“lastDividendDate”]. We can work with timestamps (time values in seconds since January 1, 1970) to perform the comparison directly.
Here is the adjusted code using info[“lastDividendDate”] directly:
import time
from colorama import Fore, Style
# Retrieving the dividend date in timestamp format
lastDividendDate_timestamp = info["lastDividendDate"]
# Conversion of the timestamp to date in "day/month/year" format
lastDividendDate_str = time.strftime('%d/%m/%Y', time.localtime(lastDividendDate_timestamp))
# Calculation of the difference between the current date and lastDividendDate
current_timestamp = time.time()
time_difference = current_timestamp - lastDividendDate_timestamp
# Check if more than 18 months have passed
if time_difference > 18 * 30 * 24 * 60 * 60: # Assuming an average month has 30 days and a day has 24 hours
lastDividendDate_str = Fore.RED + lastDividendDate_str + Style.RESET_ALL
print("Last dividend version: " + lastDividendDate_str)
In this code, we use info[“lastDividendDate”] directly to get the timestamp of the last dividend date. Then, we perform the same calculation of the time difference in seconds between the current date and lastDividendDate_timestamp to check if more than 18 months have passed since that date.
When the time difference exceeds 18 months (in seconds), we use Fore.RED to display the date in red. Otherwise, we display it normally.
This code should work similarly to the previous version but avoids the additional conversion of the date to a datetime object.