zoukankan      html  css  js  c++  java
  • 基于单片机定时器---年月日时分秒的算法

    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!

  • 相关阅读:
    codevs 1202 求和
    codevs 1201 最小数和最大数
    nyist 240 小明的调查统计(二)
    nyist28大数阶乘
    nyist 626 intersection set
    【】小技巧】CSS文字两端对齐
    Vue.js项目模板搭建
    25个最基本的JavaScript面试问题及答案
    java抽象类与接口的区别及用法
    JQuery事件手册
  • 原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/7648070.html
Copyright © 2011-2022 走看看