[34] | 1 | /****************************************************************************** |
---|
| 2 | * time.c |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include <xen/config.h> |
---|
| 20 | #include <xen/time.h> |
---|
| 21 | |
---|
| 22 | /* Nonzero if YEAR is a leap year (every 4 years, |
---|
| 23 | except every 100th isn't, and every 400th is). */ |
---|
| 24 | #define __isleap(year) \ |
---|
| 25 | ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0)) |
---|
| 26 | |
---|
| 27 | /* How many days are in each month. */ |
---|
| 28 | const unsigned short int __mon_lengths[2][12] = { |
---|
| 29 | /* Normal years. */ |
---|
| 30 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
---|
| 31 | /* Leap years. */ |
---|
| 32 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
---|
| 33 | }; |
---|
| 34 | |
---|
| 35 | #define SECS_PER_HOUR (60 * 60) |
---|
| 36 | #define SECS_PER_DAY (SECS_PER_HOUR * 24) |
---|
| 37 | |
---|
| 38 | struct tm gmtime(unsigned long t) |
---|
| 39 | { |
---|
| 40 | struct tm tbuf; |
---|
| 41 | long days, rem; |
---|
| 42 | int y; |
---|
| 43 | const unsigned short int *ip; |
---|
| 44 | |
---|
| 45 | days = t / SECS_PER_DAY; |
---|
| 46 | rem = t % SECS_PER_DAY; |
---|
| 47 | |
---|
| 48 | tbuf.tm_hour = rem / SECS_PER_HOUR; |
---|
| 49 | rem %= SECS_PER_HOUR; |
---|
| 50 | tbuf.tm_min = rem / 60; |
---|
| 51 | tbuf.tm_sec = rem % 60; |
---|
| 52 | /* January 1, 1970 was a Thursday. */ |
---|
| 53 | tbuf.tm_wday = (4 + days) % 7; |
---|
| 54 | if ( tbuf.tm_wday < 0 ) |
---|
| 55 | tbuf.tm_wday += 7; |
---|
| 56 | y = 1970; |
---|
| 57 | while ( days >= (rem = __isleap(y) ? 366 : 365) ) |
---|
| 58 | { |
---|
| 59 | ++y; |
---|
| 60 | days -= rem; |
---|
| 61 | } |
---|
| 62 | while ( days < 0 ) |
---|
| 63 | { |
---|
| 64 | --y; |
---|
| 65 | days += __isleap(y) ? 366 : 365; |
---|
| 66 | } |
---|
| 67 | tbuf.tm_year = y - 1900; |
---|
| 68 | tbuf.tm_yday = days; |
---|
| 69 | ip = (const unsigned short int *)__mon_lengths[__isleap(y)]; |
---|
| 70 | for ( y = 0; days >= ip[y]; ++y ) |
---|
| 71 | days -= ip[y]; |
---|
| 72 | tbuf.tm_mon = y; |
---|
| 73 | tbuf.tm_mday = days + 1; |
---|
| 74 | tbuf.tm_isdst = -1; |
---|
| 75 | |
---|
| 76 | return tbuf; |
---|
| 77 | } |
---|