Saving data with timestamps
Overview
- In this lesson we are going to learn how to add a date and time to our data which is sometimes called a timestamp.
- A timestamp records the current time that an event took place.
- This becomes important later when we want to graph the data that we collected.
- Computer settings are important with dates and times.
- When we initially set up our Raspberry Pi, we need to define localisation options to be Melbourne, Australia.
- The computer can then calculate our local time and even make allowances for daylight savings and atomic clock corrections.
- It is also important that the Raspberry Pi is connected to the internet so that it can re-calibrate the onboard clock.
- In python there is a special library called datetime that makes it easy to record and format dates and times.
- In this lesson we will create a new program to get some experience with the datetime library before we add it back into our main program.
Learning Objectives
- Learn how use the python datetime library.
- Learn how to update our main program with the datetime library functions.
- Learn how to verify that our data has saved correctly using the File Manager and Terminal.
Getting the current date and time
- In this example we will experiment with a new python library named datetime.
- We will demonstrate how to get the current computer time, and also learn how to format the response.
- More information on formatting the datetime output can also be obtained from W3Schools at the following URL: Python Dates (w3schools.com)
Create a new file named timestamp.py
- From the Raspberry Pi main menu drop down select Programming > Python3 (IDLE).
- This will open the Python Shell.
- From the File drop down menu select New File.
- Add a comment to the new file - # timestamp.py.
- The file name will be timestamp.py
- From the File dropdown menu select Save As.
- Navigate to your project directory.
- In this case it is botanica-park-lake
- Name the file timestamp.py and click Save.
- Enter the following code:
- import datetime – library import
- now = datetime.datetime(now) – creates a new datetime object named now
- print(now) – printing the now object
- Save and Run the program
- The output in the Shell should show the year, month, day, hour, minute, second and microsecond time components.
Using the strftime() method
- When we create a datetime object (now) we have access to a method for formatting output of the date object into more readable strings.
- The method that does this is strftime().
- To format the datetime object we need to use some special characters called Directives.
- Each Directive has a percentage sign (%) and a single upper or lower-case letter (a-z, A-Z).
- %Y – full version of year – e.g. 2021
- %b – month name short version – e.g. Jan, Feb, Mar
- %d – day of the month 01 to 31 – e.g. 28
- %H – hour 00 to 24 – e.g. 08
- %M – minute 00 to 59 – e.g. 48
- To use these Directives to format the date object add the following two lines of code.
- date_stamp = now.strftime(“%Y”)
- print(date_stamp)
- The full code is included below.
- Save and Run the program.
- There are two lines of output:
- 2021-05026 03:45:43... – print out of the now instance of the datetime object
- 2021 – print out of the now datetime object formatted using the strftime() method to show the Year (%Y).
- We can now keep on adding Directives to achieve our own customised format for the date and time.
- In this example we have added the short version of the month using the (%b) Directive – e.g. Jan.
- To add the full version of the month we would use the (%B) Directive – e.g. January.
- To represent months as numbers 01 to 12 use the (%m) Directive – e.g. 01 (for January).
- Save and Run the program and test some options.









