summaryrefslogtreecommitdiffstats
path: root/watch-library/watch/watch_utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'watch-library/watch/watch_utility.c')
-rw-r--r--watch-library/watch/watch_utility.c76
1 files changed, 76 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;