Advanced Python Development Course
Chapter
>
Level
String & Time Modules
Date/Time Module
Objective
Adjust records and set up shipping dates for supplies using Date & Time module functions.
The lower floor of the annex of the new construction area is a warehouse containing some of the recently delivered supplies that have been packed and ready to ship to areas around the new construction. They need to be labeled with their correct dates and times, and ensure that any labels that have already been applied have all relevant information. To do this we could take the time to manually write and format dates and time, but there’s a much quicker way to do this by using the date and time module.
The date and time module is imported by writing import datetime , this unlocks a series of powerful functions that allow you to read and set formatted times and dates which you can store in objects. You can also use: from datetime import timezone, timedelta in order to set time zones. The following functions are unlocked by importing the module:
datetime.datetime(): Allows you to set and store a time of your choosing, takes several arguments as follows:year,month,day,hour,minute,second,microsecond,timezone. You don’t have to enter all arguments, but rather the ones you wish to use.datetime.datetime.now(): Returns your own current date and time as it would appear in a clock.datetime.datetime.date(): Returns the datetime date only, no time includeddatetime.datetime.time(): Returns the datetime time only, no date includeddatetime.datetime.timestamp(): Returns the datetime’s timestamp as a UNIX code.timezone(): Allows you to set a time zone for use with your dates and time arguments.datetime.strftime(): Allows you to extract specific information of date and time and format it to your liking using specific codes. For our purposes we’ll be using the following codes:"%a": Weekday , shortened"%d": Day of the month"%B": Month name, written in full"%I": Hour , twelve hour cycle"%M": Minute"%p": Meridian, AM/PM"%x": Local date format"%X": Local time format"%j": Day of the year"%U": Week of the year"%z": UTC time zone
To start off walk to the light X mark and face the desk, use the read() function to check the packing dates for the shipments as well as start and end dates for the projects. Make note of the start and end dates provided in the document (month/day/year). The three (3) first data points written in the memo as well as the time zone are pre-written in the code editor for ease of use in later functions.
On this same space, create a variable named today and store the return value of datetime.datetime.now() . Use str() to convert the value into a string so it’s in a readable format, like this: today = str(datetime.datetime.now()). Use the display() function to chart down today’s date on the memo, with this the date for the shipping allocations is set and you can begin adding missing data points to the necessary areas.
Walk to the gold X mark next to the desk and here we’ll be calculating and charting down the number of days this project will take. There are two variables written in the editor named start and end that store datetime. Write down the start and end dates you previously read into the spaces provided. Create a variable named result and store the result of the of a subtraction between the end and start variables. Use the str() function to convert the value into a readable string format, like this: result = str(end-start) . Use the write() function to chart down the result variable, to mark down how long this will take.
From here we’re going to move on to the gold X mark next to the red carpet and will be moving down to the X marks next to the red carpet crates on the left hand column. Here we will be adding specific parts of the packing dates that are missing from the crates using datetime() . In the editor there will be a pre-written date allocated to the variable red_date , we’ll be extracting various bit’s of data and using it to label the crates.
For the red carpets:
-
On the gold X mark , face the crates and create a variable named
red_daysstore in it the date without the time usingdate()from thered_datevariable and, convert it usingstr(), like this:red_days = str(red_date.date()). Use thewrite()function to label the crates usingred_days. -
On the light X mark , face the crates and create a variable named
red_timestore in it the time without the date usingtime()from thered_datevariable and convert it usingstr(), like this:red_time = str(red_date.time()). Use thewrite()function to label the crates usingred_time. -
On the dark X mark , face the crates and create a variable named
red_timestampstore in it the timestamp calculated from thered_datevariable, like this:red_timestamp = red_date.timestamp(). Use thewrite()function to label the crates usingred_timestamp.
Follow this up with moving onto X marks in front of blue carpet crates. Like before there is a series of three (3) colored X marks, in this case referencing the pre-written blue_date variable to complete the labels. Go through the column and use the strftime() function to add missing elements to the shipping labels. Unlike the previous functions this one requires a specific code to extract and format elements in the target date. For example, if we want to get the day of the week for the blue_date variable you will use the code "%a" with the strftime() function like this: blue_date.strftime("%a") .
For the X marks next to the blue carpet crates, use the strftime() function with the specific code required. Look through the previously outlined code list depending on the requirements. For the blue carpets:
-
On the gold X mark , face the crates and create a variable named
blue_weekdaystore in it the day of the week usingstrftime()from theblue_datevariable like this:blue_day = blue_date.strftime("%a"). Create another variable namedblue_dayand store the day of the month usingstrftime()from theblue_datevariable. Create another variable namedblue_monthand store the month name, written in full, usingstrftime()from theblue_datevariable. Insert theblue_weekday,blue_dayandblue_monthvariables in the pre-writtenwrite()function. -
On the light X mark , face the crates and create a variable named
blue_hourstore in it the hour , twelve hour cycle, usingstrftime()from theblue_datevariable. Create another variable namedblue_minuteand store the minute usingstrftime()from theblue_datevariable. Create another variable namedblue_meridianand store the Meridian, AM/PM, usingstrftime()from theblue_datevariable. Insert theblue_hour,blue_minuteandblue_meridianvariables in the pre-writtenwrite()function. -
On the dark X mark , face the crates and create a variable named
blue_local_datestore in it the local date format, usingstrftime()from theblue_datevariable. Create another variable namedblue_local_timeand store the local time format usingstrftime()from theblue_datevariable. Insert theblue_local_date,blue_local_timeandblue_meridianvariables in the pre-writtendisplay()function.
Finally, we’re going to move on to the gold X mark next to the green carpet and will be moving down to the X marks next to the green carpet crates on the right hand column. Here we will continue adding specific parts of the packing dates that are missing from the crates using strftime() . In the editor there will be a pre-written date allocated to the variable green_date , we’ll be extracting various bit’s of data and using it to label the crates.
For the green carpets:
-
On the gold X mark , face the crates and create a variable named
green_daystore in it the day of the year usingstrftime()from thegreen_datevariable. Use thewrite()function to label the crates usinggreen_day. -
On the light X mark , face the crates and create a variable named
green_weekstore in it the week of the year usingstrftime()from thegreen_datevariable. Use thewrite()function to label the crates usinggreen_week. -
On the dark X mark , face the crates and create a variable named
green_timezonestore in it the UTC time zone usingstrftime()from thegreen_datevariable. Use thewrite()function to label the crates usinggreen_timezone.
Once all labels on X marks next to colored carpets have been charted down, the level will be complete.