imx233: modify arm cache timings on frequency switch
[maemo-rb.git] / firmware / libc / gmtime.c
blobe7ebdf0d90d712840638851a11c3b45b01ec15cf
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include "time.h"
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;
55 /* weekday */
56 tm->tm_wday = (seconds / DAY_SECONDS + 4) % 7;
58 /* Year */
59 year = 1970;
60 while (seconds >= LEAP_YEAR_SECONDS)
62 if (is_leapyear(year)){
63 seconds -= LEAP_YEAR_SECONDS;
64 } else {
65 seconds -= YEAR_SECONDS;
68 year++;
71 if (is_leapyear(year)) {
72 days_in_month[1] = 29;
73 } else {
74 days_in_month[1] = 28;
75 if(seconds>YEAR_SECONDS){
76 year++;
77 seconds -= YEAR_SECONDS;
80 tm->tm_year = year - 1900;
82 /* Month */
83 for (i = 0; i < 12; i++)
85 if (seconds < days_in_month[i]*DAY_SECONDS){
86 tm->tm_mon = i;
87 break;
90 seconds -= days_in_month[i]*DAY_SECONDS;
93 /* Month Day */
94 mday = seconds/DAY_SECONDS;
95 seconds -= mday*DAY_SECONDS;
96 tm->tm_mday = mday + 1; /* 1 ... 31 */
98 /* Hour */
99 hour = seconds/HOUR_SECONDS;
100 seconds -= hour*HOUR_SECONDS;
101 tm->tm_hour = hour;
103 /* Minute */
104 min = seconds/MINUTE_SECONDS;
105 seconds -= min*MINUTE_SECONDS;
106 tm->tm_min = min;
108 /* Second */
109 tm->tm_sec = seconds;
111 return tm;