Enable Javascript


Last Arduino/ESP project (click to open)

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Article Index

sMsecTime.h
A very Simple, easy to use and small ESP32 Milliseconds Time library.
Time synchronization from NTP time servers.
Timestamp in milliseconds: 1586793080376
Date and time: Monday 13 April 2020 15:51:20.376


(click thumbnail)

       sMsecTime.ino - sMsecTimeCalc.ino

Why a time in milliseconds?:
A time in hours and minutes is sufficient for normal use. Sometimes it is also useful to know the number of seconds. However, when storing or transmitting data, such as sensor data, a timestamp in milliseconds is highly desirable.
With my simple and small time library you have all the usual date and time values, included milliseconds, through the sEpoch and sDateTime structures.

No third-party time libraries required:
miliseconds, seconds, minutes, hours, day, month, year, week, weekday, yearday, zone, DST, offset (zone+DST), month Long, month Short, day Long, day Short.
seconds - offset = UTC/GMT
Time synchronization from NTP time servers or user input.

My sMsecTime.h
Is a very simple, easy to use and small ESP32 Time library to set, get and manipulate date and time.
The sMsecTime.h structures and functions can be found on page 3.
Date and time manipulation is easily done by adding or subtracting a timestamp. You can calc a timestamp or use one of the functions sMakeTime() or use the function sNow() to get it from the system timer.
EXAMPLES (download my simpleLib):
  sMsecTimeM5StickC.ino (picture on top) (using NTP time server)
  sMsecTime.ino (thumbnail 1) (using NTP time server)
  sMsecTimeCalc.ino (thumbnail 2) (using timestamps)

ESP32 Time from the system timer:
The time can be easily set and used via the internal RTC system timer and the ESP32 timeval structure. We set the system timer time to a UNIX epoch timestamp, the number of seconds from January 1, 1970 00:00:00. We do this easily with the sGetNTP() function. Now we can use the timeval tructure to set the struct sEpoch sec and msec that we use in this library.

NTP Time Servers:
With one of these default servers time.google.com, pool.ntp.org and time.nist.gov and the sGetNTP() function we get a actual UNIX epoch timestamp. We use this at start and for synchronization, so we also use WiFi. The default SNTP synchronization interval (3600 sec) is used.

Timezone and DST/Summertime:
Use timezone data from: https://sites.google.com/a/usapiens.com/opnode/time-zones
char * timeZone = "<tz-string>";  // in your ino-code and call sSetTz(timeZone) in setup().
  "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"  EU Amsterdam/Barcelona with Summertime (DST)
  "EST5EDT,M3.2.0,M11.1.0"  USA Eastern Time with DST
  "CET-1"  EU Amsterdam/Barcelona NO Summertime (DST)
  "CET+0"  Coordinated Universal Time (UTC) / Greenwich Mean Time (GMT)