Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson
2025-01-27 11:38:16 -08:00
committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions
+328
View File
@@ -0,0 +1,328 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "drivers/rtc.h"
#include "syscall/syscall_internal.h"
#include "time.h"
#include "util/math.h"
#include <string.h>
#include "FreeRTOS.h"
#include "portmacro.h"
// timezone abbreviation
static char s_timezone_abbr[TZ_LEN] = { 0 }; // longest timezone abbreviation is 5 char + null
static int32_t s_timezone_gmtoffset = 0;
static int32_t s_dst_adjust = SECONDS_PER_HOUR;
static time_t s_dst_start = 0;
static time_t s_dst_end = 0;
static const uint8_t s_mon_lengths[2][MONTHS_PER_YEAR] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static const uint16_t s_year_lengths[2] = {
365,
366
};
int32_t time_get_gmtoffset(void) {
return s_timezone_gmtoffset;
}
bool time_get_isdst(time_t utc_time) {
// do we have any DST set for the timezone we are in
if ((s_dst_start == 0) || (s_dst_end == 0)) {
return false;
}
return ((s_dst_start <= utc_time) && (utc_time < s_dst_end));
}
int time_will_transition_dst(time_t prev, time_t next) {
if (time_get_isdst(prev) == time_get_isdst(next)) {
return 0;
} else if (time_get_isdst(prev)) {
return time_get_dstoffset();
} else {
return -time_get_dstoffset();
}
}
int32_t time_get_dstoffset(void) {
return s_dst_adjust;
}
time_t time_get_dst_start(void) {
return s_dst_start;
}
time_t time_get_dst_end(void) {
return s_dst_end;
}
DEFINE_SYSCALL(time_t, sys_time_utc_to_local, time_t t) {
return time_utc_to_local(t);
}
time_t time_utc_to_local(time_t utc_time) {
utc_time += time_get_isdst(utc_time) ? s_dst_adjust : 0;
utc_time += s_timezone_gmtoffset;
return utc_time;
}
time_t time_local_to_utc(time_t local_time) {
// Note that there is 1 hour a year where it is impossible to undo the DST offset based solely
// on local time. For example, if the clock goes backward by 1 hour at 2am, then all times
// between 1am and 2am will appear twice, and there is no way to tell which of the two
// intervals we are being passed.
local_time -= s_timezone_gmtoffset;
local_time -= time_get_isdst(local_time - s_dst_adjust) ? s_dst_adjust : 0;
return local_time;
}
void time_get_timezone_abbr(char *out_buf, time_t utc_time) {
if (!out_buf) {
return;
}
strncpy(out_buf, s_timezone_abbr, TZ_LEN);
out_buf[TZ_LEN - 1] = 0;
// Timezones with daylight savings, update modifier with current dst char
// ie. P*T is PDT for daylight savings, PST for non-daylight savings
char *tz_zone_dst_char = memchr(out_buf, '*', TZ_LEN);
if (tz_zone_dst_char) {
*tz_zone_dst_char = (time_get_isdst(utc_time)) ? 'D' : 'S';
// Workaround for UK Winter, Greenwich Mean Time; UK Summer, British Summer Time
if (!strncmp(out_buf, "BDT", TZ_LEN)) {
strncpy(out_buf, "BST", TZ_LEN);
} else if (!strncmp(out_buf, "BST", TZ_LEN)) {
strncpy(out_buf, "GMT", TZ_LEN);
}
}
}
// converts time_t to struct tm for localtime and gmtime
struct tm *time_to_tm(const time_t * tim_p, struct tm *res, bool utc_mode) {
time_t local_time;
time_t utc_time = *tim_p;
if (utc_mode) {
res->tm_gmtoff = 0;
res->tm_isdst = 0;
strncpy(res->tm_zone, "UTC", TZ_LEN);
res->tm_zone[TZ_LEN - 1] = '\0';
local_time = utc_time;
} else {
res->tm_gmtoff = time_get_gmtoffset();
res->tm_isdst = time_get_isdst(utc_time);
time_get_timezone_abbr(res->tm_zone, utc_time);
local_time = utc_time + res->tm_gmtoff + (res->tm_isdst ? s_dst_adjust : 0);
}
int32_t days = local_time / SECONDS_PER_DAY;
int32_t rem = local_time % SECONDS_PER_DAY;
while (rem < 0) {
rem += SECONDS_PER_DAY;
--days;
}
while (rem >= SECONDS_PER_DAY) {
rem -= SECONDS_PER_DAY;
++days;
}
/* compute hour, min, and sec */
res->tm_hour = (int) (rem / SECONDS_PER_HOUR);
rem %= SECONDS_PER_HOUR;
res->tm_min = (int) (rem / SECONDS_PER_MINUTE);
res->tm_sec = (int) (rem % SECONDS_PER_MINUTE);
/* compute day of week */
if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYS_PER_WEEK)) < 0) {
res->tm_wday += DAYS_PER_WEEK;
}
/* compute year & day of year */
int y = EPOCH_YEAR;
int yleap;
if (days >= 0) {
for (;;) {
yleap = YEAR_IS_LEAP(y);
if (days < s_year_lengths[yleap]) {
break;
}
y++;
days -= s_year_lengths[yleap];
}
} else {
do {
--y;
yleap = YEAR_IS_LEAP(y);
days += s_year_lengths[yleap];
} while (days < 0);
}
res->tm_year = y - TM_YEAR_ORIGIN;
res->tm_yday = days;
const uint8_t *ip = s_mon_lengths[yleap];
for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon) {
days -= ip[res->tm_mon];
}
res->tm_mday = days + 1;
return res;
}
struct tm *gmtime_r(const time_t *timep, struct tm *result) {
return time_to_tm(timep, result, true);
}
struct tm *localtime_r(const time_t *timep, struct tm *result) {
return time_to_tm(timep, result, false);
}
void time_util_split_seconds_into_parts(uint32_t seconds,
uint32_t *day_part, uint32_t *hour_part, uint32_t *minute_part, uint32_t *second_part) {
*day_part = seconds / SECONDS_PER_DAY;
seconds -= *day_part * SECONDS_PER_DAY;
*hour_part = seconds / SECONDS_PER_HOUR;
seconds -= *hour_part * SECONDS_PER_HOUR;
*minute_part = seconds / SECONDS_PER_MINUTE;
seconds -= *minute_part * SECONDS_PER_MINUTE;
*second_part = seconds;
}
int time_util_get_num_hours(int hours, bool is24h) {
return is24h ? hours : (hours + 12 - 1) % 12 + 1;
}
int time_util_get_seconds_until_daily_time(struct tm *time, int hour, int minute) {
int hour_diff = hour - time->tm_hour;
if (hour < time->tm_hour || (hour == time->tm_hour && minute <= time->tm_min)) {
// It's past the mark, skip to tomorrow.
hour_diff += HOURS_PER_DAY;
}
int minutes_diff = (hour_diff * MINUTES_PER_HOUR) + (minute - time->tm_min);
return (minutes_diff * SECONDS_PER_MINUTE) - (time->tm_sec);
}
void time_util_update_timezone(const TimezoneInfo *tz_info) {
strncpy(s_timezone_abbr, tz_info->tm_zone, sizeof(tz_info->tm_zone));
s_timezone_abbr[TZ_LEN - 1] = '\0';
s_timezone_gmtoffset = tz_info->tm_gmtoff;
s_dst_start = tz_info->dst_start;
s_dst_end = tz_info->dst_end;
// Lord Howe Island has a half-hour DST
if (tz_info->dst_id == DSTID_LORDHOWE) {
s_dst_adjust = SECONDS_PER_HOUR / 2;
} else {
s_dst_adjust = SECONDS_PER_HOUR;
}
}
time_t time_util_get_midnight_of(time_t ts) {
struct tm tm;
localtime_r(&ts, &tm);
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
return mktime(&tm);
}
bool time_util_range_spans_day(time_t start, time_t end, time_t start_of_day) {
return (start <= start_of_day && end >= (start_of_day + SECONDS_PER_DAY));
}
time_t time_utc_to_local_using_offset(time_t utc_time, int16_t utc_offset_min) {
if (utc_offset_min < 0) {
return utc_time - (time_t) ABS(utc_offset_min) * SECONDS_PER_MINUTE;
} else {
return utc_time + (time_t) utc_offset_min * SECONDS_PER_MINUTE;
}
}
time_t time_local_to_utc_using_offset(time_t local_time, int16_t utc_offset_min) {
if (utc_offset_min < 0) {
return local_time + (time_t) ABS(utc_offset_min) * SECONDS_PER_MINUTE;
} else {
return local_time - (time_t) utc_offset_min * SECONDS_PER_MINUTE;
}
}
time_t time_util_utc_to_local_offset(void) {
time_t now = rtc_get_time();
return (time_utc_to_local(now) - now);
}
// ---------------------------------------------------------------------------------------
DayInWeek time_util_get_day_in_week(time_t utc_sec) {
struct tm local_tm;
localtime_r(&utc_sec, &local_tm);
return local_tm.tm_wday;
}
// ---------------------------------------------------------------------------------------
uint16_t time_util_get_day(time_t utc_sec) {
// Convert to local seconds
time_t local_sec = time_utc_to_local(utc_sec);
// Figure out the day index
return (local_sec / SECONDS_PER_DAY);
}
// ---------------------------------------------------------------------------------------
int time_util_get_minute_of_day(time_t utc_sec) {
struct tm local_tm;
localtime_r(&utc_sec, &local_tm);
return (local_tm.tm_hour * MINUTES_PER_HOUR) + local_tm.tm_min;
}
// ---------------------------------------------------------------------------------------
int time_util_minute_of_day_adjust(int minute, int delta) {
minute += delta;
if (minute < 0) {
minute += MINUTES_PER_DAY;
} else if (minute >= MINUTES_PER_DAY) {
minute -= MINUTES_PER_DAY;
}
return minute;
}
// ---------------------------------------------------------------------------------------
time_t time_start_of_today(void) {
time_t now = rtc_get_time();
return time_util_get_midnight_of(now);
}
DEFINE_SYSCALL(time_t, sys_time_start_of_today, void) {
return time_start_of_today();
}
// ---------------------------------------------------------------------------------------
uint32_t time_get_uptime_seconds(void) {
RtcTicks ticks = rtc_get_ticks();
return ticks / configTICK_RATE_HZ;
}
+226
View File
@@ -0,0 +1,226 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h> // time_t and size_t
#define TM_YEAR_ORIGIN 1900
#define EPOCH_YEAR 1970
#define EPOCH_WDAY 4
#define DAYS_PER_WEEK 7
#define MONTHS_PER_YEAR 12
#define MS_PER_SECOND (1000)
#define SECONDS_PER_MINUTE (60)
#define MS_PER_MINUTE (MS_PER_SECOND * SECONDS_PER_MINUTE)
#define MINUTES_PER_HOUR (60)
#define SECONDS_PER_HOUR (SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
#define HOURS_PER_DAY (24)
#define MINUTES_PER_DAY (HOURS_PER_DAY * MINUTES_PER_HOUR)
#define SECONDS_PER_DAY (MINUTES_PER_DAY * SECONDS_PER_MINUTE)
#define SECONDS_PER_WEEK (SECONDS_PER_DAY * DAYS_PER_WEEK)
#define YEAR_IS_LEAP(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
#define IS_WEEKDAY(d) (d >= Monday && d <= Friday)
#define IS_WEEKEND(d) (d == Saturday || d == Sunday)
#define TZ_LEN 6
// DST special cases. These map to indexes in the tools/timezones.py script that handles parsing
// the olsen database into a compressed form. Don't change these without changing the script.
//
// Note that we don't correctly handle Morroco's DST rules, they're incredibly complex due to them
// suspending DST each year for Ramadan, resulting in 4 DST transitions each year.
//
// Any DST ids that aren't listed below have sane DST rules, where they change to DST in the
// spring on the same day by 1 hour each year and change from DST on a later day each year.
#define DSTID_BRAZIL 6
#define DSTID_LORDHOWE 20
//! @file time.h
//! @addtogroup StandardC Standard C
//! @{
//! @addtogroup StandardTime Time
//! \brief Standard system time functions
//!
//! This module contains standard time functions and formatters for printing.
//! Note that Pebble now supports both local time and UTC time
//! (including timezones and daylight savings time).
//! Most of these functions are part of the C standard library which is documented at
//! https://sourceware.org/newlib/libc.html#Timefns
//! @{
//! structure containing broken-down time for expressing calendar time
//! (ie. Year, Month, Day of Month, Hour of Day) and timezone information
struct tm {
int tm_sec; /*!< Seconds. [0-60] (1 leap second) */
int tm_min; /*!< Minutes. [0-59] */
int tm_hour; /*!< Hours. [0-23] */
int tm_mday; /*!< Day. [1-31] */
int tm_mon; /*!< Month. [0-11] */
int tm_year; /*!< Years since 1900 */
int tm_wday; /*!< Day of week. [0-6] */
int tm_yday; /*!< Days in year.[0-365] */
int tm_isdst; /*!< DST. [-1/0/1] */
int tm_gmtoff; /*!< Seconds east of UTC */
char tm_zone[TZ_LEN]; /*!< Timezone abbreviation */
};
//! Enumeration of each day of the week.
typedef enum DayInWeek {
Sunday = 0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
} DayInWeek;
//! Obtain the number of seconds and milliseconds part since the epoch.
//! This is a non-standard C function provided for convenience.
//! @param tloc Optionally points to an address of a time_t variable to store the time in.
//! You may pass NULL into tloc if you don't need a time_t variable to be set
//! with the seconds since the epoch
//! @param out_ms Optionally points to an address of a uint16_t variable to store the
//! number of milliseconds since the last second in.
//! If you only want to use the return value, you may pass NULL into out_ms instead
//! @return The number of milliseconds since the last second
uint16_t time_ms(time_t *tloc, uint16_t *out_ms);
//! @} // end addtogroup StandardTime
//! @} // end addtogroup StandardC
// The below standard c time functions are documented for the SDK
// by their applib pbl_override wrappers in pbl_std.h
struct tm *localtime(const time_t *timep);
struct tm *gmtime(const time_t *timep);
size_t strftime(char *s, size_t max, const char *fmt, const struct tm *tm);
time_t mktime(struct tm *tb);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime_r(const time_t *timep, struct tm *result);
//! Minimal struct to store timezone info in RTC registers
typedef struct TimezoneInfo {
char tm_zone[TZ_LEN - 1]; //!< Up to 5 character (no null terminator) timezone abbreviation
uint8_t dst_id; //!< Daylight savings time zone index
int16_t timezone_id; //!< Olson index of timezone
int32_t tm_gmtoff; //!< GMT time offset
time_t dst_start; //!< timestamp of start of daylight savings period (0 if none)
time_t dst_end; //!< timestamp of end of daylight savings period (0 if none)
} TimezoneInfo;
//! Provides the timezone abbreviation string for the given time. Uses the utc_time provided
//! to correct the abbreviation for daylight savings time if applicable
//! @param out_buf should have length TZ_LEN
void time_get_timezone_abbr(char *out_buf, time_t utc_time);
//! Provides the gmt offset
int32_t time_get_gmtoffset(void);
//! Returns true if the UNIX time provided falls within DST
bool time_get_isdst(time_t utc_time);
// 0 = no transition
// <0 = dst begins between prev and next
// >0 = dst ends between prev and next
// returns difference in seconds of DST
int time_will_transition_dst(time_t prev, time_t next);
//! Returns the DST offset
int32_t time_get_dstoffset(void);
//! Returns the DST start timestamp
time_t time_get_dst_start(void);
//! Returns the DST end timestamp
time_t time_get_dst_end(void);
//! Convert UTC time, as returned by rtc_get_time() into local time
time_t time_utc_to_local(time_t utc_time);
//! Convert local time to UTC time
time_t time_local_to_utc(time_t local_time);
void time_util_split_seconds_into_parts(uint32_t seconds, uint32_t *day_part,
uint32_t *hour_part, uint32_t *minute_part, uint32_t *second_part);
int time_util_get_num_hours(int hours, bool is24h);
int time_util_get_seconds_until_daily_time(struct tm *time, int hour, int minute);
//! Set the timezone
void time_util_update_timezone(const TimezoneInfo *tz_info);
time_t time_util_get_midnight_of(time_t ts);
bool time_util_range_spans_day(time_t start, time_t end, time_t start_of_day);
//! User-mode access calls
time_t sys_time_utc_to_local(time_t t);
time_t time_util_utc_to_local_offset(void);
time_t time_utc_to_local_using_offset(time_t utc_time, int16_t utc_offset_min);
time_t time_local_to_utc_using_offset(time_t local_time, int16_t utc_offset_min);
//! Computes the day index from UTC seconds. This index should change every day at midnight local
//! time
//! @param utc_sec Time to retrieve index for
//! @return Day index
uint16_t time_util_get_day(time_t utc_sec);
DayInWeek time_util_get_day_in_week(time_t utc_sec);
//! Computes the minute of the day
//! @param utc_sec Time to retrieve minute for
//! @return Minute of day
int time_util_get_minute_of_day(time_t utc_sec);
//! Adds a delta to the minute of the day and will wrap to the next or previous day if the
//! resulting minutes would have been out of bounds
//! @param minute Minute to adjust
//! @param delta Minutes to add to \ref minute
//! @return Adjusted minutes
int time_util_minute_of_day_adjust(int minute, int delta);
//! Return the UTC time that corresponds to the start of today (midnight).
//! @return the UTC time corresponding to the start of today (midnight)
time_t time_start_of_today(void);
//! Return the number of seconds since the system was restarted. This time is based on the
//! tickcount and so, unlike rtc_get_time(), it won't be affected if the phone changes the UTC
//! time on the watch.
uint32_t time_get_uptime_seconds(void);