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

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

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的临泉网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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

import datetime,time
import os

time_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%12 == 0:
		ret = os.write(fd, bytes("\r",'UTF-8'))
	target_str = "0x%08X, " % 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 360

uint32_t month_timestamp_fix[REC_MAX]={
0x4B3D3B00, 0x4B661980, 0x4B8B0380, 0x4BB3E200, 0x4BDB6F00, 0x4C044D80, 0x4C2BDA80, 0x4C54B900, 0x4C7D9780, 0x4CA52480, 0x4CCE0300, 0x4CF59000, 	//2010
0x4D1E6E80, 0x4D474D00, 0x4D6C3700, 0x4D951580, 0x4DBCA280, 0x4DE58100, 0x4E0D0E00, 0x4E35EC80, 0x4E5ECB00, 0x4E865800, 0x4EAF3680, 0x4ED6C380, 	//2011
0x4EFFA200, 0x4F288080, 0x4F4EBC00, 0x4F779A80, 0x4F9F2780, 0x4FC80600, 0x4FEF9300, 0x50187180, 0x50415000, 0x5068DD00, 0x5091BB80, 0x50B94880, 	//2012 
0x50E22700, 0x510B0580, 0x512FEF80, 0x5158CE00, 0x51805B00, 0x51A93980, 0x51D0C680, 0x51F9A500, 0x52228380, 0x524A1080, 0x5272EF00, 0x529A7C00,  	//2013
0x52C35A80, 0x52EC3900, 0x53112300, 0x533A0180, 0x53618E80, 0x538A6D00, 0x53B1FA00, 0x53DAD880, 0x5403B700, 0x542B4400, 0x54542280, 0x547BAF80,  	//2014
0x54A48E00, 0x54CD6C80, 0x54F25680, 0x551B3500, 0x5542C200, 0x556BA080, 0x55932D80, 0x55BC0C00, 0x55E4EA80, 0x560C7780, 0x56355600, 0x565CE300,  	//2015
0x5685C180, 0x56AEA000, 0x56D4DB80, 0x56FDBA00, 0x57254700, 0x574E2580, 0x5775B280, 0x579E9100, 0x57C76F80, 0x57EEFC80, 0x5817DB00, 0x583F6800,  	//2016
0x58684680, 0x58912500, 0x58B60F00, 0x58DEED80, 0x59067A80, 0x592F5900, 0x5956E600, 0x597FC480, 0x59A8A300, 0x59D03000, 0x59F90E80, 0x5A209B80,  	//2017
0x5A497A00, 0x5A725880, 0x5A974280, 0x5AC02100, 0x5AE7AE00, 0x5B108C80, 0x5B381980, 0x5B60F800, 0x5B89D680, 0x5BB16380, 0x5BDA4200, 0x5C01CF00,  	//2018
0x5C2AAD80, 0x5C538C00, 0x5C787600, 0x5CA15480, 0x5CC8E180, 0x5CF1C000, 0x5D194D00, 0x5D422B80, 0x5D6B0A00, 0x5D929700, 0x5DBB7580, 0x5DE30280,  	//2019
0x5E0BE100, 0x5E34BF80, 0x5E5AFB00, 0x5E83D980, 0x5EAB6680, 0x5ED44500, 0x5EFBD200, 0x5F24B080, 0x5F4D8F00, 0x5F751C00, 0x5F9DFA80, 0x5FC58780,  	//2020
0x5FEE6600, 0x60174480, 0x603C2E80, 0x60650D00, 0x608C9A00, 0x60B57880, 0x60DD0580, 0x6105E400, 0x612EC280, 0x61564F80, 0x617F2E00, 0x61A6BB00,  	//2021
0x61CF9980, 0x61F87800, 0x621D6200, 0x62464080, 0x626DCD80, 0x6296AC00, 0x62BE3900, 0x62E71780, 0x630FF600, 0x63378300, 0x63606180, 0x6387EE80,  	//2022
0x63B0CD00, 0x63D9AB80, 0x63FE9580, 0x64277400, 0x644F0100, 0x6477DF80, 0x649F6C80, 0x64C84B00, 0x64F12980, 0x6518B680, 0x65419500, 0x65692200,  	//2023
0x65920080, 0x65BADF00, 0x65E11A80, 0x6609F900, 0x66318600, 0x665A6480, 0x6681F180, 0x66AAD000, 0x66D3AE80, 0x66FB3B80, 0x67241A00, 0x674BA700,  	//2024
0x67748580, 0x679D6400, 0x67C24E00, 0x67EB2C80, 0x6812B980, 0x683B9800, 0x68632500, 0x688C0380, 0x68B4E200, 0x68DC6F00, 0x69054D80, 0x692CDA80,  	//2025
0x6955B900, 0x697E9780, 0x69A38180, 0x69CC6000, 0x69F3ED00, 0x6A1CCB80, 0x6A445880, 0x6A6D3700, 0x6A961580, 0x6ABDA280, 0x6AE68100, 0x6B0E0E00,  	//2026
0x6B36EC80, 0x6B5FCB00, 0x6B84B500, 0x6BAD9380, 0x6BD52080, 0x6BFDFF00, 0x6C258C00, 0x6C4E6A80, 0x6C774900, 0x6C9ED600, 0x6CC7B480, 0x6CEF4180,  	//2027
0x6D182000, 0x6D40FE80, 0x6D673A00, 0x6D901880, 0x6DB7A580, 0x6DE08400, 0x6E081100, 0x6E30EF80, 0x6E59CE00, 0x6E815B00, 0x6EAA3980, 0x6ED1C680,  	//2028
0x6EFAA500, 0x6F238380, 0x6F486D80, 0x6F714C00, 0x6F98D900, 0x6FC1B780, 0x6FE94480, 0x70122300, 0x703B0180, 0x70628E80, 0x708B6D00, 0x70B2FA00,  	//2029
0x70DBD880, 0x7104B700, 0x7129A100, 0x71527F80, 0x717A0C80, 0x71A2EB00, 0x71CA7800, 0x71F35680, 0x721C3500, 0x7243C200, 0x726CA080, 0x72942D80,  	//2030
0x72BD0C00, 0x72E5EA80, 0x730AD480, 0x7333B300, 0x735B4000, 0x73841E80, 0x73ABAB80, 0x73D48A00, 0x73FD6880, 0x7424F580, 0x744DD400, 0x74756100,  	//2031
0x749E3F80, 0x74C71E00, 0x74ED5980, 0x75163800, 0x753DC500, 0x7566A380, 0x758E3080, 0x75B70F00, 0x75DFED80, 0x76077A80, 0x76305900, 0x7657E600,  	//2032
0x7680C480, 0x76A9A300, 0x76CE8D00, 0x76F76B80, 0x771EF880, 0x7747D700, 0x776F6400, 0x77984280, 0x77C12100, 0x77E8AE00, 0x78118C80, 0x78391980,  	//2033
0x7861F800, 0x788AD680, 0x78AFC080, 0x78D89F00, 0x79002C00, 0x79290A80, 0x79509780, 0x79797600, 0x79A25480, 0x79C9E180, 0x79F2C000, 0x7A1A4D00,  	//2034
0x7A432B80, 0x7A6C0A00, 0x7A90F400, 0x7AB9D280, 0x7AE15F80, 0x7B0A3E00, 0x7B31CB00, 0x7B5AA980, 0x7B838800, 0x7BAB1500, 0x7BD3F380, 0x7BFB8080,  	//2035
0x7C245F00, 0x7C4D3D80, 0x7C737900, 0x7C9C5780, 0x7CC3E480, 0x7CECC300, 0x7D145000, 0x7D3D2E80, 0x7D660D00, 0x7D8D9A00, 0x7DB67880, 0x7DDE0580,  	//2036
0x7E06E400, 0x7E2FC280, 0x7E54AC80, 0x7E7D8B00, 0x7EA51800, 0x7ECDF680, 0x7EF58380, 0x7F1E6200, 0x7F474080, 0x7F6ECD80, 0x7F97AC00, 0x7FBF3900,  	//2037
0x7FE81780, 0x8010F600, 0x8035E000, 0x805EBE80, 0x80864B80, 0x80AF2A00, 0x80D6B700, 0x80FF9580, 0x81287400, 0x81500100, 0x8178DF80, 0x81A06C80,  	//2038
0x81C94B00, 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 2010

void 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 datetime
import os

time_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%12 == 0:
        #ret = os.write(fd, bytes("\r",'UTF-8'))
        ret = os.write(fd, "\r")
    target_str = "0x%08X, " % 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中怎么生成时间戳控制数组,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


当前标题:python中怎么生成时间戳控制数组
文章出自:http://csdahua.cn/article/pdseph.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流