Skip to content

Commit 95b8ce4

Browse files
committed
machine_rtc.c: Fix the time zone inversion issue.
For example, to set the Eastern Time Zone, please use GMT+8 or UTC+8. Signed-off-by: lbuque <[email protected]>
1 parent c115948 commit 95b8ce4

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

docs/en/system/time.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Functions
100100

101101
This is inverse function of localtime. It's argument is a full 8-tuple
102102
which expresses a time as per localtime. It returns an integer which is
103-
the number of seconds since Jan 1, 2000.
103+
the number of seconds since Jan 1, 1970.
104104

105105
UIFLOW2:
106106

m5stack/machine_rtc.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,31 @@ static mp_obj_t machine_rtc_timezone(size_t n_args, const mp_obj_t *args) {
208208
if (tz == NULL) {
209209
return mp_const_none;
210210
} else {
211+
char *ptr = strchr(tz, '+');
212+
if (ptr != NULL) {
213+
*ptr = '-';
214+
} else {
215+
ptr = strchr(tz, '-');
216+
if (ptr != NULL) {
217+
*ptr = '+';
218+
}
219+
}
211220
return mp_obj_new_str(tz, strlen(tz));
212221
}
213222
} else {
214-
char tz[64];
215-
snprintf(tz, sizeof(tz), "%s", mp_obj_str_get_str(args[1]));
223+
char tz[64] = { 0 };
224+
snprintf(tz, sizeof(tz), "%s", mp_obj_str_get_str(args[0]));
225+
226+
char *ptr = strchr(tz, '-');
227+
if (ptr != NULL) {
228+
*ptr = '+';
229+
} else {
230+
ptr = strchr(tz, '+');
231+
if (ptr != NULL) {
232+
*ptr = '-';
233+
}
234+
}
235+
216236
setenv("TZ", tz, 1);
217237
tzset();
218238

m5stack/patches/0006-modtime-add-timezone-method.patch

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Index: micropython/ports/esp32/modtime.c
118118
struct timeval tv;
119119
gettimeofday(&tv, NULL);
120120
timeutils_struct_time_t tm;
121-
@@ -56,3 +95,36 @@ static mp_obj_t mp_time_time_get(void) {
121+
@@ -56,3 +95,46 @@ static mp_obj_t mp_time_time_get(void) {
122122
gettimeofday(&tv, NULL);
123123
return mp_obj_new_int(tv.tv_sec);
124124
}
@@ -129,23 +129,33 @@ Index: micropython/ports/esp32/modtime.c
129129
+ if (tz == NULL) {
130130
+ return mp_const_none;
131131
+ } else {
132+
+ char *ptr = strchr(tz, '+');
133+
+ if (ptr != NULL) {
134+
+ *ptr = '-';
135+
+ } else {
136+
+ ptr = strchr(tz, '-');
137+
+ if (ptr != NULL) {
138+
+ *ptr = '+';
139+
+ }
140+
+ }
132141
+ return mp_obj_new_str(tz, strlen(tz));
133142
+ }
134143
+ } else {
135-
+ char tz[64];
144+
+ char tz[64] = { 0 };
136145
+ snprintf(tz, sizeof(tz), "%s", mp_obj_str_get_str(args[0]));
137-
+ setenv("TZ", tz, 1);
138-
+ tzset();
139146
+
140-
+ time_t now;
141-
+ char strftime_buf[64];
142-
+ struct tm timeinfo;
143-
+
144-
+ time(&now);
147+
+ char *ptr = strchr(tz, '-');
148+
+ if (ptr != NULL) {
149+
+ *ptr = '+';
150+
+ } else {
151+
+ ptr = strchr(tz, '+');
152+
+ if (ptr != NULL) {
153+
+ *ptr = '-';
154+
+ }
155+
+ }
145156
+
146-
+ localtime_r(&now, &timeinfo);
147-
+ strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
148-
+ ESP_LOGE("time", "The current date/time in Shanghai is: %s", strftime_buf);
157+
+ setenv("TZ", tz, 1);
158+
+ tzset();
149159
+
150160
+ nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", tz);
151161
+ return mp_const_none;

0 commit comments

Comments
 (0)