千家信息网

python中怎么生成时间戳控制数组

发表于:2025-01-24 作者:千家信息网编辑
千家信息网最后更新 2025年01月24日,本篇文章为大家展示了python中怎么生成时间戳控制数组,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。在嵌入式实现时间戳转日期的快速查找import date
千家信息网最后更新 2025年01月24日python中怎么生成时间戳控制数组

本篇文章为大家展示了python中怎么生成时间戳控制数组,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

在嵌入式实现时间戳转日期的快速查找

import datetime,timeimport ostime_base = datetime.date(1970,1,1)second_fix = []for year in range(2010, 2040):        for month in range(1,13):                time_set = datetime.date(year, month, 1)                second_fix.append(int((time_set-time_base).total_seconds()))print(second_fix)cnt = len(second_fix)fd = os.open("arr.c", os.O_RDWR|os.O_CREAT)ret = os.write(fd, bytes("uint32_t arr[%d]={"%cnt,'UTF-8'))for index in range(cnt):        if index == 0:                ret = os.write(fd, bytes("\r",'UTF-8'))        target_str = "0xX, " % second_fix[index]         os.write(fd, bytes(target_str,'UTF-8'))ret = os.write(fd, bytes("\r};\r",'UTF-8'))os.close(fd)

对了 一不小心用的python3.5的环境 2.7的朋友自己适配

以前用过一个很笨的办法、、后面换查表法了

生成的文件加工之后

#define REC_MAX 360uint32_t month_timestamp_fix[REC_MAX]={0x4B3D3B00, 0x4B661980, 0x4B8B0380, 0x4BB3E200, 0x4BDB6F00, 0x4C044D80, 0x4C2BDA80, 0x4C54B900, 0x4C7D9780, 0x4CA52480, 0x4CCE0300, 0x4CF59000,     //20100x4D1E6E80, 0x4D474D00, 0x4D6C3700, 0x4D951580, 0x4DBCA280, 0x4DE58100, 0x4E0D0E00, 0x4E35EC80, 0x4E5ECB00, 0x4E865800, 0x4EAF3680, 0x4ED6C380,     //20110x4EFFA200, 0x4F288080, 0x4F4EBC00, 0x4F779A80, 0x4F9F2780, 0x4FC80600, 0x4FEF9300, 0x50187180, 0x50415000, 0x5068DD00, 0x5091BB80, 0x50B94880,     //2012 0x50E22700, 0x510B0580, 0x512FEF80, 0x5158CE00, 0x51805B00, 0x51A93980, 0x51D0C680, 0x51F9A500, 0x52228380, 0x524A1080, 0x5272EF00, 0x529A7C00,        //20130x52C35A80, 0x52EC3900, 0x53112300, 0x533A0180, 0x53618E80, 0x538A6D00, 0x53B1FA00, 0x53DAD880, 0x5403B700, 0x542B4400, 0x54542280, 0x547BAF80,        //20140x54A48E00, 0x54CD6C80, 0x54F25680, 0x551B3500, 0x5542C200, 0x556BA080, 0x55932D80, 0x55BC0C00, 0x55E4EA80, 0x560C7780, 0x56355600, 0x565CE300,        //20150x5685C180, 0x56AEA000, 0x56D4DB80, 0x56FDBA00, 0x57254700, 0x574E2580, 0x5775B280, 0x579E9100, 0x57C76F80, 0x57EEFC80, 0x5817DB00, 0x583F6800,        //20160x58684680, 0x58912500, 0x58B60F00, 0x58DEED80, 0x59067A80, 0x592F5900, 0x5956E600, 0x597FC480, 0x59A8A300, 0x59D03000, 0x59F90E80, 0x5A209B80,        //20170x5A497A00, 0x5A725880, 0x5A974280, 0x5AC02100, 0x5AE7AE00, 0x5B108C80, 0x5B381980, 0x5B60F800, 0x5B89D680, 0x5BB16380, 0x5BDA4200, 0x5C01CF00,        //20180x5C2AAD80, 0x5C538C00, 0x5C787600, 0x5CA15480, 0x5CC8E180, 0x5CF1C000, 0x5D194D00, 0x5D422B80, 0x5D6B0A00, 0x5D929700, 0x5DBB7580, 0x5DE30280,        //20190x5E0BE100, 0x5E34BF80, 0x5E5AFB00, 0x5E83D980, 0x5EAB6680, 0x5ED44500, 0x5EFBD200, 0x5F24B080, 0x5F4D8F00, 0x5F751C00, 0x5F9DFA80, 0x5FC58780,        //20200x5FEE6600, 0x60174480, 0x603C2E80, 0x60650D00, 0x608C9A00, 0x60B57880, 0x60DD0580, 0x6105E400, 0x612EC280, 0x61564F80, 0x617F2E00, 0x61A6BB00,        //20210x61CF9980, 0x61F87800, 0x621D6200, 0x62464080, 0x626DCD80, 0x6296AC00, 0x62BE3900, 0x62E71780, 0x630FF600, 0x63378300, 0x63606180, 0x6387EE80,        //20220x63B0CD00, 0x63D9AB80, 0x63FE9580, 0x64277400, 0x644F0100, 0x6477DF80, 0x649F6C80, 0x64C84B00, 0x64F12980, 0x6518B680, 0x65419500, 0x65692200,        //20230x65920080, 0x65BADF00, 0x65E11A80, 0x6609F900, 0x66318600, 0x665A6480, 0x6681F180, 0x66AAD000, 0x66D3AE80, 0x66FB3B80, 0x67241A00, 0x674BA700,        //20240x67748580, 0x679D6400, 0x67C24E00, 0x67EB2C80, 0x6812B980, 0x683B9800, 0x68632500, 0x688C0380, 0x68B4E200, 0x68DC6F00, 0x69054D80, 0x692CDA80,        //20250x6955B900, 0x697E9780, 0x69A38180, 0x69CC6000, 0x69F3ED00, 0x6A1CCB80, 0x6A445880, 0x6A6D3700, 0x6A961580, 0x6ABDA280, 0x6AE68100, 0x6B0E0E00,        //20260x6B36EC80, 0x6B5FCB00, 0x6B84B500, 0x6BAD9380, 0x6BD52080, 0x6BFDFF00, 0x6C258C00, 0x6C4E6A80, 0x6C774900, 0x6C9ED600, 0x6CC7B480, 0x6CEF4180,        //20270x6D182000, 0x6D40FE80, 0x6D673A00, 0x6D901880, 0x6DB7A580, 0x6DE08400, 0x6E081100, 0x6E30EF80, 0x6E59CE00, 0x6E815B00, 0x6EAA3980, 0x6ED1C680,        //20280x6EFAA500, 0x6F238380, 0x6F486D80, 0x6F714C00, 0x6F98D900, 0x6FC1B780, 0x6FE94480, 0x70122300, 0x703B0180, 0x70628E80, 0x708B6D00, 0x70B2FA00,        //20290x70DBD880, 0x7104B700, 0x7129A100, 0x71527F80, 0x717A0C80, 0x71A2EB00, 0x71CA7800, 0x71F35680, 0x721C3500, 0x7243C200, 0x726CA080, 0x72942D80,        //20300x72BD0C00, 0x72E5EA80, 0x730AD480, 0x7333B300, 0x735B4000, 0x73841E80, 0x73ABAB80, 0x73D48A00, 0x73FD6880, 0x7424F580, 0x744DD400, 0x74756100,        //20310x749E3F80, 0x74C71E00, 0x74ED5980, 0x75163800, 0x753DC500, 0x7566A380, 0x758E3080, 0x75B70F00, 0x75DFED80, 0x76077A80, 0x76305900, 0x7657E600,        //20320x7680C480, 0x76A9A300, 0x76CE8D00, 0x76F76B80, 0x771EF880, 0x7747D700, 0x776F6400, 0x77984280, 0x77C12100, 0x77E8AE00, 0x78118C80, 0x78391980,        //20330x7861F800, 0x788AD680, 0x78AFC080, 0x78D89F00, 0x79002C00, 0x79290A80, 0x79509780, 0x79797600, 0x79A25480, 0x79C9E180, 0x79F2C000, 0x7A1A4D00,        //20340x7A432B80, 0x7A6C0A00, 0x7A90F400, 0x7AB9D280, 0x7AE15F80, 0x7B0A3E00, 0x7B31CB00, 0x7B5AA980, 0x7B838800, 0x7BAB1500, 0x7BD3F380, 0x7BFB8080,        //20350x7C245F00, 0x7C4D3D80, 0x7C737900, 0x7C9C5780, 0x7CC3E480, 0x7CECC300, 0x7D145000, 0x7D3D2E80, 0x7D660D00, 0x7D8D9A00, 0x7DB67880, 0x7DDE0580,        //20360x7E06E400, 0x7E2FC280, 0x7E54AC80, 0x7E7D8B00, 0x7EA51800, 0x7ECDF680, 0x7EF58380, 0x7F1E6200, 0x7F474080, 0x7F6ECD80, 0x7F97AC00, 0x7FBF3900,        //20370x7FE81780, 0x8010F600, 0x8035E000, 0x805EBE80, 0x80864B80, 0x80AF2A00, 0x80D6B700, 0x80FF9580, 0x81287400, 0x81500100, 0x8178DF80, 0x81A06C80,        //20380x81C94B00, 0x81F22980, 0x82171380, 0x823FF200, 0x82677F00, 0x82905D80, 0x82B7EA80, 0x82E0C900, 0x8309A780, 0x83313480, 0x835A1300, 0x8381A000,        //2039};static uint32_t get_index(uint32_t stamp, const uint32_t* src, uint32_t size){        uint32_t index_start = 0;        uint32_t index_end = size - 1;        uint32_t result = 0;                for(int i= 0;i<10;i++){                if(stamp < src[index_start+1]){                        result = index_start;                        break;                }else if(stamp > src[index_end]){                        result = index_end;                        break;                }else{                        uint32_t index_mid = (index_start+index_end)/2;                        if(stamp >= src[index_mid]){                                index_start = index_mid;                                continue;                        }else{                                index_end = index_mid;                                continue;                        }                }        }        if(i == 10){                APP_ERROR_CHECK(1);        }        return result;}

然后附上使用方式

typedef struct{    uint16_t year;   //direct year    uint8_t month;    uint8_t day;       //day begin with 1 not 0    uint8_t hour;    uint8_t minute;    uint8_t second;}clock_t;typedef enum {    week_mon = 0,    week_tues,    week_wed,    week_thur,    week_fri,    week_sat,    week_sun,} week_t;#define STAMP_TABLE_YEAR_START 2010void stamp_to_clock(const timestamp_t timestamp, clock_t* p_clock_out){    uint32_t start_index = get_index(timestamp, month_timestamp_fix, REC_MAX);    timestamp_t tmp_stamp = timestamp - month_timestamp_fix[start_index];    p_clock_out->year = start_index / 12 + STAMP_TABLE_YEAR_START;    p_clock_out->month = start_index % 12;    p_clock_out->day = tmp_stamp / 86400 + 1;    p_clock_out->hour = (tmp_stamp % 86400) / 3600;    p_clock_out->minute = (tmp_stamp / 60) % 60;    p_clock_out->second = tmp_stamp % 60;}void clock_to_stamp(const clock_t* p_clock_in, timestamp_t* timestamp_out){        if((p_clock_in->year <= 2010)                ||(p_clock_in->year >= 2040)){                APP_ERROR_CHECK(2);                return;        }    uint32_t timestamp = month_timestamp_fix[((p_clock_in->year - STAMP_TABLE_YEAR_START) * 12) + p_clock_in->month];    timestamp += (p_clock_in->day - 1) * 86400;    timestamp += p_clock_in->hour * 3600;    timestamp += p_clock_in->minute * 60;    timestamp += p_clock_in->second;    *timestamp_out = timestamp;}week_t week_now_get(void){    const week_t the_day_1970_1_1 = week_thur;    uint32_t day_past_now = timestamp_now / 86400;    week_t week_now = (week_t)((day_past_now + the_day_1970_1_1) % 7);    return week_now;}

为了省ROM 查表只映射了2010到2040年

import datetimeimport ostime_base = datetime.date(1970,1,1)second_fix = []for year in range(2010, 2040):    for month in range(1,13):        time_set = datetime.date(year, month, 1)        second_fix.append(int((time_set-time_base).total_seconds()))print(second_fix)cnt = len(second_fix)fd = os.open("arr.c", os.O_RDWR|os.O_CREAT)#ret = os.write(fd, bytes("uint32_t arr[%d]={"%cnt,'UTF-8'))ret = os.write(fd, "uint32_t arr[%d]={");for index in range(cnt):    if index == 0:        #ret = os.write(fd, bytes("\r",'UTF-8'))        ret = os.write(fd, "\r")    target_str = "0xX, " % second_fix[index]    #os.write(fd, bytes(target_str,'UTF-8'))    os.write(fd, target_str)#ret = os.write(fd, bytes("\r};\r",'UTF-8'))ret = os.write(fd, "\r};\r")os.close(fd)

上述内容就是python中怎么生成时间戳控制数组,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

时间 生成 数组 控制 内容 技能 知识 简明 对了 简明扼要 一不小心 办法 就是 嵌入式 文件 文章 方式 日期 更多 朋友 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 软件开发的软件有没有免费的 收银机本地数据库打开错误 贵州专业网络技术服务五星服务 卢雪娜 网络安全简介 山东程序软件开发定制 计科和物联网网络安全哪个好 鄞州ios软件开发服务 如何护航网络安全 坐标岛国际服务器怎么下载 网络安全和ARP欺骗的关系 atlas服务器基于什么架构 现代网络技术课程期末总结 华为云数据库产品服务部 山西餐饮软件开发价格行情 腾讯云服务器如何连接mysql 晋宁区品牌软件开发价格走势 深圳学优教育软件开发 战神36计服务器 oa办公系统有哪些数据库 哪里可以找到好的软件开发公司 怎么看数据库备份 手机登录云服务器找回手机 为什么要建立后台数据库 服务器管理器创建共享 腾迅为什么老是服务器异常 联想R730服务器进不去系统 广州卡考网络技术有限公司 互联网 智能科技 ceo 服务器进bios设置显卡 网络技术是指针对数据的采集
0