闰年判断_天数加减_UTC时区转换

  1 #include "stdio.h"
  2 #include "stdint.h"
  3 #include "stdbool.h"
  4 
  5 typedef struct
  6 {
  7     int year;
  8     int8_t month;
  9     int8_t day;
 10     int8_t hour;
 11     int8_t minute;
 12     int8_t second;
 13 }DATE_Stru;
 14 
 15 bool IsLeapYear(int year)
 16 {
 17     if(((0 == (year % 4))&&(0 != (year % 100))) ||
 18         (0 == (year % 400)))
 19     {
 20         return true;
 21     }
 22     else
 23     {
 24         return false;
 25     }
 26 }
 27 
 28 void AddOneMonth(DATE_Stru *src)
 29 {
 30     src->month += 1;
 31     if(src->month > 12)
 32     {
 33         src->month = 1;
 34         src->year += 1;
 35     }
 36 }
 37 
 38 void SubOneMonth(DATE_Stru *src)
 39 {
 40     src->month -= 1;
 41     if(src->month < 1)
 42     {
 43         src->month = 12;
 44         src->year -= 1;
 45     }
 46 }
 47 
 48 void AddOneDay(DATE_Stru *src)
 49 {
 50     uint8_t overflow_day_flg = 0;
 51 
 52     src->day += 1;
 53 
 54     if((1 == src->month)||
 55         (3 == src->month)||
 56         (5 == src->month)||
 57         (7 == src->month)||
 58         (8 == src->month)||
 59         (10 == src->month)||
 60         (12 == src->month))
 61     {
 62         /* 31day */
 63         if(src->day > 31)
 64         {
 65             overflow_day_flg = 1;
 66         }
 67     }
 68     else if((4 == src->month)||
 69             (6 == src->month)||
 70             (9 == src->month)||
 71             (11 == src->month))
 72     {
 73         /* 30day */
 74         if(src->day > 30)
 75         {
 76             overflow_day_flg = 1;
 77         }
 78     }
 79     else
 80     { /* February */
 81         if(IsLeapYear(src->year))
 82         {/* 29day */
 83             if(src->day > 29)
 84             {
 85                 overflow_day_flg = 1;
 86             }
 87         }
 88         else
 89         {/* 28day */
 90             if(src->day > 28)
 91             {
 92                 overflow_day_flg = 1;
 93             }
 94         }
 95     }
 96 
 97     if(1 == overflow_day_flg)
 98     {
 99         src->day = 1;
100         AddOneMonth(src);
101     }
102 }
103 
104 void SubOneDay(DATE_Stru *src)
105 {
106     src->day -= 1;
107 
108     if(src->day < 1)
109     {
110         SubOneMonth(src);
111         if((1 == src->month)||
112             (3 == src->month)||
113             (5 == src->month)||
114             (7 == src->month)||
115             (8 == src->month)||
116             (10 == src->month)||
117             (12 == src->month))
118         {
119             /* 31day */
120             src->day = 31;
121         }
122         else if((4 == src->month)||
123                 (6 == src->month)||
124                 (9 == src->month)||
125                 (11 == src->month))
126         {
127             /* 30day */
128             src->day = 30;
129         }
130         else
131         { /* February */
132             if(IsLeapYear(src->year))
133             {/* 29day */
134                 src->day = 29;
135             }
136             else
137             {/* 28day */
138                 src->day = 28;
139             }
140         }
141     }
142 }
143 
144 /*
145 zone: 1 ~ 23
146 */
147 void ConvertUTCToOtherZone(DATE_Stru *src, uint8_t zone)
148 {
149     uint8_t add_day_flg = 0;
150 
151     src->hour += zone;
152 
153     if(src->hour >= 24)
154     {
155         src->hour -= 24;
156         AddOneDay(src);
157     }
158 }
159 
160 void ShowDate(DATE_Stru *src)
161 {
162     printf("%d-%hhu-%hhu\n",src->year, src->month, src->day);
163 }
164 
165 void ShowTime(DATE_Stru *src)
166 {
167     printf("%hhu:%hhu:%hhu\n", src->hour, src->minute, src->second);
168 }
169 
170 int main(int argc, char** argv)
171 {
172     DATE_Stru src = {0};
173 
174     while(1)
175     {
176         printf("please input the year number: \n");
177         scanf("%d", &src.year);
178 
179         printf("please input the month number: \n");
180         scanf("%hhu", &src.month);
181 
182         printf("please input the day number: \n");
183         scanf("%hhu", &src.day);
184 
185         ShowDate(&src);
186         AddOneDay(&src);
187         ShowDate(&src);
188         SubOneDay(&src);
189         ShowDate(&src);
190 
191         ConvertUTCToOtherZone(&src, 8);
192         ShowDate(&src);
193         ShowTime(&src);
194 
195         ConvertUTCToOtherZone(&src, 8);
196         ShowDate(&src);
197         ShowTime(&src);
198 
199         ConvertUTCToOtherZone(&src, 10);
200         ShowDate(&src);
201         ShowTime(&src);
202     }
203 
204     return 0;
205 }
 
posted @ 2017-10-10 15:13  小小林林  阅读(241)  评论(0编辑  收藏  举报