Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate timestamp control array in python

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to generate a timestamp control array in python, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Realize the fast search of time stamp to date in embedded system.

Import datetime,timeimport ostime_base = datetime.date (1970 total_seconds 1) second_fix = [] for year in range (2010, 2040): for month in range (1Power13): 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)

By the way, when you accidentally use python3.5 's environment 2.7, the friends adapt themselves.

I have used a very stupid method to look up the table later.

After the generated file is processed

# define REC_MAX 360uint32_t month_timestamp_ fixs [RECs _ 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, 0x5068DD00, 0x5068DD00, 0x5091BB80, 0x5091BB80, 0x50B94880, 0x50B94880, 0x50B94880, / / 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 polii 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;}

Then attach the mode of use.

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_ fixture [start _ index]; year = start_index / 12 + STAMP_TABLE_YEAR_START; clockout-> month = start_index% 12 Paired clockout out-> day = tmp_stamp / 86400 + 1; paired clockout out-> hour = (tmp_stamp% 86400) / 3600; paired clockout out-> minute = (tmp_stamp / 60)% 60; paired clockout out-> second = tmp_stamp% 60 } void clock_to_stamp (const clock_t* p_clock_in, timestamp_t* timestamp_out) {if ((paired clocklocked in-> year year > = 2040)) {APP_ERROR_CHECK (2); return;} uint32_t timestamp = month_timestamp_fix [((paired clocklocked in-> year-STAMP_TABLE_YEAR_START) * 12) + paired clocklocked in-> month] Timestamp + = (paired clockchain-> day-1) * 86400; timestamp + = pumped clockchain-> hour * 3600; timestamp + = paired clockchain-> minute * 60; timestamp + = paired clockchain-> second; * timestamp_out = timest} 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;}

In order to save ROM, the table lookup only maps from 2010 to 2040.

Import datetimeimport ostime_base = datetime.date (1970 total_seconds 1) second_fix = [] for year in range (2010, 2040): for month in range (1Power13): 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_ fixs [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) the above is how to generate timestamp control arrays in python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report