Skip to content

Commit 1db4253

Browse files
atxdpgeorge
authored andcommitted
lib: Move time utility functions to common library.
1 parent 800d5cd commit 1db4253

12 files changed

Lines changed: 262 additions & 399 deletions

File tree

cc3200/application.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ APP_INC += -I$(BUILD)/genhdr
2121
APP_INC += -I../lib/fatfs
2222
APP_INC += -I../lib/mp-readline
2323
APP_INC += -I../lib/netutils
24+
APP_INC += -I../lib/timeutils
2425
APP_INC += -I../stmhal
2526

2627
APP_CPPDEFINES = -Dgcc -DTARGET_IS_CC3200 -DSL_FULL -DUSE_FREERTOS
@@ -144,6 +145,7 @@ APP_LIB_SRC_C = $(addprefix lib/,\
144145
libc/string0.c \
145146
mp-readline/readline.c \
146147
netutils/netutils.c \
148+
timeutils/timeutils.c \
147149
)
148150

149151
APP_STM_SRC_C = $(addprefix stmhal/,\

cc3200/fatfs/src/diskio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
#if MICROPY_HW_HAS_SDCARD
1616
#include "sd_diskio.h" /* SDCARD disk IO API */
1717
#endif
18-
#include "modutime.h"
1918
#include "inc/hw_types.h"
2019
#include "inc/hw_ints.h"
2120
#include "inc/hw_memmap.h"
2221
#include "rom_map.h"
2322
#include "prcm.h"
23+
#include "timeutils.h"
2424

2525
/* Definitions of physical drive number for each drive */
2626
#define SFLASH 0 /* Map SFLASH drive to drive number 0 */
@@ -192,13 +192,13 @@ DWORD get_fattime (
192192
void
193193
)
194194
{
195-
mod_struct_time tm;
195+
timeutils_struct_time_t tm;
196196
uint32_t seconds;
197197
uint16_t mseconds;
198198

199199
// Get the time from the on-chip RTC and convert it to struct_time
200200
MAP_PRCMRTCGet(&seconds, &mseconds);
201-
mod_time_seconds_since_2000_to_struct_time(seconds, &tm);
201+
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
202202

203203
return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) |
204204
((tm.tm_mday) << 16) | ((tm.tm_hour) << 11) |

cc3200/ftp/ftp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "ftp.h"
4141
#include "simplelink.h"
4242
#include "modwlan.h"
43-
#include "modutime.h"
4443
#include "debug.h"
4544
#include "serverstask.h"
4645
#include "ff.h"
@@ -49,6 +48,7 @@
4948
#include "diskio.h"
5049
#include "sd_diskio.h"
5150
#include "updater.h"
51+
#include "timeutils.h"
5252

5353

5454
/******************************************************************************
@@ -884,7 +884,7 @@ static int ftp_print_eplf_item (char *dest, uint32_t destsize, FILINFO *fno) {
884884
uint16_t mseconds;
885885
uint mindex = (((fno->fdate >> 5) & 0x0f) > 0) ? (((fno->fdate >> 5) & 0x0f) - 1) : 0;
886886
uint day = ((fno->fdate & 0x1f) > 0) ? (fno->fdate & 0x1f) : 1;
887-
uint fseconds = mod_time_seconds_since_2000(1980 + ((fno->fdate >> 9) & 0x7f),
887+
uint fseconds = timeutils_seconds_since_2000(1980 + ((fno->fdate >> 9) & 0x7f),
888888
(fno->fdate >> 5) & 0x0f,
889889
fno->fdate & 0x1f,
890890
(fno->ftime >> 11) & 0x1f,
@@ -912,12 +912,12 @@ static int ftp_print_eplf_item (char *dest, uint32_t destsize, FILINFO *fno) {
912912
}
913913

914914
static int ftp_print_eplf_drive (char *dest, uint32_t destsize, char *name) {
915-
mod_struct_time tm;
915+
timeutils_struct_time_t tm;
916916
uint32_t tseconds;
917917
uint16_t mseconds;
918918
char *type = "d";
919919

920-
mod_time_seconds_since_2000_to_struct_time((FTP_UNIX_TIME_20150101 - FTP_UNIX_TIME_20000101), &tm);
920+
timeutils_seconds_since_2000_to_struct_time((FTP_UNIX_TIME_20150101 - FTP_UNIX_TIME_20000101), &tm);
921921

922922
MAP_PRCMRTCGet(&tseconds, &mseconds);
923923
if (FTP_UNIX_SECONDS_180_DAYS < tseconds - (FTP_UNIX_TIME_20150101 - FTP_UNIX_TIME_20000101)) {

cc3200/mods/moduos.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
#include "diskio.h"
3939
#include "sflash_diskio.h"
4040
#include "file.h"
41-
#include "modutime.h"
4241
#include "random.h"
4342
#include "sd_diskio.h"
4443
#include "mpexception.h"
4544
#include "version.h"
45+
#include "timeutils.h"
4646

4747
/// \module os - basic "operating system" services
4848
///
@@ -285,7 +285,7 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
285285
} else {
286286
mode |= 0x8000; // stat.S_IFREG
287287
}
288-
mp_int_t seconds = mod_time_seconds_since_2000(
288+
mp_int_t seconds = timeutils_seconds_since_2000(
289289
1980 + ((fno.fdate >> 9) & 0x7f),
290290
(fno.fdate >> 5) & 0x0f,
291291
fno.fdate & 0x1f,

cc3200/mods/modutime.c

Lines changed: 8 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include MICROPY_HAL_H
3333
#include "py/nlr.h"
3434
#include "py/obj.h"
35-
#include "modutime.h"
35+
#include "timeutils.h"
3636
#include "inc/hw_types.h"
3737
#include "inc/hw_ints.h"
3838
#include "inc/hw_memmap.h"
@@ -41,126 +41,11 @@
4141
#include "pybrtc.h"
4242
#include "mpexception.h"
4343

44-
45-
// LEAPOCH corresponds to 2000-03-01, which is a mod-400 year, immediately
46-
// after Feb 29. We calculate seconds as a signed integer relative to that.
47-
//
48-
// Our timebase is relative to 2000-01-01.
49-
50-
#define LEAPOCH ((31 + 29) * 86400)
51-
52-
#define DAYS_PER_400Y (365*400 + 97)
53-
#define DAYS_PER_100Y (365*100 + 24)
54-
#define DAYS_PER_4Y (365*4 + 1)
55-
56-
5744
/// \module time - time related functions
5845
///
5946
/// The `time` module provides functions for getting the current time and date,
6047
/// and for sleeping.
6148

62-
STATIC const uint16_t days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
63-
64-
STATIC bool is_leap_year(mp_uint_t year) {
65-
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
66-
}
67-
68-
// Month is one based
69-
STATIC mp_uint_t mod_time_days_in_month(mp_uint_t year, mp_uint_t month) {
70-
mp_uint_t mdays = days_since_jan1[month] - days_since_jan1[month - 1];
71-
if (month == 2 && is_leap_year(year)) {
72-
mdays++;
73-
}
74-
return mdays;
75-
}
76-
77-
// compute the day of the year, between 1 and 366
78-
// month should be between 1 and 12, date should start at 1
79-
STATIC mp_uint_t mod_time_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date) {
80-
mp_uint_t yday = days_since_jan1[month - 1] + date;
81-
if (month >= 3 && is_leap_year(year)) {
82-
yday += 1;
83-
}
84-
return yday;
85-
}
86-
87-
// returns the number of seconds, as an integer, since 2000-01-01
88-
mp_uint_t mod_time_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
89-
return
90-
second
91-
+ minute * 60
92-
+ hour * 3600
93-
+ (mod_time_year_day(year, month, date) - 1
94-
+ ((year - 2000 + 3) / 4) // add a day each 4 years starting with 2001
95-
- ((year - 2000 + 99) / 100) // subtract a day each 100 years starting with 2001
96-
+ ((year - 2000 + 399) / 400) // add a day each 400 years starting with 2001
97-
) * 86400
98-
+ (year - 2000) * 31536000;
99-
}
100-
101-
void mod_time_seconds_since_2000_to_struct_time(mp_uint_t t, mod_struct_time *tm) {
102-
// The following algorithm was adapted from musl's __secs_to_tm and adapted
103-
// for differences in Micro Python's timebase.
104-
105-
mp_int_t seconds = t - LEAPOCH;
106-
107-
mp_int_t days = seconds / 86400;
108-
seconds %= 86400;
109-
tm->tm_hour = seconds / 3600;
110-
tm->tm_min = seconds / 60 % 60;
111-
tm->tm_sec = seconds % 60;
112-
113-
mp_int_t wday = (days + 2) % 7; // Mar 1, 2000 was a Wednesday (2)
114-
if (wday < 0) {
115-
wday += 7;
116-
}
117-
tm->tm_wday = wday;
118-
119-
mp_int_t qc_cycles = days / DAYS_PER_400Y;
120-
days %= DAYS_PER_400Y;
121-
if (days < 0) {
122-
days += DAYS_PER_400Y;
123-
qc_cycles--;
124-
}
125-
mp_int_t c_cycles = days / DAYS_PER_100Y;
126-
if (c_cycles == 4) {
127-
c_cycles--;
128-
}
129-
days -= (c_cycles * DAYS_PER_100Y);
130-
131-
mp_int_t q_cycles = days / DAYS_PER_4Y;
132-
if (q_cycles == 25) {
133-
q_cycles--;
134-
}
135-
days -= q_cycles * DAYS_PER_4Y;
136-
137-
mp_int_t years = days / 365;
138-
if (years == 4) {
139-
years--;
140-
}
141-
days -= (years * 365);
142-
143-
tm->tm_year = 2000 + years + 4 * q_cycles + 100 * c_cycles + 400 * qc_cycles;
144-
145-
// Note: days_in_month[0] corresponds to March
146-
STATIC const int8_t days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29};
147-
148-
mp_int_t month;
149-
for (month = 0; days_in_month[month] <= days; month++) {
150-
days -= days_in_month[month];
151-
}
152-
153-
tm->tm_mon = month + 2;
154-
if (tm->tm_mon >= 12) {
155-
tm->tm_mon -= 12;
156-
tm->tm_year++;
157-
}
158-
tm->tm_mday = days + 1; // Make one based
159-
tm->tm_mon++; // Make one based
160-
161-
tm->tm_yday = mod_time_year_day(tm->tm_year, tm->tm_mon, tm->tm_mday);
162-
}
163-
16449
/// \function localtime([secs])
16550
/// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
16651
/// contains: (year, month, mday, hour, minute, second, weekday, yearday)
@@ -175,14 +60,14 @@ void mod_time_seconds_since_2000_to_struct_time(mp_uint_t t, mod_struct_time *tm
17560
/// yearday is 1-366
17661
STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) {
17762
if (n_args == 0 || args[0] == mp_const_none) {
178-
mod_struct_time tm;
63+
timeutils_struct_time_t tm;
17964
uint32_t seconds;
18065
uint16_t mseconds;
18166

18267
// get the seconds and the milliseconds from the RTC
18368
MAP_PRCMRTCGet(&seconds, &mseconds);
18469
mseconds = RTC_CYCLES_U16MS(mseconds);
185-
mod_time_seconds_since_2000_to_struct_time(seconds, &tm);
70+
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
18671

18772
mp_obj_t tuple[8] = {
18873
mp_obj_new_int(tm.tm_year),
@@ -197,8 +82,8 @@ STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) {
19782
return mp_obj_new_tuple(8, tuple);
19883
} else {
19984
mp_int_t seconds = mp_obj_get_int(args[0]);
200-
mod_struct_time tm;
201-
mod_time_seconds_since_2000_to_struct_time(seconds, &tm);
85+
timeutils_struct_time_t tm;
86+
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
20287
mp_obj_t tuple[8] = {
20388
tuple[0] = mp_obj_new_int(tm.tm_year),
20489
tuple[1] = mp_obj_new_int(tm.tm_mon),
@@ -231,64 +116,10 @@ STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
231116
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
232117
}
233118

234-
mp_int_t year = mp_obj_get_int(elem[0]);
235-
mp_int_t month = mp_obj_get_int(elem[1]);
236-
mp_int_t mday = mp_obj_get_int(elem[2]);
237-
mp_int_t hours = mp_obj_get_int(elem[3]);
238-
mp_int_t minutes = mp_obj_get_int(elem[4]);
239-
mp_int_t seconds = mp_obj_get_int(elem[5]);
119+
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
120+
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
121+
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
240122

241-
// Normalize the tuple. This allows things like:
242-
//
243-
// tm_tomorrow = list(time.localtime())
244-
// tm_tomorrow[2] += 1 # Adds 1 to mday
245-
// tomorrow = time.mktime(tm_tommorrow)
246-
//
247-
// And not have to worry about all the weird overflows.
248-
//
249-
// You can subtract dates/times this way as well.
250-
251-
minutes += seconds / 60;
252-
if ((seconds = seconds % 60) < 0) {
253-
seconds += 60;
254-
minutes--;
255-
}
256-
257-
hours += minutes / 60;
258-
if ((minutes = minutes % 60) < 0) {
259-
minutes += 60;
260-
hours--;
261-
}
262-
263-
mday += hours / 24;
264-
if ((hours = hours % 24) < 0) {
265-
hours += 24;
266-
mday--;
267-
}
268-
269-
month--; // make month zero based
270-
year += month / 12;
271-
if ((month = month % 12) < 0) {
272-
month += 12;
273-
year--;
274-
}
275-
month++; // back to one based
276-
277-
while (mday < 1) {
278-
if (--month == 0) {
279-
month = 12;
280-
year--;
281-
}
282-
mday += mod_time_days_in_month(year, month);
283-
}
284-
while (mday > mod_time_days_in_month(year, month)) {
285-
mday -= mod_time_days_in_month(year, month);
286-
if (++month == 13) {
287-
month = 1;
288-
year++;
289-
}
290-
}
291-
return mp_obj_new_int_from_uint(mod_time_seconds_since_2000(year, month, mday, hours, minutes, seconds));
292123
}
293124
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
294125

cc3200/mods/pybrtc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include MICROPY_HAL_H
3232
#include "py/obj.h"
3333
#include "py/runtime.h"
34-
#include "modutime.h"
3534
#include "inc/hw_types.h"
3635
#include "inc/hw_ints.h"
3736
#include "inc/hw_memmap.h"
@@ -40,6 +39,7 @@
4039
#include "pybrtc.h"
4140
#include "pybsleep.h"
4241
#include "mpcallback.h"
42+
#include "timeutils.h"
4343

4444
/// \moduleref pyb
4545
/// \class RTC - real time clock
@@ -82,7 +82,7 @@ void pybrtc_init(void) {
8282
// fresh reset; configure the RTC Calendar
8383
// set the date to 1st Jan 2015
8484
// set the time to 00:00:00
85-
uint32_t seconds = mod_time_seconds_since_2000(2015, 1, 1, 0, 0, 0);
85+
uint32_t seconds = timeutils_seconds_since_2000(2015, 1, 1, 0, 0, 0);
8686

8787
// Mark the RTC in use first
8888
MAP_PRCMRTCInUseSet();
@@ -137,15 +137,15 @@ STATIC void pyb_rtc_callback_disable (mp_obj_t self_in) {
137137
/// `weekday` is 0-6 for Monday through Sunday.
138138
///
139139
mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
140-
mod_struct_time tm;
140+
timeutils_struct_time_t tm;
141141
uint32_t seconds;
142142
uint16_t mseconds;
143143

144144
if (n_args == 1) {
145145
// get the seconds and the milliseconds from the RTC
146146
MAP_PRCMRTCGet(&seconds, &mseconds);
147147
mseconds = RTC_CYCLES_U16MS(mseconds);
148-
mod_time_seconds_since_2000_to_struct_time(seconds, &tm);
148+
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
149149

150150
mp_obj_t tuple[8] = {
151151
mp_obj_new_int(tm.tm_year),
@@ -172,7 +172,7 @@ mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
172172
tm.tm_sec = mp_obj_get_int(items[6]);
173173
mseconds = mp_obj_get_int(items[7]);
174174

175-
seconds = mod_time_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
175+
seconds = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
176176
mseconds = RTC_U16MS_CYCLES(mseconds);
177177
MAP_PRCMRTCSet(seconds, mseconds);
178178

0 commit comments

Comments
 (0)