Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions to convert between TimeLib and time.h time representations. #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ConvertTime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
ConvertTime.h - convert time representations between TimeLib's and sdtlib <time.h>'s
*/

#ifndef _ConvertTime_h
#define _ConvertTime_h

#include <time.h>
#include <TimeLib.h>
//fortunately TimeLib and time.h's definitions of time_t are both unsigned 32 bit, otherwise there might be trouble.

//converts TimeLib's time representations to time.h's representations
inline void convertToTimeH(tmElements_t &timelib, tm &timeh){
time_t t = makeTime(timelib) - UNIX_OFFSET;
localtime_r(&t, &timeh);
}
inline void convertToTimeH(time_t timelib, tm &timeh){
time_t t = timelib - UNIX_OFFSET;
localtime_r(&t, &timeh);
}
inline time_t convertToTimeH(tmElements_t &timelib){
return makeTime(timelib) - UNIX_OFFSET;
}
inline time_t convertToTimeH(time_t timelib){
return timelib - UNIX_OFFSET;
}

//converts time.h's time representations to TimeLib's representations
inline void convertToTimeLib(tm &timeh, tmElements_t &timelib){
breakTime(mktime(&timeh) + UNIX_OFFSET, timelib);
}
inline void convertToTimeLib(time_t timeh, tmElements_t &timelib){
breakTime(timeh + UNIX_OFFSET, timelib);
}
inline time_t convertToTimeLib(tm &timeh){
return mktime(&timeh) + UNIX_OFFSET;
}
inline time_t convertToTimeLib(time_t timeh){
return timeh + UNIX_OFFSET;
}
#endif _ConvertTime_h
98 changes: 98 additions & 0 deletions examples/TimeConversion/TimeConversion.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* TimeConversion.ino
*
* This program demonstrates one way to use the time conversion <ConvertTime.h> part of the library,
* to allow you to easily handle timezones and daylight savings time, and make printing much simpler.
*
* To customize the timezone, edit the UTC_OFFSET value.
* To enable automatic daylight saving time calculations, uncomment the corresponding daylight
* savings blocks.
*
* This uses the same code as the TimeSerial example to synchronize the time.
* A Processing example sketch to automatically send the messages is included in the download
* On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
*/


// change this to the UTC offset of the timezone you want:
// for reference here are the USA offset values:
// PST = -8, MST = -7, CST = -6, EST = -5
#define UTC_OFFSET -7

// Uncomment one of these blocks to enable daylight savings time calculations:
// For USA daylight savings:

//#include <util/usa_dst.h>
//#define DST usa_dst;

// For european daylight savings:

//#include <util/eur_dst.h>
//#define DST eur_dst;




#include <time.h>

#include <Time.h>
#include <ConvertTime.h>


void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only

Serial.println("Waiting for sync message");

#ifdef DST // set daylight savings if configured.
set_dst(DST);
#endif

set_zone(UTC_OFFSET * SECS_PER_HOUR); //set timezone offset

}

void loop() {
// Serial clock synchronization routine. See the TimeSerial example for an explanation of how this works.
if (Serial.available()) {
processSyncMessage();
}

if (timeStatus()!= timeNotSet) {
digitalClockDisplay();
}

delay(1000);
}


void digitalClockDisplay() {
time_t libtime = now(); // get the current time according to LibTime.
time_t timeh = convertToTimeH(libtime); // time.h uses a different format, so have to convert.
String str = ctime(&timeh); // but once we have it there, it only takes one line to turn it into a string.
// You can also easily do custom time formats using the 'strftime', although it's a bit more complicated to use.
Serial.println(str); // and print that time.
}



#define TIME_HEADER "T" // Header tag for serial time sync message

// Serial clock synchronization routine. See the TimeSerial example to see how this works.
void processSyncMessage() {
unsigned long pctime;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}





2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ setSyncProvider KEYWORD2
setSyncInterval KEYWORD2
timeStatus KEYWORD2
TimeLib KEYWORD2
convertToTimeLib KEYWORD2
convertToTimeH KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
Expand Down