写了一个时间处理的类,能将人类时间转换成距离公元零年一月一日秒数(时间戳),同时支持时间戳转换成日期时间

  1 #include "stdafx.h"
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <time.h>
  5 
  6 #define IS_LEAP_YEAR(y)  (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  7 
  8 #define if_not_eual_ret(left, right) do {\
  9     if (left < right) {return true;} else if(left > right) {return false;}\
 10     } while(0)
 11 
 12 //从网上抄的 计算日期相差天数的函数
 13 int day_diff(int year_start, int month_start, int day_start, 
 14     int year_end, int month_end, int day_end)
 15 {
 16     int y2, m2, d2;
 17     int y1, m1, d1;
 18 
 19     m1 = (month_start + 9) % 12;
 20     y1 = year_start - m1/10;
 21     d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_start - 1);
 22 
 23     m2 = (month_end + 9) % 12;
 24     y2 = year_end - m2/10;
 25     d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_end - 1);
 26 
 27     return (d2 - d1);
 28 }
 29 
 30 
 31 class CMyDateTime
 32 {
 33 public:
 34     CMyDateTime(int sec_from_beg = 0)
 35     {
 36         m_sec_from_beg = sec_from_beg;
 37     }
 38 
 39     CMyDateTime(int year, int month, int day, int hour, int minute, int second)
 40     {
 41         m_sec_from_beg = to_second(year, month, day, hour, minute, second);
 42     }
 43 
 44     bool operator < (const CMyDateTime& theDate)
 45     {
 46         return m_sec_from_beg < theDate.m_sec_from_beg;
 47     }
 48 
 49     //获得0001 01:01:00至当前时间秒数
 50     long long to_second()
 51     {
 52         return m_sec_from_beg;
 53     }
 54 
 55     
 56     //年 取1900-3000 月 取0-11 日 取1-31 时分秒 取0-59
 57     static long long to_second(int year, int month, int day, int hour, int minute, int second)
 58     {
 59         //获取当前日期至纪元1年的天数
 60         int y1 = year, m1 = 0, d1 = day - 1;
 61         long long sec;
 62 
 63         //每月天数
 64         int month_day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 65 
 66         if (IS_LEAP_YEAR(year))
 67         {
 68             month_day[1]++;
 69         }
 70         for (int i = 0; i < month; i++)
 71         {
 72             m1 += month_day[i];
 73         }
 74 
 75         y1 -= 1;
 76         d1 = 365*y1 + y1/4 - y1/100 + y1/400 + m1 + d1;
 77 
 78         //24小时 60分 60秒
 79         sec = (long long)d1 * (24 * 60 * 60);
 80         sec += hour * 3600 + minute * 60 + second;
 81         return sec;
 82     }
 83 
 84     //把秒数转换成标准日期
 85     bool to_date(int& year, int& month, int& day, int& hour, int& minute, int& second)
 86     {
 87         // 先计算大概年份,用秒数除以366天秒数
 88         int day_from_beg = (int)(m_sec_from_beg / (24 * 60 * 60));
 89 
 90         int second_left = int(m_sec_from_beg - day_from_beg * (long long) (24 * 60 * 60));
 91         hour = second_left / (60 * 60);
 92 
 93         second_left = int (second_left - hour * (60 * 60));
 94         minute = second_left / 60;
 95         second = second_left % 60;
 96 
 97         int year_cnt = day_from_beg / 366;
 98         //每年365天 闰年多一天
 99         int day_from_beg_to_year = (year_cnt * 365 + year_cnt / 4 - year_cnt / 100 + year_cnt / 400);
100 
101         //然后逐年累加天数,直到天数大于day_from_beg
102         while (day_from_beg_to_year <= day_from_beg)
103         {
104             if (IS_LEAP_YEAR(year_cnt + 1))
105             {
106                 day_from_beg_to_year += 366;
107             }
108             else
109             {
110                 day_from_beg_to_year += 365;
111             }
112             year_cnt++;
113         }
114         //因为是要取最后一个小于等于在数 所以在这里要减一
115         year_cnt--;
116 
117         //知道年份后,再计算月份 先计算出当前日期距离今年开始相差多少天
118         int day_from_year = day_from_beg - (year_cnt * 365 + year_cnt / 4 - year_cnt / 100 + year_cnt / 400);
119         //今年的年数 = 经过的年数 + 1
120         year = year_cnt + 1;
121 
122         //每月天数
123         int month_day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
124 
125         if (IS_LEAP_YEAR(year))
126         {
127             month_day[1]++;
128         }
129 
130         for (month = 0; month < 12; month++)
131         {
132             if ((day_from_year - month_day[month]) < 0)
133             {
134                 break;
135             }
136 
137             day_from_year -= month_day[month];
138         }
139         day = day_from_year + 1;
140         return true;
141     }
142 private:
143     //当前日期至纪元1年的秒数
144     long long m_sec_from_beg;
145 };
146 
147 //测试函数,测试每一秒钟从人类日期格式转换成CMyDateTime 再转换回来是否正确
148 void test_time()
149 {
150     int month_day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
151 
152     for (int year = 1; year < 2100; year++)
153     {
154         double start = GetTickCount();
155         bool is_leap = IS_LEAP_YEAR(year);
156         if (is_leap)
157         {
158             month_day[1] = 29;
159         }
160 
161         for (int month = 0; month < 12; month++)
162         {
163             for (int day = 1; day <= month_day[month]; day++)
164             {
165                 for (int hour = 0; hour < 24; hour++)
166                 {
167                     for (int minute = 0; minute < 60; minute++)
168                     {
169                         for (int second = 0; second < 60; second++)
170                         {
171                             CMyDateTime t(year, month, day, hour, minute, second);
172                             int year_new, month_new, day_new, hour_new, minute_new, second_new;
173                             if (t.to_date(year_new, month_new, day_new, hour_new, minute_new, second_new))
174                             {
175                                 if (year != year_new || month != month_new || day_new != day ||
176                                     hour != hour_new || minute != minute_new || second != second_new)
177                                 {
178                                     printf("day = %d day_new = %d\n", day, day_new);
179                                     printf("month = %d month_new = %d\n", month, month_new);
180                                     printf("year = %d year_new = %d\n", year, year_new);
181 
182 
183                                     printf("hour = %d hour_new = %d\n", hour, hour_new);
184                                     printf("minute = %d minute_new = %d\n", minute, minute_new);
185                                     printf("second = %d second_new = %d\n", second, second_new);
186                                     printf("------------------------\n");
187                                 }
188                             }
189                         }
190                     }
191                 }
192             }
193         }
194 
195         if (is_leap)
196         {
197             month_day[1] = 28;
198         }
199 
200         //我的机器一年花5秒左右 效率不是特别高 但目前没有优化头绪
201         printf("总共耗时 %f 毫秒\n", GetTickCount() - start);
202         printf("------------[%04d]------------\n", year);
203     }
204 }

 

posted @ 2016-10-07 22:58  皇家救星  阅读(1037)  评论(0编辑  收藏  举报