Saving data with timestamps

From Sensors in Schools
Jump to navigation Jump to search

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).