diff options
| author | Joey Castillo <joeycastillo@utexas.edu> | 2021-11-25 11:10:06 -0500 | 
|---|---|---|
| committer | Joey Castillo <joeycastillo@utexas.edu> | 2021-11-25 11:10:06 -0500 | 
| commit | ac43c1ca48ba4e8d645f040d9930e761558fdad5 (patch) | |
| tree | 197a6bb3adf359c55a5dfea618782c12edf054df | |
| parent | aac7a2a85474fe70c89723a1755a2d0e74bffbaf (diff) | |
| download | Sensor-Watch-ac43c1ca48ba4e8d645f040d9930e761558fdad5.tar.gz Sensor-Watch-ac43c1ca48ba4e8d645f040d9930e761558fdad5.tar.bz2 Sensor-Watch-ac43c1ca48ba4e8d645f040d9930e761558fdad5.zip  | |
utilities: convert unix timestamp to watch_date_time
| -rw-r--r-- | watch-library/watch/watch_utility.c | 76 | ||||
| -rw-r--r-- | watch-library/watch/watch_utility.h | 10 | 
2 files changed, 86 insertions, 0 deletions
diff --git a/watch-library/watch/watch_utility.c b/watch-library/watch/watch_utility.c index 82ea5a6b..a1f696c4 100644 --- a/watch-library/watch/watch_utility.c +++ b/watch-library/watch/watch_utility.c @@ -74,6 +74,82 @@ uint32_t watch_utility_date_time_to_unix_time(watch_date_time date_time, uint32_      return watch_utility_convert_to_unix_time(date_time.unit.year + WATCH_RTC_REFERENCE_YEAR, date_time.unit.month, date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second, utc_offset);  } +#define LEAPOCH (946684800LL + 86400*(31+29)) + +#define DAYS_PER_400Y (365*400 + 97) +#define DAYS_PER_100Y (365*100 + 24) +#define DAYS_PER_4Y   (365*4   + 1) + +watch_date_time watch_utility_date_time_from_unix_time(uint32_t timestamp, uint32_t utc_offset) { +    watch_date_time retval; +    retval.reg = 0; +    int32_t days, secs; +    int32_t remdays, remsecs, remyears; +    int32_t qc_cycles, c_cycles, q_cycles; +    int32_t years, months; +    int32_t wday, yday, leap; +    static const int8_t days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29}; +    timestamp += utc_offset; + +    secs = timestamp - LEAPOCH; +    days = secs / 86400; +    remsecs = secs % 86400; +    if (remsecs < 0) { +        remsecs += 86400; +        days--; +    } + +    wday = (3+days)%7; +    if (wday < 0) wday += 7; + +    qc_cycles = (int)(days / DAYS_PER_400Y); +    remdays = days % DAYS_PER_400Y; +    if (remdays < 0) { +        remdays += DAYS_PER_400Y; +        qc_cycles--; +    } + +    c_cycles = remdays / DAYS_PER_100Y; +    if (c_cycles == 4) c_cycles--; +    remdays -= c_cycles * DAYS_PER_100Y; + +    q_cycles = remdays / DAYS_PER_4Y; +    if (q_cycles == 25) q_cycles--; +    remdays -= q_cycles * DAYS_PER_4Y; + +    remyears = remdays / 365; +    if (remyears == 4) remyears--; +    remdays -= remyears * 365; + +    leap = !remyears && (q_cycles || !c_cycles); +    yday = remdays + 31 + 28 + leap; +    if (yday >= 365+leap) yday -= 365+leap; + +    years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles; + +    for (months=0; days_in_month[months] <= remdays; months++) +        remdays -= days_in_month[months]; + +    years += 2000; + +    months += 2; +    if (months >= 12) { +        months -=12; +        years++; +    } + +    if (years < 2020 || years > 2083) return retval; +    retval.unit.year = years - WATCH_RTC_REFERENCE_YEAR; +    retval.unit.month = months + 1; +    retval.unit.day = remdays + 1; + +    retval.unit.hour = remsecs / 3600; +    retval.unit.minute = remsecs / 60 % 60; +    retval.unit.second = remsecs % 60; + +    return retval; +} +  float watch_utility_thermistor_temperature(uint16_t value, bool highside, float b_coefficient, float nominal_temperature, float nominal_resistance, float series_resistance) {      float reading = (float)value; diff --git a/watch-library/watch/watch_utility.h b/watch-library/watch/watch_utility.h index 5374fb1c..0a8f2b13 100644 --- a/watch-library/watch/watch_utility.h +++ b/watch-library/watch/watch_utility.h @@ -60,6 +60,16 @@ uint32_t watch_utility_convert_to_unix_time(uint16_t year, uint8_t month, uint8_    */  uint32_t watch_utility_date_time_to_unix_time(watch_date_time date_time, uint32_t utc_offset); +/** @brief Returns the UNIX time (seconds since 1970) for a given watch_date_time struct. +  * @param timestamp The UNIX timestamp that you wish to convert. +  * @param utc_offset The number of seconds that you wish date_time to be offset from UTC. +  * @return A watch_date_time for the given UNIX timestamp and UTC offset, or if outside the range that +  *         watch_date_time can represent, a watch_date_time with all fields set to 0. +  * @note Adapted from MIT-licensed code from musl, Copyright © 2005-2014 Rich Felker, et al.: +  *       https://github.com/esmil/musl/blob/1cc81f5cb0df2b66a795ff0c26d7bbc4d16e13c6/src/time/__secs_to_tm.c +  */ +watch_date_time watch_utility_date_time_from_unix_time(uint32_t timestamp, uint32_t utc_offset); +  /** @brief Returns a temperature in degrees Celsius for a given thermistor voltage divider circuit.    * @param value The raw analog reading from the thermistor pin (0-65535)    * @param highside True if the thermistor is connected to VCC and the series resistor is connected  | 
