1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2012 by Bertrik Sikken
12 * Based on code from: rtc_as3514.c
13 * Copyright (C) 2007 by Barry Wardell
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
28 #define MINUTE_SECONDS 60
29 #define HOUR_SECONDS 3600
30 #define DAY_SECONDS 86400
31 #define WEEK_SECONDS 604800
32 #define YEAR_SECONDS 31536000
33 #define LEAP_YEAR_SECONDS 31622400
35 /* Days in each month */
36 static uint8_t days_in_month
[] =
37 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
39 static inline bool is_leapyear(int year
)
41 return (((year
%4)==0) && (((year
%100)!=0) || ((year
%400)==0)));
44 struct tm
*gmtime(const time_t *timep
)
46 static struct tm time
;
47 return gmtime_r(timep
, &time
);
50 struct tm
*gmtime_r(const time_t *timep
, struct tm
*tm
)
52 time_t seconds
= *timep
;
53 int year
, i
, mday
, hour
, min
;
56 tm
->tm_wday
= (seconds
/ DAY_SECONDS
+ 4) % 7;
60 while (seconds
>= LEAP_YEAR_SECONDS
)
62 if (is_leapyear(year
)){
63 seconds
-= LEAP_YEAR_SECONDS
;
65 seconds
-= YEAR_SECONDS
;
71 if (is_leapyear(year
)) {
72 days_in_month
[1] = 29;
74 days_in_month
[1] = 28;
75 if(seconds
>YEAR_SECONDS
){
77 seconds
-= YEAR_SECONDS
;
80 tm
->tm_year
= year
- 1900;
83 for (i
= 0; i
< 12; i
++)
85 if (seconds
< days_in_month
[i
]*DAY_SECONDS
){
90 seconds
-= days_in_month
[i
]*DAY_SECONDS
;
94 mday
= seconds
/DAY_SECONDS
;
95 seconds
-= mday
*DAY_SECONDS
;
96 tm
->tm_mday
= mday
+ 1; /* 1 ... 31 */
99 hour
= seconds
/HOUR_SECONDS
;
100 seconds
-= hour
*HOUR_SECONDS
;
104 min
= seconds
/MINUTE_SECONDS
;
105 seconds
-= min
*MINUTE_SECONDS
;
109 tm
->tm_sec
= seconds
;