C语言练习项目-UNIX时间戳4字节转时间可移植函数单片机嵌入式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C语言练习项目-UNIX时间戳4字节转时间可移植函数单片机嵌入式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3302字,纯文字阅读大概需要5分钟。
内容图文
4字节unix时间戳和正常年月日互相转换,很多协议用的到,c语言,有可以优化的地方欢迎讨论,根据网上案例总结优化后自己写的。
评论免费拿走!!!
1 #include"stdio.h" 2 #include "stdint.h" 3 #include <string.h> 4 5 #define TIME_ZONE 8 //北京时间 6 7 uint8_t Common_month_day[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年 8 uint8_t Leap_month_day[12] ={ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年 9 10 typedef struct 11 { 12 uint32_t Year; // 1970~2038 13 uint8_t Month; // 1~12 14 uint8_t Day; // 1~31 15 uint8_t Hour; // 0~23 16 uint8_t Min; // 0~59 17 uint8_t Sec; // 0~59 18 }UTC_TIME_T; 19 20 UTC_TIME_T Standard_Time = {0}; 21 22 unsigned int year_days(unsigned int year) 23 { 24 return ( ((year%4 == 0 && year%100 != 0) || (year%400 == 0)) ? 366 : 365 ); 25 } 26 27 void* unix_to_time(unsigned long long unix) 28 { 29 UTC_TIME_T* time_p = &Standard_Time; 30 31 uint32_t day_total = 0; 32 uint32_t day_compare = 0; 33 uint32_t sec_total = 0; 34 35 if(unix > 2147483647) //32 位限制,0x7FFFFFFF 36 unix = 0; 37 38 unix += (TIME_ZONE*60*60); //时区的差值 39 day_total = unix/(60*60*24); 40 sec_total = unix%(60*60*24); 41 42 for( time_p->Year = 1970; time_p->Year < 2039; time_p->Year ++ ) 43 { 44 day_compare += year_days(time_p->Year); 45 if( day_total < day_compare) 46 { 47 day_total -= (time_p->Year == 1970) ? 0 : (day_compare - year_days(time_p->Year)); 48 break; 49 } 50 } 51 52 for( day_compare = 0,time_p->Month = 1; time_p->Month < 13; time_p->Month ++ ) 53 { 54 day_compare += ( year_days( time_p->Year ) == 366 ) ? Leap_month_day[time_p->Month-1] : Common_month_day[time_p->Month-1] ; 55 56 if( day_total < day_compare) 57 { 58 if(time_p->Month != 1) 59 day_total -= ( year_days( time_p->Year ) == 366 ) ? (day_compare - Leap_month_day[time_p->Month-1]) : (day_compare - Common_month_day[time_p->Month-1]); 60 time_p->Day = ++day_total; 61 break; 62 } 63 } 64 time_p->Hour = sec_total/(60*60); 65 time_p->Min = (sec_total%(60*60))/60; 66 time_p->Sec = sec_total%60; 67 return time_p; 68 } 69 70 unsigned long long time_to_unix(void* time_input) 71 { 72 unsigned int temp = 0; 73 unsigned long long unix = 0; 74 UTC_TIME_T *time = (UTC_TIME_T*) time_input; 75 76 for( temp = 1970; temp < time->Year; temp++ ) 77 unix += year_days( temp )*(60*60*24); 78 79 for( temp = 1; temp < time->Month; temp++ ) 80 unix += (60*60*24)*((year_days(time->Year) == 366) ? (Leap_month_day[temp-1]) : (Common_month_day[temp-1])) ; 81 82 unix += (time->Day - 1) * (60*60*24); 83 unix += time->Hour * (60*60); 84 unix += time->Min * 60; 85 unix += time->Sec; 86 unix -= (TIME_ZONE*60*60); //减时区 87 return unix; 88 } 89 90 int main() 91 { 92 unsigned long long temp; 93 unsigned long long unix_long; 94 95 UTC_TIME_T *time_temp; 96 97 while(1) 98 { 99 printf("input unix:"); 100 scanf("%lld",&temp); 101 102 time_temp = unix_to_time( temp ); 103 unix_long = time_to_unix( time_temp ); 104 105 printf("%4d-%02d-%02d %02d:%02d:%02d unix:%lld\r\n",time_temp->Year,time_temp->Month,time_temp->Day,time_temp->Hour,time_temp->Min,time_temp->Sec,unix_long); 106 } 107 108 return 0; 109 } 110 111 #if 0 112 unsigned long long byte_to_long(uint8_t high_three, uint8_t high_two, uint8_t low_one,uint8_t low_zero) 113 { 114 return (high_three*16777216 + high_two*65536 + low_one*256 + low_zero); 115 } 116 #endif
内容总结
以上是互联网集市为您收集整理的C语言练习项目-UNIX时间戳4字节转时间可移植函数单片机嵌入式全部内容,希望文章能够帮你解决C语言练习项目-UNIX时间戳4字节转时间可移植函数单片机嵌入式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。