|
5 | 5 | *
|
6 | 6 | * Copyright (c) 2017 "Eric Poulsen" <[email protected]>
|
7 | 7 | * Copyright (c) 2017 "Tom Manning" <[email protected]>
|
8 |
| - * Copyright (c) 2024 M5Stack Technology CO LTD |
9 | 8 | *
|
10 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | 10 | * of this software and associated documentation files (the "Software"), to deal
|
|
37 | 36 | #include "py/obj.h"
|
38 | 37 | #include "py/runtime.h"
|
39 | 38 | #include "py/mphal.h"
|
| 39 | +#include "extmod/modmachine.h" |
40 | 40 | #include "shared/timeutils/timeutils.h"
|
41 |
| -#include "modmachine.h" |
42 | 41 | #include "machine_rtc.h"
|
43 | 42 | #include "uiflow_utility.h"
|
44 |
| -#include "extmod/modmachine.h" |
45 | 43 |
|
46 | 44 | typedef struct _machine_rtc_obj_t {
|
47 | 45 | 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
|
109 | 107 | // Get time
|
110 | 108 |
|
111 | 109 | struct timeval tv;
|
112 |
| - |
113 | 110 | gettimeofday(&tv, NULL);
|
114 |
| - timeutils_struct_time_t tm; |
115 | 111 |
|
116 |
| - timeutils_seconds_since_epoch_to_struct_time(tv.tv_sec, &tm); |
| 112 | + struct tm tm; |
| 113 | + gmtime_r(&tv.tv_sec, &tm); |
117 | 114 |
|
118 | 115 | 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), |
121 | 118 | mp_obj_new_int(tm.tm_mday),
|
122 | 119 | mp_obj_new_int(tm.tm_wday),
|
123 | 120 | 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,
|
204 | 201 |
|
205 | 202 | static mp_obj_t machine_rtc_timezone(size_t n_args, const mp_obj_t *args) {
|
206 | 203 | if (n_args == 1) {
|
| 204 | + // Get timezone |
207 | 205 | char *tz = getenv("TZ");
|
208 | 206 | if (tz == NULL) {
|
209 | 207 | return mp_const_none;
|
210 | 208 | } else {
|
211 |
| - char *ptr = strchr(tz, '+'); |
| 209 | + char timezone[64] = { 0 }; |
| 210 | + memcpy(timezone, tz, strlen(tz)); |
| 211 | + char *ptr = strchr(timezone, '+'); |
212 | 212 | if (ptr != NULL) {
|
213 | 213 | *ptr = '-';
|
214 | 214 | } else {
|
215 |
| - ptr = strchr(tz, '-'); |
| 215 | + ptr = strchr(timezone, '-'); |
216 | 216 | if (ptr != NULL) {
|
217 | 217 | *ptr = '+';
|
218 | 218 | }
|
219 | 219 | }
|
220 |
| - return mp_obj_new_str(tz, strlen(tz)); |
| 220 | + return mp_obj_new_str(timezone, strlen(timezone)); |
221 | 221 | }
|
222 | 222 | } 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])); |
225 | 226 |
|
226 |
| - char *ptr = strchr(tz, '-'); |
| 227 | + char *ptr = strchr(timezone, '-'); |
227 | 228 | if (ptr != NULL) {
|
228 | 229 | *ptr = '+';
|
229 | 230 | } else {
|
230 |
| - ptr = strchr(tz, '+'); |
| 231 | + ptr = strchr(timezone, '+'); |
231 | 232 | if (ptr != NULL) {
|
232 | 233 | *ptr = '-';
|
233 | 234 | }
|
234 | 235 | }
|
235 | 236 |
|
236 |
| - setenv("TZ", tz, 1); |
| 237 | + setenv("TZ", timezone, 1); |
237 | 238 | tzset();
|
238 | 239 |
|
239 |
| - nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", tz); |
| 240 | + nvs_write_str_helper(UIFLOW_NVS_NAMESPACE, "tz", timezone); |
240 | 241 | return mp_const_none;
|
241 | 242 | }
|
242 | 243 | }
|
|
0 commit comments