]> git.earman.xyz Git - sensor-watch.git/commitdiff
utilities: convert unix timestamp to watch_date_time
authorJoey Castillo <joeycastillo@utexas.edu>
Thu, 25 Nov 2021 16:10:06 +0000 (11:10 -0500)
committerJoey Castillo <joeycastillo@utexas.edu>
Thu, 25 Nov 2021 16:10:06 +0000 (11:10 -0500)
watch-library/watch/watch_utility.c
watch-library/watch/watch_utility.h

index 82ea5a6b57224a6723b87ca7a943b111ff129cbb..a1f696c40dec3ef81e5dbfbf2fedfb98a6f38e63 100644 (file)
@@ -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;
 
index 5374fb1c44f0e5b98aaa0714bc477767e45037c1..0a8f2b135184d67304d6562b87b0d7e6b4e9ee94 100644 (file)
@@ -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