Skip to content

Add method to set time from compiler time #53

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

Open
tigoe opened this issue Dec 4, 2019 · 1 comment · May be fixed by #59
Open

Add method to set time from compiler time #53

tigoe opened this issue Dec 4, 2019 · 1 comment · May be fixed by #59
Labels
type: enhancement Proposed improvement

Comments

@tigoe
Copy link

tigoe commented Dec 4, 2019

It would be handy to be able to set the time from the C TIME and DATE variables. For example:

https://github.com/ITPNYU/clock-club/tree/master/Microcontroller_Time_Setting_Methods/CompileTimeSet

@tigoe tigoe added the type: enhancement Proposed improvement label Dec 4, 2019
@mgtm98 mgtm98 linked a pull request Mar 5, 2020 that will close this issue
@trlafleur
Copy link

trlafleur commented Jun 2, 2020

Here is a function you can adapt to your program to get compile date-time to Unix time...

#include <Arduino.h>
#include <TimeLib.h>
#include <RTCZero.h>
#include <stdio.h>

time_t cvt_date(char const *date, char const *time);
void print2digits(int number);

/* Create an rtc object */
RTCZero rtc;

void setup() 
{
    Serial.begin(115200);
    delay(5000);

    Serial.println("Set Compile time to RTC");
    
    // Show raw system strings
    Serial.println(String("__DATE__ = ") + __DATE__);
    Serial.println(String("__TIME__ = ") + __TIME__);

    rtc.begin(); // initialize RTC
    
    // set system time = compile time
    rtc.setEpoch(cvt_date(__DATE__, __TIME__));
    Serial.print("Unix Time: ");
    Serial.println(rtc.getEpoch());
}


time_t cvt_date(char const *date, char const *time)
{
    char s_month[5];
    int year, day;
    int hour, minute, second;
    tmElements_t t;
    static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
    sscanf(date, "%s %d %d", s_month, &day, &year);
    //sscanf(time, "%2hhd %*c %2hhd %*c %2hhd", &t.Hour, &t.Minute, &t.Second);
    sscanf(time, "%2d %*c %2d %*c %2d", &hour, &minute, &second);
    // Find where is s_month in month_names. Deduce month value.
    t.Month = (strstr(month_names, s_month) - month_names) / 3 + 1;
    t.Day = day;
    // year can be given as '2010' or '10'. It is converted to years since 1970
    if (year > 99) t.Year = year - 1970;
    else t.Year = year + 50;
    t.Hour = hour;
    t.Minute = minute;
    t.Second = second;
    return makeTime(t);
}
void loop() {
}

@per1234 per1234 linked a pull request May 2, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants