diff --git a/package/PikaStdDevice/PikaStdDevice.pyi b/package/PikaStdDevice/PikaStdDevice.pyi index 7b7585c4f..f24daf145 100644 --- a/package/PikaStdDevice/PikaStdDevice.pyi +++ b/package/PikaStdDevice/PikaStdDevice.pyi @@ -98,49 +98,8 @@ class GPIO(BaseDev): def platformRead(self): ... -class Time(BaseDev): - def __init__(self): ... - - def sleep(self, s: float): - """Sleep for s seconds.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def time(self) -> float: - """Get the current time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def time_ns(self) -> int: - """Get the current time in nanoseconds.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def gmtime(self, unix_time: float): - """Convert unix time to struct_time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def localtime(self, unix_time: float): - """Convert unix time to struct_time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def mktime(self) -> int: - """Convert struct_time to unix time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def asctime(self): - """Convert struct_time to string.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def ctime(self, unix_time: float): - """Convert unix time to string.""" - - @abstractmethod - def sleep_s(self, s: int): ... - - @abstractmethod - def sleep_ms(self, ms: int): ... - - @abstractmethod - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def platformGetTick(): ... +# class Time(BaseDev): + # use time module instead class ADC(BaseDev): diff --git a/package/PikaStdDevice/PikaStdDevice_Time.c b/package/time/_time.c similarity index 67% rename from package/PikaStdDevice/PikaStdDevice_Time.c rename to package/time/_time.c index f0c3dd7ca..9aff69382 100644 --- a/package/PikaStdDevice/PikaStdDevice_Time.c +++ b/package/time/_time.c @@ -1,5 +1,5 @@ -#include "PikaStdDevice_Time.h" -#include "PikaStdDevice_common.h" +#include "_time.h" +#include "PikaVM.h" #if defined(__linux) #include #endif @@ -7,7 +7,7 @@ #include #endif -void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) { +void _time_sleep_ms(PikaObj* self, int ms) { #if defined(__linux) usleep(ms * 1000); #elif defined(_WIN32) @@ -16,7 +16,7 @@ void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) { __platform_sleep_ms(ms); #endif } -void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) { +void _time_sleep_s(PikaObj* self, int s) { #if defined(__linux) sleep(s); #elif defined(_WIN32) @@ -26,7 +26,7 @@ void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) { #endif } -void PikaStdDevice_Time_platformGetTick(PikaObj* self) { +void _time_platformGetTick(PikaObj* self) { obj_setInt(self, "tick", __platform_getTick()); } @@ -39,8 +39,8 @@ void PikaStdDevice_Time_platformGetTick(PikaObj* self) { #include "stdint.h" #include "stdio.h" -//结构体时间类型定义(来源c标准库corect_wtime.h) -//无论是16位整数还是32位整数都满足需求 +// 结构体时间类型定义(来源c标准库corect_wtime.h) +// 无论是16位整数还是32位整数都满足需求 typedef struct _tm { int tm_sec; // seconds after the minute - [0, 60] including leap second int tm_min; // minutes after the hour - [0, 59] @@ -53,16 +53,16 @@ typedef struct _tm { int tm_isdst; // daylight savings time flag } _tm; -//时间戳时间类型定义(来源c标准库time.h) -//直接支持64位秒数时间,附加时间精度为ns,根据设备决定,需要1GHz及以上时钟频率才能支持1ns级别时间精度 -//内部时间比对数据类型,传递给外界的时候会使用浮点数,所以精度会降低 -//但内部使用复合数据类型比对,以实现平台支持的最小时间精度比较 +// 时间戳时间类型定义(来源c标准库time.h) +// 直接支持64位秒数时间,附加时间精度为ns,根据设备决定,需要1GHz及以上时钟频率才能支持1ns级别时间精度 +// 内部时间比对数据类型,传递给外界的时候会使用浮点数,所以精度会降低 +// 但内部使用复合数据类型比对,以实现平台支持的最小时间精度比较 typedef struct { int64_t tv_sec; // Seconds - >= 0 int32_t tv_nsec; // Nanoseconds - [0, 999999999] } _timespec; -//错误处理 +// 错误处理 typedef int status; #define TIME_OK 0 @@ -74,9 +74,9 @@ typedef int status; #define TIME_LESS_THAN_1970 5 #define TIME_ERROR_STRUCT_TIME 6 -//错误状态处理函数 +// 错误状态处理函数 void status_deal(status s) { -//输出异常信息 +// 输出异常信息 #define time_printf(...) __platform_printf(__VA_ARGS__) time_printf("\n[Error-info]Checking a exception : "); @@ -108,59 +108,59 @@ void status_deal(status s) { time_printf("\n"); } -//获取硬件平台的Unix时间戳,时间精度为1s级别, +// 获取硬件平台的Unix时间戳,时间精度为1s级别, status time_get_unix_time(PikaObj* self, _timespec* this_timespec) { this_timespec->tv_sec = (int64_t)(obj_getInt(self, "tick") / 1000); return TIME_OK; } -//获取硬件平台的Tick时间,时间精度为1s级别以下 -//即1s的小数部分 +// 获取硬件平台的Tick时间,时间精度为1s级别以下 +// 即1s的小数部分 status time_get_tick_ns(PikaObj* self, _timespec* this_timespec) { this_timespec->tv_nsec = (obj_getInt(self, "tick") % 1000) * 1000000; return TIME_OK; } -//标准time()方法,返回以浮点数表示的从 epoch 开始的秒数的时间值。 -// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), +// 标准time()方法,返回以浮点数表示的从 epoch 开始的秒数的时间值。 +// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), pika_float time_time(PikaObj* self) { - status res = 0; //状态响应 + status res = 0; // 状态响应 _timespec temp_timespec = {0}; - //调用硬件平台函数,获取当前时间 + // 调用硬件平台函数,获取当前时间 res = time_get_unix_time(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 + } // 异常处理 res = time_get_tick_ns(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 - //以浮点数返回时间,float + } // 异常处理 + // 以浮点数返回时间,float return temp_timespec.tv_sec + (pika_float)temp_timespec.tv_nsec / 1000000000; } -//标准time_ns()方法,返回以整数表示的从 epoch 开始的纳秒数的时间值。 -// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), +// 标准time_ns()方法,返回以整数表示的从 epoch 开始的纳秒数的时间值。 +// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), int64_t time_time_ns(PikaObj* self) { - status res = 0; //状态响应 + status res = 0; // 状态响应 _timespec temp_timespec = {0}; - //调用硬件平台函数,获取当前时间 + // 调用硬件平台函数,获取当前时间 res = time_get_unix_time(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 + } // 异常处理 res = time_get_tick_ns(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 - //以浮点数返回时间,float + } // 异常处理 + // 以浮点数返回时间,float return temp_timespec.tv_sec * 1000000000 + temp_timespec.tv_nsec; } -//利用基姆拉尔森计算公式计算星期 +// 利用基姆拉尔森计算公式计算星期 int time_get_week(const _tm* this_tm) { - //月份要+1 + // 月份要+1 int month = this_tm->tm_mon + 1; int year = this_tm->tm_year; int day = this_tm->tm_mday; @@ -179,16 +179,16 @@ int time_get_week(const _tm* this_tm) { return w; } -//由Unix时间戳计算标准UTC时间 +// 由Unix时间戳计算标准UTC时间 status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { int32_t total_day; int32_t extra_second; int year_400, year_100, year_4, year_1; - int february_offset, temp; //二月偏移量,零时变量 + int february_offset, temp; // 二月偏移量,零时变量 - //判断是否输入小于0的时间戳 + // 判断是否输入小于0的时间戳 if (unix_time < 0) { - //暂不支持小于0的时间戳 + // 暂不支持小于0的时间戳 return TIME_LESS_THAN_ZERO; } @@ -198,31 +198,31 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { total_day = unix_time / DAY_SECOND; extra_second = unix_time - total_day * DAY_SECOND; -//为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 -// 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 -// 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 -#define YEAR_START (1600) //初始年份 -#define DAY_OFFSET (135140) //时间偏移量 +// 为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 +// 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 +// 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 +#define YEAR_START (1600) // 初始年份 +#define DAY_OFFSET (135140) // 时间偏移量 total_day += DAY_OFFSET; -//从1600年到3200年有1600/4-(1600/100-1600/400)=388个闰年 -//即 MAX_DAY 1600*365+388=584388 day -#define MAX_DAY (584388) //最大可判断时间天数 +// 从1600年到3200年有1600/4-(1600/100-1600/400)=388个闰年 +// 即 MAX_DAY 1600*365+388=584388 day +#define MAX_DAY (584388) // 最大可判断时间天数 if (total_day > MAX_DAY) { - //超过3200年的换算暂不支持 + // 超过3200年的换算暂不支持 return TIME_OVER_3200; } else { -//从1600年开始,天数都要多减一天,因为1600年是闰年 -//但是由于日期不包含当天时间,即2月2号,实际是2月1号+时:分:秒 -//所以算出来的日期要加上一天 -//两者配合,无需加减 +// 从1600年开始,天数都要多减一天,因为1600年是闰年 +// 但是由于日期不包含当天时间,即2月2号,实际是2月1号+时:分:秒 +// 所以算出来的日期要加上一天 +// 两者配合,无需加减 -//从400年,100年,4年逐渐缩小范围 -// 400个公历年天数为365*400+97=146097天 -// 400年内的100个公历年天数为365*100+24=36524天 -// 100年内的4年365*4+1=1461天 +// 从400年,100年,4年逐渐缩小范围 +// 400个公历年天数为365*400+97=146097天 +// 400年内的100个公历年天数为365*100+24=36524天 +// 100年内的4年365*4+1=1461天 #define DAY_OF_400Y (146097) #define DAY_OF_100Y (36524) #define DAY_OF_4Y (1461) @@ -230,56 +230,56 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { // 400年也要注意,要实际401年才可 year_400 = (total_day - 366) / DAY_OF_400Y; total_day -= year_400 * DAY_OF_400Y; - //计算400年内的情况 + // 计算400年内的情况 year_100 = (total_day - 1) / DAY_OF_100Y; total_day -= year_100 * DAY_OF_100Y; - //计算100年内的情况,要到第二年的第一天才算,即365+1 + // 计算100年内的情况,要到第二年的第一天才算,即365+1 year_4 = (total_day - 366) / DAY_OF_4Y; - //计算4年,需要格外注意0-5-8年,才会计算一个闰年,因为它才包含了4这个闰年,但并不包含8 + // 计算4年,需要格外注意0-5-8年,才会计算一个闰年,因为它才包含了4这个闰年,但并不包含8 total_day -= year_4 * DAY_OF_4Y; - //计算4年内的情况 - //需要减去1天,因为当天是不存在的 - //需要注意闰年会多一天 - //所有闰年都放在这里来考虑,即只要当前是闰年,那么这里就会剩下第一年闰年和第四年闰年两种情况 + // 计算4年内的情况 + // 需要减去1天,因为当天是不存在的 + // 需要注意闰年会多一天 + // 所有闰年都放在这里来考虑,即只要当前是闰年,那么这里就会剩下第一年闰年和第四年闰年两种情况 if (year_100 == 4) { - //第一年是闰年,此时为400*n+1年内 + // 第一年是闰年,此时为400*n+1年内 year_1 = 0; february_offset = 1; } else if (total_day <= DAY_OF_1Y * 4) { // 100*n+(4,8,...96)+1年,都是从第二年算起,非闰年 - //非闰年,需要减去1天,因为当天是不存在的 + // 非闰年,需要减去1天,因为当天是不存在的 year_1 = (total_day - 1) / DAY_OF_1Y; total_day -= year_1 * DAY_OF_1Y; february_offset = 0; } else { - //第四年是闰年 + // 第四年是闰年 year_1 = 4; total_day -= year_1 * DAY_OF_1Y; february_offset = 1; } - //计算出当前年份 + // 计算出当前年份 this_tm->tm_year = (year_400 * 400 + year_100 * 100 + year_4 * 4 + year_1) + YEAR_START; - //保存一年的天数 + // 保存一年的天数 this_tm->tm_yday = total_day; - //剩下的天数为1年内的天数,直接计算月和日 - //根据当前是否为闰年设置二月偏移量是否为1 - //能被4整除且不被100整除或者能被400整除 + // 剩下的天数为1年内的天数,直接计算月和日 + // 根据当前是否为闰年设置二月偏移量是否为1 + // 能被4整除且不被100整除或者能被400整除 - //闰年需要减去一天再计算 + // 闰年需要减去一天再计算 total_day -= february_offset; - //使用二分法快速定位月份,使用平年计算,在月份确定到2月时,再考虑闰年 - //判断是否在1-6月里面 - // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 + // 使用二分法快速定位月份,使用平年计算,在月份确定到2月时,再考虑闰年 + // 判断是否在1-6月里面 + // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 if (total_day <= 181) { - //判断是否在1-3月里面 + // 判断是否在1-3月里面 if (total_day <= 90) { - //判断是否在1-2月里面 + // 判断是否在1-2月里面 if (total_day <= 59) { - total_day += february_offset; //去掉二月的偏置 + total_day += february_offset; // 去掉二月的偏置 if (total_day <= 31) { // 1月 temp = 0; @@ -296,7 +296,7 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } else { // 4-6月 total_day -= 90; - //是否在4月里面 + // 是否在4月里面 if (total_day <= 30) { // 4月 temp = 3; @@ -315,9 +315,9 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } } else { total_day -= 181; - //判断是否在7-9月里面 + // 判断是否在7-9月里面 if (total_day <= 92) { - //是否在7-8月 + // 是否在7-8月 if (total_day <= 62) { if (total_day <= 31) { // 7月 @@ -335,7 +335,7 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } else { // 10-12月 total_day -= 92; - //是否在10-11月 + // 是否在10-11月 if (total_day <= 61) { if (total_day <= 31) { // 10月 @@ -353,11 +353,11 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } } - //记录当前月份和天数 - this_tm->tm_mon = temp; //月份 [0,11] - this_tm->tm_mday = total_day; //天数 + // 记录当前月份和天数 + this_tm->tm_mon = temp; // 月份 [0,11] + this_tm->tm_mday = total_day; // 天数 - //利用额外秒数计算时-分-秒 + // 利用额外秒数计算时-分-秒 temp = extra_second / 3600; this_tm->tm_hour = temp; extra_second = extra_second - temp * 3600; @@ -368,70 +368,70 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { this_tm->tm_sec = extra_second; - //计算出当前日期的星期数 + // 计算出当前日期的星期数 this_tm->tm_wday = time_get_week(this_tm); - //夏令时不明 + // 夏令时不明 this_tm->tm_isdst = -1; } return TIME_OK; } -//由标准UTC时间生成Unix时间戳 +// 由标准UTC时间生成Unix时间戳 status utc_struct_time_to_unix_time(const _tm* this_tm, int64_t* unix_time) { int32_t total_day, total_leap_year, dyear; - int february_offset; //二月偏移量,零时变量 + int february_offset; // 二月偏移量,零时变量 // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - //每个月份对应前面所有月的天数 + // 每个月份对应前面所有月的天数 const int month_day[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; - //每天总秒数一定,将UTC时间(年月)转换成天数 - //为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 - // 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 - // 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 + // 每天总秒数一定,将UTC时间(年月)转换成天数 + // 为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 + // 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 + // 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 if (this_tm->tm_year < 1970) { - //暂不支持1970之前的时间 + // 暂不支持1970之前的时间 *unix_time = 0; return TIME_LESS_THAN_1970; } if (this_tm->tm_year >= 3200) { - //暂不支持3200及以后的时间 + // 暂不支持3200及以后的时间 *unix_time = 0; return TIME_OVER_3200; } - //计算总年数要去掉尾巴,如年数20年,那么实际应该4个闰年,因为20这一年没有包含在里面 - //要减去一年来算闰年次数 - //先计算到相对1600年的天数,再转换到1970年 + // 计算总年数要去掉尾巴,如年数20年,那么实际应该4个闰年,因为20这一年没有包含在里面 + // 要减去一年来算闰年次数 + // 先计算到相对1600年的天数,再转换到1970年 dyear = this_tm->tm_year - YEAR_START - 1; total_leap_year = dyear / 4 - (dyear / 100 - dyear / 400 - 1); - //恢复减去的一年 + // 恢复减去的一年 dyear += 1; total_day = dyear * 365 + total_leap_year; - //减去1970到1600的总天数 + // 减去1970到1600的总天数 total_day -= DAY_OFFSET; - //增加月和日的总天数 - //判断是否是闰年 - //能被4整除且不被100整除或者能被400整除 + // 增加月和日的总天数 + // 判断是否是闰年 + // 能被4整除且不被100整除或者能被400整除 if (((dyear % 4 == 0) && (dyear % 100 != 0)) || (dyear % 400 == 0)) { - //闰年 + // 闰年 february_offset = 1; } else { february_offset = 0; } - //计算含月和日的总天数,日期要减去当天 + // 计算含月和日的总天数,日期要减去当天 total_day += month_day[this_tm->tm_mon] + this_tm->tm_mday - 1; - //二月以上需要加上偏移量 + // 二月以上需要加上偏移量 if (this_tm->tm_mon > 1) { total_day += february_offset; } - //根据天数以及时分秒计算Unix时间戳 + // 根据天数以及时分秒计算Unix时间戳 *unix_time = (int64_t)total_day * DAY_SECOND + this_tm->tm_hour * 3600 + this_tm->tm_min * 60 + this_tm->tm_sec; @@ -447,41 +447,41 @@ void time_struct_format(const _tm* this_tm, char* str) { this_tm->tm_wday, this_tm->tm_yday, this_tm->tm_isdst); } -//标准库函数gmtime,将以自 epoch 开始的秒数表示的时间转换为 UTC 的 struct_time +// 标准库函数gmtime,将以自 epoch 开始的秒数表示的时间转换为 UTC 的 struct_time void time_gmtime(pika_float unix_time, _tm* this_tm) { status res; - //转化时间 + // 转化时间 res = unix_time_to_utc_struct_time(this_tm, (int64_t)unix_time); if (res) { - status_deal(res); //异常情况处理 - //返回默认值 - // note: 异常情况返回默认时间起始点 + status_deal(res); // 异常情况处理 + // 返回默认值 + // note: 异常情况返回默认时间起始点 unix_time_to_utc_struct_time(this_tm, (int64_t)0); } } -//标准库函数localtime,将以自 epoch 开始的秒数表示的时间转换为当地时间的 -// struct_time +// 标准库函数localtime,将以自 epoch 开始的秒数表示的时间转换为当地时间的 +// struct_time void time_localtime(pika_float unix_time, _tm* this_tm, int locale) { status res; int local_offset; - //获取本地时间偏移量(小时) + // 获取本地时间偏移量(小时) local_offset = locale * 60 * 60; - //转化时间 + // 转化时间 res = unix_time_to_utc_struct_time(this_tm, (int64_t)unix_time + local_offset); if (res) { - status_deal(res); //异常情况处理 - //这里处理的策略和标准库不同,标准库最初始的时间是1970-1-1,00:00:00,对于不同时区来说,其值是不一样的 - //但本函数是要求各时区的起始时间不超过1970-1-1,00:00:00,实际上UTC时间可以更前,可靠的最早时间可到1600年 - //对于西时区来说,时间会缺失 + status_deal(res); // 异常情况处理 + // 这里处理的策略和标准库不同,标准库最初始的时间是1970-1-1,00:00:00,对于不同时区来说,其值是不一样的 + // 但本函数是要求各时区的起始时间不超过1970-1-1,00:00:00,实际上UTC时间可以更前,可靠的最早时间可到1600年 + // 对于西时区来说,时间会缺失 unix_time_to_utc_struct_time(this_tm, (int64_t)0); } } -//检测结构体时间是否在合适的范围内,但不检查它的正确性 +// 检测结构体时间是否在合适的范围内,但不检查它的正确性 status time_check_struct_time(const _tm* this_tm) { if (this_tm->tm_sec < 0 || this_tm->tm_sec > 60) { return TIME_ERROR_STRUCT_TIME; @@ -507,47 +507,47 @@ status time_check_struct_time(const _tm* this_tm) { return TIME_OK; } -//标准库函数mktime(t),将当地时间的 -// struct_time转换为以自epoch开始的秒数表示的时间 +// 标准库函数mktime(t),将当地时间的 +// struct_time转换为以自epoch开始的秒数表示的时间 int64_t time_mktime(const _tm* this_tm, int locale) { status res; int local_offset; int64_t unix_time; - //获取本地时间偏移量(小时) + // 获取本地时间偏移量(小时) local_offset = locale * 60 * 60; - //检测时间结构体范围正确性 + // 检测时间结构体范围正确性 res = time_check_struct_time(this_tm); if (res) { status_deal(res); return 0; - } //异常情况返回时间零点 + } // 异常情况返回时间零点 - //转化时间 + // 转化时间 res = utc_struct_time_to_unix_time(this_tm, &unix_time); if (res) { status_deal(res); return 0; - } //异常情况返回时间零点 - //减去本地偏移时间 - //可能出现负数,严格来说,这不影响什么! + } // 异常情况返回时间零点 + // 减去本地偏移时间 + // 可能出现负数,严格来说,这不影响什么! unix_time -= local_offset; - //显示出来 - // time_printf("%I64d\n",unix_time); + // 显示出来 + // time_printf("%I64d\n",unix_time); - //返回数据 + // 返回数据 return unix_time; } -//标准库函数asctime() -//把结构化时间struct_time元组表示为以下形式的字符串: `'Sun Jun 20 23:21:05 -// 1993'`。 +// 标准库函数asctime() +// 把结构化时间struct_time元组表示为以下形式的字符串: `'Sun Jun 20 23:21:05 +// 1993'`。 void time_asctime(const _tm* this_tm) { - //星期缩写,python标准库是三个字母,这里并不相同 + // 星期缩写,python标准库是三个字母,这里并不相同 const char* week[] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"}; - //月份缩写 + // 月份缩写 const char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}; @@ -559,7 +559,7 @@ void time_asctime(const _tm* this_tm) { time_printf("%s\n", str); } -pika_float PikaStdDevice_Time_time(PikaObj* self) { +pika_float _time_time(PikaObj* self) { /* run platformGetTick() */ PIKA_PYTHON_BEGIN /* clang-format off */ @@ -579,7 +579,7 @@ pika_float PikaStdDevice_Time_time(PikaObj* self) { return time_time(self); } -int PikaStdDevice_Time_time_ns(PikaObj* self) { +int _time_time_ns(PikaObj* self) { return time_time_ns(self); } @@ -603,7 +603,7 @@ void time_set_tm_value(PikaObj* self, const _tm* this_tm) { #endif } -void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) { +void _time_gmtime(PikaObj* self, pika_float unix_time) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -613,14 +613,14 @@ void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) { char str[200]; time_gmtime(unix_time, &this_tm); time_set_tm_value(self, &this_tm); - //格式化字符 + // 格式化字符 time_struct_format(&this_tm, str); - //显示出来 + // 显示出来 time_printf("%s\n", str); #endif } -void PikaStdDevice_Time_localtime(PikaObj* self, pika_float unix_time) { +void _time_localtime(PikaObj* self, pika_float unix_time) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -631,9 +631,9 @@ void PikaStdDevice_Time_localtime(PikaObj* self, pika_float unix_time) { int locale = obj_getInt(self, "locale"); time_localtime(unix_time, &this_tm, locale); time_set_tm_value(self, &this_tm); - //格式化字符 + // 格式化字符 time_struct_format(&this_tm, str); - //显示出来 + // 显示出来 time_printf("%s\n", str); #endif } @@ -656,7 +656,7 @@ void time_get_tm_value(PikaObj* self, _tm* this_tm) { #endif } -int PikaStdDevice_Time_mktime(PikaObj* self) { +int _time_mktime(PikaObj* self) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -670,7 +670,7 @@ int PikaStdDevice_Time_mktime(PikaObj* self) { #endif } -void PikaStdDevice_Time_asctime(PikaObj* self) { +void _time_asctime(PikaObj* self) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -681,7 +681,7 @@ void PikaStdDevice_Time_asctime(PikaObj* self) { time_asctime(&this_tm); #endif } -void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) { +void _time_ctime(PikaObj* self, pika_float unix_time) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -694,7 +694,7 @@ void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) { #endif } -void PikaStdDevice_Time___init__(PikaObj* self) { +void _time___init__(PikaObj* self) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE #else _tm this_tm; @@ -704,7 +704,7 @@ void PikaStdDevice_Time___init__(PikaObj* self) { #endif } -void PikaStdDevice_Time_sleep(PikaObj* self, pika_float s) { +void _time_sleep(PikaObj* self, pika_float s) { Args* args = New_args(NULL); args_setInt(args, "ms", s * 1000); obj_runNativeMethod(self, "sleep_ms", args); diff --git a/package/time/_time.pyi b/package/time/_time.pyi new file mode 100644 index 000000000..5a414f412 --- /dev/null +++ b/package/time/_time.pyi @@ -0,0 +1,56 @@ +from PikaObj import * + + +def __init__(self): ... + + +def sleep(self, s: float): + """Sleep for s seconds.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def time(self) -> float: + """Get the current time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def time_ns(self) -> int: + """Get the current time in nanoseconds.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def gmtime(self, unix_time: float): + """Convert unix time to struct_time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def localtime(self, unix_time: float): + """Convert unix time to struct_time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def mktime(self) -> int: + """Convert struct_time to unix time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def asctime(self): + """Convert struct_time to string.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def ctime(self, unix_time: float): + """Convert unix time to string.""" + + +@abstractmethod +def sleep_s(self, s: int): ... + + +@abstractmethod +def sleep_ms(self, ms: int): ... + + +@abstractmethod +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def platformGetTick(): ... diff --git a/package/time/time.py b/package/time/time.py new file mode 100644 index 000000000..699b75b45 --- /dev/null +++ b/package/time/time.py @@ -0,0 +1,41 @@ +import _time + + +def sleep(s: float): + return _time.sleep(s) + + +def sleep_s(s: int): + return _time.sleep_s(s) + + +def sleep_ms(ms: int): + return _time.sleep_ms(ms) + + +def time() -> float: + return _time.time() + + +def time_ns() -> int: + return _time.time_ns() + + +def gmtime(unix_time: float): + return _time.gmtime(unix_time) + + +def localtime(unix_time: float): + return _time.localtime(unix_time) + + +def mktime() -> int: + return _time.mktime() + + +def asctime() -> str: + return _time.asctime() + + +def ctime(unix_time: float) -> str: + return _time.ctime(unix_time) diff --git a/port/linux/package/pikascript/PikaStdDevice.pyi b/port/linux/package/pikascript/PikaStdDevice.pyi index 7b7585c4f..f24daf145 100644 --- a/port/linux/package/pikascript/PikaStdDevice.pyi +++ b/port/linux/package/pikascript/PikaStdDevice.pyi @@ -98,49 +98,8 @@ class GPIO(BaseDev): def platformRead(self): ... -class Time(BaseDev): - def __init__(self): ... - - def sleep(self, s: float): - """Sleep for s seconds.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def time(self) -> float: - """Get the current time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def time_ns(self) -> int: - """Get the current time in nanoseconds.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def gmtime(self, unix_time: float): - """Convert unix time to struct_time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def localtime(self, unix_time: float): - """Convert unix time to struct_time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def mktime(self) -> int: - """Convert struct_time to unix time.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def asctime(self): - """Convert struct_time to string.""" - - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def ctime(self, unix_time: float): - """Convert unix time to string.""" - - @abstractmethod - def sleep_s(self, s: int): ... - - @abstractmethod - def sleep_ms(self, ms: int): ... - - @abstractmethod - @PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") - def platformGetTick(): ... +# class Time(BaseDev): + # use time module instead class ADC(BaseDev): diff --git a/port/linux/package/pikascript/TemplateDevice.pyi b/port/linux/package/pikascript/TemplateDevice.pyi index a21b186b3..538b08d12 100644 --- a/port/linux/package/pikascript/TemplateDevice.pyi +++ b/port/linux/package/pikascript/TemplateDevice.pyi @@ -7,14 +7,6 @@ class GPIO(PikaStdDevice.GPIO): def eventTest(self): ... -class Time(PikaStdDevice.Time): - # override - def sleep_s(self, s: int): ... - def sleep_ms(self, ms: int): ... - def platformGetTick(): ... - def platformGetEventId(self): ... - - class ADC(PikaStdDevice.ADC): # override def platformEnable(self): ... diff --git a/port/linux/package/pikascript/_time.pyi b/port/linux/package/pikascript/_time.pyi new file mode 100644 index 000000000..5a414f412 --- /dev/null +++ b/port/linux/package/pikascript/_time.pyi @@ -0,0 +1,56 @@ +from PikaObj import * + + +def __init__(self): ... + + +def sleep(self, s: float): + """Sleep for s seconds.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def time(self) -> float: + """Get the current time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def time_ns(self) -> int: + """Get the current time in nanoseconds.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def gmtime(self, unix_time: float): + """Convert unix time to struct_time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def localtime(self, unix_time: float): + """Convert unix time to struct_time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def mktime(self) -> int: + """Convert struct_time to unix time.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def asctime(self): + """Convert struct_time to string.""" + + +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def ctime(self, unix_time: float): + """Convert unix time to string.""" + + +@abstractmethod +def sleep_s(self, s: int): ... + + +@abstractmethod +def sleep_ms(self, ms: int): ... + + +@abstractmethod +@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE") +def platformGetTick(): ... diff --git a/port/linux/package/pikascript/main.py b/port/linux/package/pikascript/main.py index 3cebc6cab..0781ece7e 100644 --- a/port/linux/package/pikascript/main.py +++ b/port/linux/package/pikascript/main.py @@ -5,7 +5,7 @@ import GTestTask, TempDevTest import cb_test import configparser import test_module1, test_cmodule, test_module4, import_test -import hashlib, hmac, aes, base64 +import hashlib, hmac, aes, base64, time mem = PikaStdLib.MemChecker() print('hello pikascript!') diff --git a/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_Time.c b/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_Time.c deleted file mode 100644 index 5d93d9ace..000000000 --- a/port/linux/package/pikascript/pikascript-lib/TemplateDevice/TemplateDevice_Time.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "TemplateDevice_Time.h" - -void TemplateDevice_Time_sleep_ms(PikaObj *self, int ms){ - -} -void TemplateDevice_Time_sleep_s(PikaObj *self, int s){ - -} - -static volatile uint64_t tick_ms = 0; -void TemplateDevice_Time_platformGetTick(PikaObj *self){ - tick_ms += 50; - obj_setInt(self, "tick", tick_ms); -} - -void TemplateDevice_Time_platformGetEventId(PikaObj *self){ - -} \ No newline at end of file diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_Time.c b/port/linux/package/pikascript/pikascript-lib/time/_time.c similarity index 67% rename from port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_Time.c rename to port/linux/package/pikascript/pikascript-lib/time/_time.c index f0c3dd7ca..9aff69382 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdDevice/PikaStdDevice_Time.c +++ b/port/linux/package/pikascript/pikascript-lib/time/_time.c @@ -1,5 +1,5 @@ -#include "PikaStdDevice_Time.h" -#include "PikaStdDevice_common.h" +#include "_time.h" +#include "PikaVM.h" #if defined(__linux) #include #endif @@ -7,7 +7,7 @@ #include #endif -void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) { +void _time_sleep_ms(PikaObj* self, int ms) { #if defined(__linux) usleep(ms * 1000); #elif defined(_WIN32) @@ -16,7 +16,7 @@ void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) { __platform_sleep_ms(ms); #endif } -void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) { +void _time_sleep_s(PikaObj* self, int s) { #if defined(__linux) sleep(s); #elif defined(_WIN32) @@ -26,7 +26,7 @@ void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) { #endif } -void PikaStdDevice_Time_platformGetTick(PikaObj* self) { +void _time_platformGetTick(PikaObj* self) { obj_setInt(self, "tick", __platform_getTick()); } @@ -39,8 +39,8 @@ void PikaStdDevice_Time_platformGetTick(PikaObj* self) { #include "stdint.h" #include "stdio.h" -//结构体时间类型定义(来源c标准库corect_wtime.h) -//无论是16位整数还是32位整数都满足需求 +// 结构体时间类型定义(来源c标准库corect_wtime.h) +// 无论是16位整数还是32位整数都满足需求 typedef struct _tm { int tm_sec; // seconds after the minute - [0, 60] including leap second int tm_min; // minutes after the hour - [0, 59] @@ -53,16 +53,16 @@ typedef struct _tm { int tm_isdst; // daylight savings time flag } _tm; -//时间戳时间类型定义(来源c标准库time.h) -//直接支持64位秒数时间,附加时间精度为ns,根据设备决定,需要1GHz及以上时钟频率才能支持1ns级别时间精度 -//内部时间比对数据类型,传递给外界的时候会使用浮点数,所以精度会降低 -//但内部使用复合数据类型比对,以实现平台支持的最小时间精度比较 +// 时间戳时间类型定义(来源c标准库time.h) +// 直接支持64位秒数时间,附加时间精度为ns,根据设备决定,需要1GHz及以上时钟频率才能支持1ns级别时间精度 +// 内部时间比对数据类型,传递给外界的时候会使用浮点数,所以精度会降低 +// 但内部使用复合数据类型比对,以实现平台支持的最小时间精度比较 typedef struct { int64_t tv_sec; // Seconds - >= 0 int32_t tv_nsec; // Nanoseconds - [0, 999999999] } _timespec; -//错误处理 +// 错误处理 typedef int status; #define TIME_OK 0 @@ -74,9 +74,9 @@ typedef int status; #define TIME_LESS_THAN_1970 5 #define TIME_ERROR_STRUCT_TIME 6 -//错误状态处理函数 +// 错误状态处理函数 void status_deal(status s) { -//输出异常信息 +// 输出异常信息 #define time_printf(...) __platform_printf(__VA_ARGS__) time_printf("\n[Error-info]Checking a exception : "); @@ -108,59 +108,59 @@ void status_deal(status s) { time_printf("\n"); } -//获取硬件平台的Unix时间戳,时间精度为1s级别, +// 获取硬件平台的Unix时间戳,时间精度为1s级别, status time_get_unix_time(PikaObj* self, _timespec* this_timespec) { this_timespec->tv_sec = (int64_t)(obj_getInt(self, "tick") / 1000); return TIME_OK; } -//获取硬件平台的Tick时间,时间精度为1s级别以下 -//即1s的小数部分 +// 获取硬件平台的Tick时间,时间精度为1s级别以下 +// 即1s的小数部分 status time_get_tick_ns(PikaObj* self, _timespec* this_timespec) { this_timespec->tv_nsec = (obj_getInt(self, "tick") % 1000) * 1000000; return TIME_OK; } -//标准time()方法,返回以浮点数表示的从 epoch 开始的秒数的时间值。 -// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), +// 标准time()方法,返回以浮点数表示的从 epoch 开始的秒数的时间值。 +// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), pika_float time_time(PikaObj* self) { - status res = 0; //状态响应 + status res = 0; // 状态响应 _timespec temp_timespec = {0}; - //调用硬件平台函数,获取当前时间 + // 调用硬件平台函数,获取当前时间 res = time_get_unix_time(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 + } // 异常处理 res = time_get_tick_ns(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 - //以浮点数返回时间,float + } // 异常处理 + // 以浮点数返回时间,float return temp_timespec.tv_sec + (pika_float)temp_timespec.tv_nsec / 1000000000; } -//标准time_ns()方法,返回以整数表示的从 epoch 开始的纳秒数的时间值。 -// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), +// 标准time_ns()方法,返回以整数表示的从 epoch 开始的纳秒数的时间值。 +// epoch 是 1970 年 1 月 1 日 00:00:00 (UTC), int64_t time_time_ns(PikaObj* self) { - status res = 0; //状态响应 + status res = 0; // 状态响应 _timespec temp_timespec = {0}; - //调用硬件平台函数,获取当前时间 + // 调用硬件平台函数,获取当前时间 res = time_get_unix_time(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 + } // 异常处理 res = time_get_tick_ns(self, &temp_timespec); if (res) { status_deal(res); - } //异常处理 - //以浮点数返回时间,float + } // 异常处理 + // 以浮点数返回时间,float return temp_timespec.tv_sec * 1000000000 + temp_timespec.tv_nsec; } -//利用基姆拉尔森计算公式计算星期 +// 利用基姆拉尔森计算公式计算星期 int time_get_week(const _tm* this_tm) { - //月份要+1 + // 月份要+1 int month = this_tm->tm_mon + 1; int year = this_tm->tm_year; int day = this_tm->tm_mday; @@ -179,16 +179,16 @@ int time_get_week(const _tm* this_tm) { return w; } -//由Unix时间戳计算标准UTC时间 +// 由Unix时间戳计算标准UTC时间 status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { int32_t total_day; int32_t extra_second; int year_400, year_100, year_4, year_1; - int february_offset, temp; //二月偏移量,零时变量 + int february_offset, temp; // 二月偏移量,零时变量 - //判断是否输入小于0的时间戳 + // 判断是否输入小于0的时间戳 if (unix_time < 0) { - //暂不支持小于0的时间戳 + // 暂不支持小于0的时间戳 return TIME_LESS_THAN_ZERO; } @@ -198,31 +198,31 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { total_day = unix_time / DAY_SECOND; extra_second = unix_time - total_day * DAY_SECOND; -//为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 -// 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 -// 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 -#define YEAR_START (1600) //初始年份 -#define DAY_OFFSET (135140) //时间偏移量 +// 为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 +// 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 +// 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 +#define YEAR_START (1600) // 初始年份 +#define DAY_OFFSET (135140) // 时间偏移量 total_day += DAY_OFFSET; -//从1600年到3200年有1600/4-(1600/100-1600/400)=388个闰年 -//即 MAX_DAY 1600*365+388=584388 day -#define MAX_DAY (584388) //最大可判断时间天数 +// 从1600年到3200年有1600/4-(1600/100-1600/400)=388个闰年 +// 即 MAX_DAY 1600*365+388=584388 day +#define MAX_DAY (584388) // 最大可判断时间天数 if (total_day > MAX_DAY) { - //超过3200年的换算暂不支持 + // 超过3200年的换算暂不支持 return TIME_OVER_3200; } else { -//从1600年开始,天数都要多减一天,因为1600年是闰年 -//但是由于日期不包含当天时间,即2月2号,实际是2月1号+时:分:秒 -//所以算出来的日期要加上一天 -//两者配合,无需加减 +// 从1600年开始,天数都要多减一天,因为1600年是闰年 +// 但是由于日期不包含当天时间,即2月2号,实际是2月1号+时:分:秒 +// 所以算出来的日期要加上一天 +// 两者配合,无需加减 -//从400年,100年,4年逐渐缩小范围 -// 400个公历年天数为365*400+97=146097天 -// 400年内的100个公历年天数为365*100+24=36524天 -// 100年内的4年365*4+1=1461天 +// 从400年,100年,4年逐渐缩小范围 +// 400个公历年天数为365*400+97=146097天 +// 400年内的100个公历年天数为365*100+24=36524天 +// 100年内的4年365*4+1=1461天 #define DAY_OF_400Y (146097) #define DAY_OF_100Y (36524) #define DAY_OF_4Y (1461) @@ -230,56 +230,56 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { // 400年也要注意,要实际401年才可 year_400 = (total_day - 366) / DAY_OF_400Y; total_day -= year_400 * DAY_OF_400Y; - //计算400年内的情况 + // 计算400年内的情况 year_100 = (total_day - 1) / DAY_OF_100Y; total_day -= year_100 * DAY_OF_100Y; - //计算100年内的情况,要到第二年的第一天才算,即365+1 + // 计算100年内的情况,要到第二年的第一天才算,即365+1 year_4 = (total_day - 366) / DAY_OF_4Y; - //计算4年,需要格外注意0-5-8年,才会计算一个闰年,因为它才包含了4这个闰年,但并不包含8 + // 计算4年,需要格外注意0-5-8年,才会计算一个闰年,因为它才包含了4这个闰年,但并不包含8 total_day -= year_4 * DAY_OF_4Y; - //计算4年内的情况 - //需要减去1天,因为当天是不存在的 - //需要注意闰年会多一天 - //所有闰年都放在这里来考虑,即只要当前是闰年,那么这里就会剩下第一年闰年和第四年闰年两种情况 + // 计算4年内的情况 + // 需要减去1天,因为当天是不存在的 + // 需要注意闰年会多一天 + // 所有闰年都放在这里来考虑,即只要当前是闰年,那么这里就会剩下第一年闰年和第四年闰年两种情况 if (year_100 == 4) { - //第一年是闰年,此时为400*n+1年内 + // 第一年是闰年,此时为400*n+1年内 year_1 = 0; february_offset = 1; } else if (total_day <= DAY_OF_1Y * 4) { // 100*n+(4,8,...96)+1年,都是从第二年算起,非闰年 - //非闰年,需要减去1天,因为当天是不存在的 + // 非闰年,需要减去1天,因为当天是不存在的 year_1 = (total_day - 1) / DAY_OF_1Y; total_day -= year_1 * DAY_OF_1Y; february_offset = 0; } else { - //第四年是闰年 + // 第四年是闰年 year_1 = 4; total_day -= year_1 * DAY_OF_1Y; february_offset = 1; } - //计算出当前年份 + // 计算出当前年份 this_tm->tm_year = (year_400 * 400 + year_100 * 100 + year_4 * 4 + year_1) + YEAR_START; - //保存一年的天数 + // 保存一年的天数 this_tm->tm_yday = total_day; - //剩下的天数为1年内的天数,直接计算月和日 - //根据当前是否为闰年设置二月偏移量是否为1 - //能被4整除且不被100整除或者能被400整除 + // 剩下的天数为1年内的天数,直接计算月和日 + // 根据当前是否为闰年设置二月偏移量是否为1 + // 能被4整除且不被100整除或者能被400整除 - //闰年需要减去一天再计算 + // 闰年需要减去一天再计算 total_day -= february_offset; - //使用二分法快速定位月份,使用平年计算,在月份确定到2月时,再考虑闰年 - //判断是否在1-6月里面 - // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 + // 使用二分法快速定位月份,使用平年计算,在月份确定到2月时,再考虑闰年 + // 判断是否在1-6月里面 + // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 if (total_day <= 181) { - //判断是否在1-3月里面 + // 判断是否在1-3月里面 if (total_day <= 90) { - //判断是否在1-2月里面 + // 判断是否在1-2月里面 if (total_day <= 59) { - total_day += february_offset; //去掉二月的偏置 + total_day += february_offset; // 去掉二月的偏置 if (total_day <= 31) { // 1月 temp = 0; @@ -296,7 +296,7 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } else { // 4-6月 total_day -= 90; - //是否在4月里面 + // 是否在4月里面 if (total_day <= 30) { // 4月 temp = 3; @@ -315,9 +315,9 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } } else { total_day -= 181; - //判断是否在7-9月里面 + // 判断是否在7-9月里面 if (total_day <= 92) { - //是否在7-8月 + // 是否在7-8月 if (total_day <= 62) { if (total_day <= 31) { // 7月 @@ -335,7 +335,7 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } else { // 10-12月 total_day -= 92; - //是否在10-11月 + // 是否在10-11月 if (total_day <= 61) { if (total_day <= 31) { // 10月 @@ -353,11 +353,11 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { } } - //记录当前月份和天数 - this_tm->tm_mon = temp; //月份 [0,11] - this_tm->tm_mday = total_day; //天数 + // 记录当前月份和天数 + this_tm->tm_mon = temp; // 月份 [0,11] + this_tm->tm_mday = total_day; // 天数 - //利用额外秒数计算时-分-秒 + // 利用额外秒数计算时-分-秒 temp = extra_second / 3600; this_tm->tm_hour = temp; extra_second = extra_second - temp * 3600; @@ -368,70 +368,70 @@ status unix_time_to_utc_struct_time(_tm* this_tm, int64_t unix_time) { this_tm->tm_sec = extra_second; - //计算出当前日期的星期数 + // 计算出当前日期的星期数 this_tm->tm_wday = time_get_week(this_tm); - //夏令时不明 + // 夏令时不明 this_tm->tm_isdst = -1; } return TIME_OK; } -//由标准UTC时间生成Unix时间戳 +// 由标准UTC时间生成Unix时间戳 status utc_struct_time_to_unix_time(const _tm* this_tm, int64_t* unix_time) { int32_t total_day, total_leap_year, dyear; - int february_offset; //二月偏移量,零时变量 + int february_offset; // 二月偏移量,零时变量 // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - //每个月份对应前面所有月的天数 + // 每个月份对应前面所有月的天数 const int month_day[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; - //每天总秒数一定,将UTC时间(年月)转换成天数 - //为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 - // 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 - // 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 + // 每天总秒数一定,将UTC时间(年月)转换成天数 + // 为了减少额外闰年判断,把时间往前推到1600年,即闰年最大的一次公倍数开始计算判断 + // 1970-1600 = 370 年 ,370/4 -(370/100-1)=90 个闰年 + // 1600 DAY_OFFSET 365*(1970-1600)+90 = 135140,7为修正天数 if (this_tm->tm_year < 1970) { - //暂不支持1970之前的时间 + // 暂不支持1970之前的时间 *unix_time = 0; return TIME_LESS_THAN_1970; } if (this_tm->tm_year >= 3200) { - //暂不支持3200及以后的时间 + // 暂不支持3200及以后的时间 *unix_time = 0; return TIME_OVER_3200; } - //计算总年数要去掉尾巴,如年数20年,那么实际应该4个闰年,因为20这一年没有包含在里面 - //要减去一年来算闰年次数 - //先计算到相对1600年的天数,再转换到1970年 + // 计算总年数要去掉尾巴,如年数20年,那么实际应该4个闰年,因为20这一年没有包含在里面 + // 要减去一年来算闰年次数 + // 先计算到相对1600年的天数,再转换到1970年 dyear = this_tm->tm_year - YEAR_START - 1; total_leap_year = dyear / 4 - (dyear / 100 - dyear / 400 - 1); - //恢复减去的一年 + // 恢复减去的一年 dyear += 1; total_day = dyear * 365 + total_leap_year; - //减去1970到1600的总天数 + // 减去1970到1600的总天数 total_day -= DAY_OFFSET; - //增加月和日的总天数 - //判断是否是闰年 - //能被4整除且不被100整除或者能被400整除 + // 增加月和日的总天数 + // 判断是否是闰年 + // 能被4整除且不被100整除或者能被400整除 if (((dyear % 4 == 0) && (dyear % 100 != 0)) || (dyear % 400 == 0)) { - //闰年 + // 闰年 february_offset = 1; } else { february_offset = 0; } - //计算含月和日的总天数,日期要减去当天 + // 计算含月和日的总天数,日期要减去当天 total_day += month_day[this_tm->tm_mon] + this_tm->tm_mday - 1; - //二月以上需要加上偏移量 + // 二月以上需要加上偏移量 if (this_tm->tm_mon > 1) { total_day += february_offset; } - //根据天数以及时分秒计算Unix时间戳 + // 根据天数以及时分秒计算Unix时间戳 *unix_time = (int64_t)total_day * DAY_SECOND + this_tm->tm_hour * 3600 + this_tm->tm_min * 60 + this_tm->tm_sec; @@ -447,41 +447,41 @@ void time_struct_format(const _tm* this_tm, char* str) { this_tm->tm_wday, this_tm->tm_yday, this_tm->tm_isdst); } -//标准库函数gmtime,将以自 epoch 开始的秒数表示的时间转换为 UTC 的 struct_time +// 标准库函数gmtime,将以自 epoch 开始的秒数表示的时间转换为 UTC 的 struct_time void time_gmtime(pika_float unix_time, _tm* this_tm) { status res; - //转化时间 + // 转化时间 res = unix_time_to_utc_struct_time(this_tm, (int64_t)unix_time); if (res) { - status_deal(res); //异常情况处理 - //返回默认值 - // note: 异常情况返回默认时间起始点 + status_deal(res); // 异常情况处理 + // 返回默认值 + // note: 异常情况返回默认时间起始点 unix_time_to_utc_struct_time(this_tm, (int64_t)0); } } -//标准库函数localtime,将以自 epoch 开始的秒数表示的时间转换为当地时间的 -// struct_time +// 标准库函数localtime,将以自 epoch 开始的秒数表示的时间转换为当地时间的 +// struct_time void time_localtime(pika_float unix_time, _tm* this_tm, int locale) { status res; int local_offset; - //获取本地时间偏移量(小时) + // 获取本地时间偏移量(小时) local_offset = locale * 60 * 60; - //转化时间 + // 转化时间 res = unix_time_to_utc_struct_time(this_tm, (int64_t)unix_time + local_offset); if (res) { - status_deal(res); //异常情况处理 - //这里处理的策略和标准库不同,标准库最初始的时间是1970-1-1,00:00:00,对于不同时区来说,其值是不一样的 - //但本函数是要求各时区的起始时间不超过1970-1-1,00:00:00,实际上UTC时间可以更前,可靠的最早时间可到1600年 - //对于西时区来说,时间会缺失 + status_deal(res); // 异常情况处理 + // 这里处理的策略和标准库不同,标准库最初始的时间是1970-1-1,00:00:00,对于不同时区来说,其值是不一样的 + // 但本函数是要求各时区的起始时间不超过1970-1-1,00:00:00,实际上UTC时间可以更前,可靠的最早时间可到1600年 + // 对于西时区来说,时间会缺失 unix_time_to_utc_struct_time(this_tm, (int64_t)0); } } -//检测结构体时间是否在合适的范围内,但不检查它的正确性 +// 检测结构体时间是否在合适的范围内,但不检查它的正确性 status time_check_struct_time(const _tm* this_tm) { if (this_tm->tm_sec < 0 || this_tm->tm_sec > 60) { return TIME_ERROR_STRUCT_TIME; @@ -507,47 +507,47 @@ status time_check_struct_time(const _tm* this_tm) { return TIME_OK; } -//标准库函数mktime(t),将当地时间的 -// struct_time转换为以自epoch开始的秒数表示的时间 +// 标准库函数mktime(t),将当地时间的 +// struct_time转换为以自epoch开始的秒数表示的时间 int64_t time_mktime(const _tm* this_tm, int locale) { status res; int local_offset; int64_t unix_time; - //获取本地时间偏移量(小时) + // 获取本地时间偏移量(小时) local_offset = locale * 60 * 60; - //检测时间结构体范围正确性 + // 检测时间结构体范围正确性 res = time_check_struct_time(this_tm); if (res) { status_deal(res); return 0; - } //异常情况返回时间零点 + } // 异常情况返回时间零点 - //转化时间 + // 转化时间 res = utc_struct_time_to_unix_time(this_tm, &unix_time); if (res) { status_deal(res); return 0; - } //异常情况返回时间零点 - //减去本地偏移时间 - //可能出现负数,严格来说,这不影响什么! + } // 异常情况返回时间零点 + // 减去本地偏移时间 + // 可能出现负数,严格来说,这不影响什么! unix_time -= local_offset; - //显示出来 - // time_printf("%I64d\n",unix_time); + // 显示出来 + // time_printf("%I64d\n",unix_time); - //返回数据 + // 返回数据 return unix_time; } -//标准库函数asctime() -//把结构化时间struct_time元组表示为以下形式的字符串: `'Sun Jun 20 23:21:05 -// 1993'`。 +// 标准库函数asctime() +// 把结构化时间struct_time元组表示为以下形式的字符串: `'Sun Jun 20 23:21:05 +// 1993'`。 void time_asctime(const _tm* this_tm) { - //星期缩写,python标准库是三个字母,这里并不相同 + // 星期缩写,python标准库是三个字母,这里并不相同 const char* week[] = {"Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"}; - //月份缩写 + // 月份缩写 const char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}; @@ -559,7 +559,7 @@ void time_asctime(const _tm* this_tm) { time_printf("%s\n", str); } -pika_float PikaStdDevice_Time_time(PikaObj* self) { +pika_float _time_time(PikaObj* self) { /* run platformGetTick() */ PIKA_PYTHON_BEGIN /* clang-format off */ @@ -579,7 +579,7 @@ pika_float PikaStdDevice_Time_time(PikaObj* self) { return time_time(self); } -int PikaStdDevice_Time_time_ns(PikaObj* self) { +int _time_time_ns(PikaObj* self) { return time_time_ns(self); } @@ -603,7 +603,7 @@ void time_set_tm_value(PikaObj* self, const _tm* this_tm) { #endif } -void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) { +void _time_gmtime(PikaObj* self, pika_float unix_time) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -613,14 +613,14 @@ void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) { char str[200]; time_gmtime(unix_time, &this_tm); time_set_tm_value(self, &this_tm); - //格式化字符 + // 格式化字符 time_struct_format(&this_tm, str); - //显示出来 + // 显示出来 time_printf("%s\n", str); #endif } -void PikaStdDevice_Time_localtime(PikaObj* self, pika_float unix_time) { +void _time_localtime(PikaObj* self, pika_float unix_time) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -631,9 +631,9 @@ void PikaStdDevice_Time_localtime(PikaObj* self, pika_float unix_time) { int locale = obj_getInt(self, "locale"); time_localtime(unix_time, &this_tm, locale); time_set_tm_value(self, &this_tm); - //格式化字符 + // 格式化字符 time_struct_format(&this_tm, str); - //显示出来 + // 显示出来 time_printf("%s\n", str); #endif } @@ -656,7 +656,7 @@ void time_get_tm_value(PikaObj* self, _tm* this_tm) { #endif } -int PikaStdDevice_Time_mktime(PikaObj* self) { +int _time_mktime(PikaObj* self) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -670,7 +670,7 @@ int PikaStdDevice_Time_mktime(PikaObj* self) { #endif } -void PikaStdDevice_Time_asctime(PikaObj* self) { +void _time_asctime(PikaObj* self) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -681,7 +681,7 @@ void PikaStdDevice_Time_asctime(PikaObj* self) { time_asctime(&this_tm); #endif } -void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) { +void _time_ctime(PikaObj* self, pika_float unix_time) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE obj_setErrorCode(self, 1); obj_setSysOut( @@ -694,7 +694,7 @@ void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) { #endif } -void PikaStdDevice_Time___init__(PikaObj* self) { +void _time___init__(PikaObj* self) { #if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE #else _tm this_tm; @@ -704,7 +704,7 @@ void PikaStdDevice_Time___init__(PikaObj* self) { #endif } -void PikaStdDevice_Time_sleep(PikaObj* self, pika_float s) { +void _time_sleep(PikaObj* self, pika_float s) { Args* args = New_args(NULL); args_setInt(args, "ms", s * 1000); obj_runNativeMethod(self, "sleep_ms", args); diff --git a/port/linux/package/pikascript/time.py b/port/linux/package/pikascript/time.py new file mode 100644 index 000000000..699b75b45 --- /dev/null +++ b/port/linux/package/pikascript/time.py @@ -0,0 +1,41 @@ +import _time + + +def sleep(s: float): + return _time.sleep(s) + + +def sleep_s(s: int): + return _time.sleep_s(s) + + +def sleep_ms(ms: int): + return _time.sleep_ms(ms) + + +def time() -> float: + return _time.time() + + +def time_ns() -> int: + return _time.time_ns() + + +def gmtime(unix_time: float): + return _time.gmtime(unix_time) + + +def localtime(unix_time: float): + return _time.localtime(unix_time) + + +def mktime() -> int: + return _time.mktime() + + +def asctime() -> str: + return _time.asctime() + + +def ctime(unix_time: float) -> str: + return _time.ctime(unix_time) diff --git a/test/pika_config_gtest.c b/test/pika_config_gtest.c index 46c28bc85..391406029 100644 --- a/test/pika_config_gtest.c +++ b/test/pika_config_gtest.c @@ -21,6 +21,12 @@ void pika_platform_printf(char* fmt, ...) { } } +static volatile uint64_t tick_ms = 0; +int64_t pika_platform_getTick(void) { + tick_ms += 50; + return tick_ms; +} + /* quick_malloc is always open */ uint8_t __is_quick_malloc(void) { // return 1; diff --git a/test/unix-time-test.cpp b/test/unix-time-test.cpp index e757c4fe6..0c589d56e 100644 --- a/test/unix-time-test.cpp +++ b/test/unix-time-test.cpp @@ -20,10 +20,12 @@ TEST(unix_time, time) { pikaMemInfo.heapUsedMax = 0; /* run */ PikaObj* self = newRootObj("pikaMain", New_PikaMain); + extern unsigned char pikaModules_py_a[]; + obj_linkLibrary(self, pikaModules_py_a); obj_run(self, - "mytime = TemplateDevice.Time()\n" - "t1= mytime.time()\n" - "t2= mytime.time()\n"); + "import time\n" + "t1= time.time()\n" + "t2= time.time()\n"); /* 获取数据比对 */ float t1 = obj_getFloat(self, "t1"); float t2 = obj_getFloat(self, "t2"); @@ -42,19 +44,21 @@ TEST(unix_time, unix_time) { pikaMemInfo.heapUsedMax = 0; /* run */ PikaObj* self = newRootObj("pikaMain", New_PikaMain); + extern unsigned char pikaModules_py_a[]; + obj_linkLibrary(self, pikaModules_py_a); obj_run(self, - "mytime = PikaStdDevice.Time()\n" - "mytime.localtime(0.0)\n"); + "import time\n" + "time.localtime(0.0)\n"); /* 获取数据比对 */ - int tm_sec = obj_getInt(self, "mytime.tm_sec"); - int tm_min = obj_getInt(self, "mytime.tm_min"); - int tm_hour = obj_getInt(self, "mytime.tm_hour"); - int tm_mday = obj_getInt(self, "mytime.tm_mday"); - int tm_mon = obj_getInt(self, "mytime.tm_mon"); - int tm_year = obj_getInt(self, "mytime.tm_year"); - int tm_wday = obj_getInt(self, "mytime.tm_wday"); - int tm_yday = obj_getInt(self, "mytime.tm_yday"); - int tm_isdst = obj_getInt(self, "mytime.tm_isdst"); + int tm_sec = obj_getInt(self, "time._time.tm_sec"); + int tm_min = obj_getInt(self, "time._time.tm_min"); + int tm_hour = obj_getInt(self, "time._time.tm_hour"); + int tm_mday = obj_getInt(self, "time._time.tm_mday"); + int tm_mon = obj_getInt(self, "time._time.tm_mon"); + int tm_year = obj_getInt(self, "time._time.tm_year"); + int tm_wday = obj_getInt(self, "time._time.tm_wday"); + int tm_yday = obj_getInt(self, "time._time.tm_yday"); + int tm_isdst = obj_getInt(self, "time._time.tm_isdst"); /* assert */ EXPECT_EQ(tm_sec, 0); EXPECT_EQ(tm_min, 0); @@ -62,7 +66,7 @@ TEST(unix_time, unix_time) { EXPECT_EQ(tm_mday, 1); EXPECT_EQ(tm_mon, 0); // 1月 EXPECT_EQ(tm_year, 1970); - EXPECT_EQ(tm_wday, 4); //周四 + EXPECT_EQ(tm_wday, 4); // 周四 EXPECT_EQ(tm_yday, 1); EXPECT_EQ(tm_isdst, -1); /* deinit */ @@ -72,7 +76,7 @@ TEST(unix_time, unix_time) { #endif int compare(const _tm* t1, const _tm* t2) { - int size = 8; //只比对前面8个数据 + int size = 8; // 只比对前面8个数据 int* it1 = (int*)t1; int* it2 = (int*)t2; for (int i = 0; i < size; i++) { @@ -113,7 +117,7 @@ TEST(unix_time, iteration_form_1970_to_2070) { temp2->tm_year += 1900; if (compare(&temp1, temp2)) { printf("error!\n"); - //格式化字符 + // 格式化字符 time_struct_format(&temp1, str); printf("%s\n", str); time_struct_format(temp2, str);