Skip to content

Commit

Permalink
zsw_clock: Adjust the RTC time according to the timezone and fix time…
Browse files Browse the repository at this point in the history
… when not using RTC.
  • Loading branch information
jakkra committed Sep 12, 2024
1 parent 212dc54 commit 50f6aed
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/src/ble/gadgetbridge/ble_gadgetbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void parse_time(char *start_time)
end_time = strstr(start_time, ")");
if (end_time) {
errno = 0;
cb.data.data.notify.id = strtol(start_time, &end_time, 10);
cb.data.data.time.seconds = strtol(start_time, &end_time, 10);
if (start_time != end_time && errno == 0) {
cb.data.type = BLE_COMM_DATA_TYPE_SET_TIME;
send_ble_data_event(&cb);
Expand Down
12 changes: 12 additions & 0 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,19 @@ static void on_zbus_ble_data_callback(const struct zbus_channel *chan)
char tz[sizeof("UTC+01")] = { '\0' };
char sign = (event->data.data.time.tz_offset < 0) ? '+' : '-';
snprintf(tz, sizeof(tz), "UTC%c%d", sign, MIN(abs(event->data.data.time.tz_offset), 99));

#ifdef CONFIG_RTC
// When using RTC, we need to adjust the current rtc_time according to the timezone.
zsw_timeval_t ztm;
zsw_clock_get_time(&ztm);
ztm.tm.tm_year -= 1900;
time_t current_rtc_time = mktime(rtc_time_to_tm(&ztm.tm));
zsw_clock_set_timezone(tz);
memcpy(&ztm.tm, localtime(&current_rtc_time), sizeof(ztm.tm)); // Adjust the time according to the new timezone
zsw_clock_set_time(&ztm);
#else
zsw_clock_set_timezone(tz);
#endif
}
break;
}
Expand Down
15 changes: 2 additions & 13 deletions app/src/zsw_clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ZBUS_CHAN_DECLARE(periodic_event_1s_chan);
ZBUS_LISTENER_DEFINE(zsw_clock_lis, zbus_periodic_slow_callback);
#endif

LOG_MODULE_REGISTER(zsw_clock, LOG_LEVEL_INF);
LOG_MODULE_REGISTER(zsw_clock, LOG_LEVEL_WRN);

#ifndef CONFIG_RTC

Expand All @@ -66,23 +66,15 @@ static void zbus_periodic_slow_callback(const struct zbus_channel *chan)

void zsw_clock_set_time(zsw_timeval_t *ztm)
{
// Substract one from the month because we want to count from December instead of January
ztm->tm.tm_mon -= 1;

// Substract 1900 from the year because we want to count from 1900
ztm->tm.tm_year -= 1900;

#if CONFIG_RTC
rtc_set_time(rtc, &ztm->tm);
#else
struct timespec tspec;

#warning "Not implemented"
tspec.tv_sec = 0;
tspec.tv_nsec = 0;
tspec.tv_sec = mktime(&ztm->tm);

clock_settime(CLOCK_REALTIME, &tspec);
zsw_clock_set_timezone(retained.timezone);
#endif
}

Expand All @@ -109,9 +101,6 @@ void zsw_clock_get_time(zsw_timeval_t *ztm)
memcpy(ztm, tm, sizeof(struct tm));
#endif

// Add one to the month because we want to count from December instead of January
ztm->tm.tm_mon += 1;

// Add 1900 to the year because we want to count from 1900
ztm->tm.tm_year += 1900;
}
Expand Down
32 changes: 22 additions & 10 deletions app/src/zsw_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,38 @@ typedef void (*zsw_clock_on_update)(void);

typedef struct {
#if CONFIG_RTC
struct rtc_time
tm; /**< Modified time object with 1900 added to the year and the month increased by one. */
struct rtc_time tm; /**< Modified time object with 1900 added to the year*/
#else
struct tm tm; /**< Modified time object with 1900 added to the year and the month increased by one. */
struct tm tm; /**< Modified time object with 1900 added to the year*/
#endif
uint32_t tv_usec;
} zsw_timeval_t;

/** @brief
* NOTE: This function needs the time as seconds
* @param ztm

/**
* Sets the time of the clock.
*
* @param ztm A pointer to a `zsw_timeval_t` structure representing the desired time.
*/
void zsw_clock_set_time(zsw_timeval_t *ztm);

/** @brief
* @param ztm

/**
* @brief Retrieves the current time from the.
*
* This function retrieves the current time from the clock and stores it in the provided zsw_timeval_t structure.
*
* @param ztm Pointer to the zsw_timeval_t structure where the current time will be stored.
*/
void zsw_clock_get_time(zsw_timeval_t *ztm);

/** @brief
* @param tz

/**
* @brief Sets the timezone for the ZSW clock.
*
* This function sets the timezone for the clock. The timezone is specified
* using the provided string in setenv and tzset format.
*
* @param tz The timezone string to set.
*/
void zsw_clock_set_timezone(char *tz);

0 comments on commit 50f6aed

Please sign in to comment.