Skip to content

Commit 793b9d8

Browse files
committed
machine_rtc.c: Fix timezone and time reading errors.
Signed-off-by: lbuque <[email protected]>
1 parent cec6531 commit 793b9d8

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

m5stack/machine_rtc.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*
66
* Copyright (c) 2017 "Eric Poulsen" <[email protected]>
77
* Copyright (c) 2017 "Tom Manning" <[email protected]>
8-
* Copyright (c) 2024 M5Stack Technology CO LTD
98
*
109
* Permission is hereby granted, free of charge, to any person obtaining a copy
1110
* of this software and associated documentation files (the "Software"), to deal
@@ -37,11 +36,10 @@
3736
#include "py/obj.h"
3837
#include "py/runtime.h"
3938
#include "py/mphal.h"
39+
#include "extmod/modmachine.h"
4040
#include "shared/timeutils/timeutils.h"
41-
#include "modmachine.h"
4241
#include "machine_rtc.h"
4342
#include "uiflow_utility.h"
44-
#include "extmod/modmachine.h"
4543

4644
typedef struct _machine_rtc_obj_t {
4745
mp_obj_base_t base;
@@ -109,15 +107,14 @@ static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *ar
109107
// Get time
110108

111109
struct timeval tv;
112-
113110
gettimeofday(&tv, NULL);
114-
timeutils_struct_time_t tm;
115111

116-
timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, &tm);
112+
struct tm tm;
113+
gmtime_r(&tv.tv_sec, &tm);
117114

118115
mp_obj_t tuple[8] = {
119-
mp_obj_new_int(tm.tm_year),
120-
mp_obj_new_int(tm.tm_mon),
116+
mp_obj_new_int(tm.tm_year + 1900),
117+
mp_obj_new_int(tm.tm_mon + 1),
121118
mp_obj_new_int(tm.tm_mday),
122119
mp_obj_new_int(tm.tm_wday),
123120
mp_obj_new_int(tm.tm_hour),
@@ -204,39 +201,43 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_local_datetime_obj, 1, 2,
204201

205202
static mp_obj_t machine_rtc_timezone(size_t n_args, const mp_obj_t *args) {
206203
if (n_args == 1) {
204+
// Get timezone
207205
char *tz = getenv("TZ");
208206
if (tz == NULL) {
209207
return mp_const_none;
210208
} else {
211-
char *ptr = strchr(tz, '+');
209+
char timezone[64] = { 0 };
210+
memcpy(timezone, tz, strlen(tz));
211+
char *ptr = strchr(timezone, '+');
212212
if (ptr != NULL) {
213213
*ptr = '-';
214214
} else {
215-
ptr = strchr(tz, '-');
215+
ptr = strchr(timezone, '-');
216216
if (ptr != NULL) {
217217
*ptr = '+';
218218
}
219219
}
220-
return mp_obj_new_str(tz, strlen(tz));
220+
return mp_obj_new_str(timezone, strlen(timezone));
221221
}
222222
} else {
223-
char tz[64] = { 0 };
224-
snprintf(tz, sizeof(tz), "%s", mp_obj_str_get_str(args[0]));
223+
// Set timezone
224+
char timezone[64] = { 0 };
225+
snprintf(timezone, sizeof(timezone) - 1, "%s", mp_obj_str_get_str(args[1]));
225226

226-
char *ptr = strchr(tz, '-');
227+
char *ptr = strchr(timezone, '-');
227228
if (ptr != NULL) {
228229
*ptr = '+';
229230
} else {
230-
ptr = strchr(tz, '+');
231+
ptr = strchr(timezone, '+');
231232
if (ptr != NULL) {
232233
*ptr = '-';
233234
}
234235
}
235236

236-
setenv("TZ", tz, 1);
237+
setenv("TZ", timezone, 1);
237238
tzset();
238239

239-
nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", tz);
240+
nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", timezone);
240241
return mp_const_none;
241242
}
242243
}

0 commit comments

Comments
 (0)