蔡勒公式用于计算星期的公式。在嵌入式物联网应用中经常需要用到将4G cat1 模块时间同步本地时间。而大多数的4G cat1 模块,都是只有 “年月日时分秒” e.g: "+QLTS: "2021/01/24,03:40:48+32,0""; ,而缺少星期。
那么蔡勒公式就可以轻松的将4G cat1模块返回的时间戳换算出星期。
一下是蔡勒公式原型:
1 C y 13*(m+1) 2 Zeller Formate:w = (----- - 2*C + y + ----- + [----------] + d - 1)MOD7 3 4 4 5 4 C = Century. y=year,m=month(month>=3.January=13,February=14),d=day,
需要注意的是,当月小于3时,那么月需要加12,年需要减1
例程:
1 #include<msp430.h> 2 #include<string.h> 3 #include <stdlib.h> 4 #include <stdio.h> 5 #include <ctype.h> 6 7 typedef unsigned long u32; 8 typedef unsigned int u16; 9 typedef unsigned char u8; 10 typedef long long s64; 11 typedef long s32; 12 typedef int s16; 13 typedef char s8; 14 typedef enum{false = 0, true=!false}bool; 15 /*---------------------------------------------------------------------------- 16 C y 13*(m+1) 17 Zeller Formate:w = (----- - 2*C + y + ----- + [----------] + d - 1)MOD7 18 4 4 5 19 C = Century. y=year,m=month(month>=3.January=13,February=14),d=day, 20 e.g 2021.1.1=2020.13.1, 2021.2.1=2020.14.1,2021.3.1=2021.3.1 21 ------------------------------------------------------------------------------*/ 22 unsigned int get_specified_str_between_chara( char *str, char start, char over, char *substr ) 23 { 24 char *p1, *p2; 25 unsigned int len = 0; 26 27 p1 = strchr( str, start ); 28 p2 = strchr( p1 + 1, over ); 29 *substr = 0; 30 31 if ( p1 != NULL && p2 != NULL && p2 > p1 + 1 ) 32 { 33 while ( ++p1 < p2 ) 34 { 35 *substr++ = *p1; 36 len++; 37 } 38 *substr = 0; 39 } 40 return len; 41 } 42 unsigned int get_specified_string_after_comma( unsigned char CNT_Comma,char *str,char *substr ) 43 { 44 char *p=str; 45 unsigned int len; 46 47 *substr = 0; 48 if( CNT_Comma ) 49 { 50 while( isprint(*p) && CNT_Comma ) 51 { 52 if( *p++ == ',' ) CNT_Comma--; 53 } 54 if( CNT_Comma ) return 0; 55 } 56 57 len=0; 58 while( isprint(*p) && *p != ',' ) 59 { 60 *substr++ = *p++; 61 len++; 62 } 63 *substr = 0; 64 return len; 65 } 66 typedef struct 67 { 68 u8 sec; 69 u8 min; 70 u8 hour; 71 u8 day; 72 u8 week; 73 u8 month; 74 u8 year; 75 }rtc_counter_value_t; 76 77 char* string="+QLTS: "2019/01/13,03:40:48+32,0""; 78 char *pval; 79 char *pdat; 80 u16 tmp,year,month,day,hour,minute,second,Century,week; 81 rtc_counter_value_t rtc_tmp; 82 void main(void) 83 { 84 //pdat="+QLTS: "2019/01/13,03:40:48+32,0"" 85 pdat=strstr(string,"+QLTS: "); 86 if(pdat!=NULL) 87 { 88 tmp=get_specified_str_between_chara(pdat,'"','/',pval); 89 if(tmp!=0) 90 { 91 year=atoi(pval); 92 } 93 rtc_tmp.year=year%100/10<<4; 94 rtc_tmp.year|=year%10; 95 96 //pdat="2019/01/13,03:40:48+32,0" 97 get_specified_str_between_chara(pdat,'"','"',pdat); 98 month=get_specified_str_between_chara(pdat+tmp,'/','/',pval);//pdat+tmp="/01/13,03:40:48+32,0" 99 if(month==2) 100 { 101 month=atoi(pval); 102 } 103 if(month<3) 104 { 105 month+=12; 106 year-=1; 107 } 108 year=2000+year%100; 109 Century=year/100; 110 year=year%100; 111 //day="13" 112 day=get_specified_str_between_chara(pdat+tmp+3,'/',',',pval);//pdat+tmp+3="/13,03:40:48+32,0" 113 day=atoi(pval); 114 115 //pval="03" 116 hour=get_specified_str_between_chara(pdat+tmp+3+3,',',':',pval);//pdat+tmp+3+3=",03:40:48+32,0" 117 //hour=03 118 hour=atoi(pval); 119 120 //pval="40" 121 minute=get_specified_str_between_chara(pdat+tmp+3+3+3,':',':',pval);//pdat+tmp+3+3+3=":40:48+32,0" 122 minute=atoi(pval); 123 //pval="48" 124 second=get_specified_str_between_chara(pdat+tmp+3+3+3+3,':','+',pval);//pdat+tmp+3+3+3+3=":48+32,0" 125 second=atoi(pval); 126 tmp=(u16)((float)Century/4+year+(float)year/4+26*(month+1)/10+day-1-2*Century); 127 week=(u16)tmp%7; 128 } 129 for(;;); 130 }