基于单片机定时器---年月日时分秒的算法

Posted Milo_lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于单片机定时器---年月日时分秒的算法相关的知识,希望对你有一定的参考价值。

Here ar a part of the code for reference:

 1 if(LocalTimes.Bit.second++>=59)
 2           {
 3             LocalTimes.Bit.second=0;
 4             if(LocalTimes.Bit.minute++>=59)
 5             {
 6               LocalTimes.Bit.minute=0;
 7               if(LocalTimes.Bit.hour++>=23)
 8               {
 9                 LocalTimes.Bit.hour=0;
10                 LocalTimes.Bit.day++;
11                 if(LocalTimes.Bit.month==2)
12                 {
13                   year=LocalTimes.Bit.year+1990;
14                   if((year%4==0&&year%100!=0)||(year%400==0))
15                   {
16                     if(LocalTimes.Bit.day>29)
17                     {
18                       LocalTimes.Bit.day=1;
19                       LocalTimes.Bit.month++;
20                     }
21                   }
22                   else
23                   {
24                     if(LocalTimes.Bit.day>28)
25                     {
26                       LocalTimes.Bit.day=1;
27                       LocalTimes.Bit.month++;
28                     }
29                   }
30                 }
31                 else if(LocalTimes.Bit.month%2) 
32                 {
33                   if(LocalTimes.Bit.month==9||LocalTimes.Bit.month==11)
34                   {
35                     if(LocalTimes.Bit.day>30)
36                     {
37                       LocalTimes.Bit.day=1;
38                       LocalTimes.Bit.month++;
39                     }
40                   }
41                   else
42                   {
43                     if(LocalTimes.Bit.day>31)
44                     {
45                       LocalTimes.Bit.day=1;
46                       LocalTimes.Bit.month++;
47                     }
48                   }
49                 }
50                 else 
51                 {
52                   if(LocalTimes.Bit.month==4||LocalTimes.Bit.month==6)
53                   {
54                     if(LocalTimes.Bit.day>30)
55                     {
56                       LocalTimes.Bit.day=1;
57                       LocalTimes.Bit.month++;
58                     }
59                   }
60                   else
61                   {
62                     if(LocalTimes.Bit.day>31)
63                     {
64                       LocalTimes.Bit.day=1;
65                       LocalTimes.Bit.month++;
66                       if(LocalTimes.Bit.month>12)
67                       {
68                         LocalTimes.Bit.month=1;
69                         if(LocalTimes.Bit.year++>62)
70                         {
71                           LocalTimes.Bit.year=9;
72                         }
73                       }
74                     }
75                   }
76                 }
77               }
78             }
79           }

Please note that the codes should be executed in the timer interrupt routine.
changed into seconds by using the RTC value:

 1 static posix_time_t update_sntp_from_localtimer(void)
 2 {
 3   struct tm timestamp;
 4   time_t t2 = 0;
 5   //0 is the first month, 1 is the second month,so on...
 6   timestamp.tm_mon = LocalTimestamp.BXI_Timestamp.Bit.month - 1;
 7   timestamp.tm_mday = LocalTimestamp.BXI_Timestamp.Bit.day;     //day of month
 8   timestamp.tm_year = LocalTimestamp.year - 1900; //start from 1900, \
 9                                                   e.g. 1900-1970 = 2208988800 seconds
10   timestamp.tm_hour = LocalTimestamp.BXI_Timestamp.Bit.hour;
11   timestamp.tm_min = LocalTimestamp.BXI_Timestamp.Bit.minute;
12   timestamp.tm_sec = LocalTimestamp.BXI_Timestamp.Bit.second;
13   timestamp.tm_yday = 0;        //day of year
14   timestamp.tm_isdst = 0;
15   t2 = mktime(&timestamp);
16   return t2;
17 }

 

Thank you!

 

以上是关于基于单片机定时器---年月日时分秒的算法的主要内容,如果未能解决你的问题,请参考以下文章

年月日时分秒的英文字母表示格式

如何把年月日时分秒的字符串转换成日期类型

如何将sql中带时分秒的日期转换为只有年月日的

php程序中如何把年月日时分秒的时间格式转化成年月日的格式,并且把年月日的值分别单独输出?

C# 接口中DateTime类型字段返回年月日格式,去掉时分秒的数据

C# 接口中DateTime类型字段返回年月日格式,去掉时分秒的数据