Log the date and time

In this tutorial we show you how to run a simple data-logging program on your device. The program logs the current date and time, which can then be viewed in the serial output of your device.

The following Toit program uses print to log the current date and time.

  time := Time.now.local
  print "Time: $(%02d time.h):$(%02d time.m)"
  print "Date: $(%04d time.year)-$(%02d time.month)-$(%02d time.day)"

Save the above Toit program in a file named time.toit, and run the command

$ jag run time.toit
Running 'time.toit' on 'mydevice' ...
Success: Sent 37KB code to 'mydevice'

This runs the time.toit program on your default device. If you have more than one device and haven't defined a default device yet, you can use the -d option to specify on which device the program should run.

To see the output of the program you need to monitor the serial line, which you can do in a different terminal window with the jag monitor command.

The output in the jag monitor will look like

[jaguar] INFO: program 7a3d83f9-837c-589d-a6e4-8f95ecb3cab4 started
Time: 00:00
Date: 1970-01-01
[jaguar] INFO: program 7a3d83f9-837c-589d-a6e4-8f95ecb3cab4 stopped

The reason you see this date from the 1970s is that the system time has not been updated on your device yet. Here is a small program that can be run after power-up to fix that.