Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmktime.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: LGPL, See LGPL.txt in the top level directory 00003 * PROJECT: ReactOS CRT library 00004 * FILE: lib/sdk/crt/time/mktime.c 00005 * PURPOSE: Implementation of mktime, _mkgmtime 00006 * PROGRAMERS: Timo Kreuzer 00007 */ 00008 #include <precomp.h> 00009 #include "bitsfixup.h" 00010 00011 #define MAX_32BIT_TIME 0xFFFFFFFFULL 00012 00013 static int g_monthdays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; 00014 00015 __time64_t 00016 mktime_worker(struct tm * ptm, int utc) 00017 { 00018 struct tm *ptm2; 00019 __time64_t time; 00020 int mons, years, leapyears; 00021 TIME_ZONE_INFORMATION tzi; 00022 DWORD ret; 00023 00024 /* Normalize year and month */ 00025 if (ptm->tm_mon < 0) 00026 { 00027 mons = -ptm->tm_mon - 1; 00028 ptm->tm_year -= 1 + mons / 12; 00029 ptm->tm_mon = 11 - (mons % 12); 00030 } 00031 else if (ptm->tm_mon > 11) 00032 { 00033 mons = ptm->tm_mon; 00034 ptm->tm_year += (mons / 12); 00035 ptm->tm_mon = mons % 12; 00036 } 00037 00038 /* Is it inside margins */ 00039 if (ptm->tm_year < 70 || ptm->tm_year > 139) // FIXME: max year for 64 bits 00040 { 00041 return -1; 00042 } 00043 00044 years = ptm->tm_year - 70; 00045 00046 /* Number of leapyears passed since 1970 */ 00047 leapyears = (years + 1) / 4; 00048 00049 /* Calculate days up to 1st of Jan */ 00050 time = years * 365 + leapyears; 00051 00052 /* Calculate days up to 1st of month */ 00053 time += g_monthdays[ptm->tm_mon]; 00054 00055 /* Check if we need to add a leap day */ 00056 if (((years + 2) % 4) == 0) 00057 { 00058 if (ptm->tm_mon > 2) 00059 { 00060 time++; 00061 } 00062 } 00063 00064 time += ptm->tm_mday - 1; 00065 00066 time *= 24; 00067 time += ptm->tm_hour; 00068 00069 time *= 60; 00070 time += ptm->tm_min; 00071 00072 time *= 60; 00073 time += ptm->tm_sec; 00074 00075 if (time < 0) 00076 { 00077 return -1; 00078 } 00079 00080 /* Finally get normalized tm struct */ 00081 ptm2 = _gmtime64(&time); 00082 if (!ptm2) 00083 { 00084 return -1; 00085 } 00086 *ptm = *ptm2; 00087 00088 /* Finally adjust by the difference to GMT in seconds */ 00089 ret = GetTimeZoneInformation(&tzi); 00090 if (ret != TIME_ZONE_ID_INVALID) 00091 { 00092 time += tzi.Bias * 60; 00093 } 00094 00095 return time; 00096 } 00097 00098 /* int tm_sec; 00099 int tm_min; 00100 int tm_hour; 00101 int tm_mday; 00102 int tm_mon; 00103 int tm_year; 00104 int tm_wday; 00105 int tm_yday; 00106 int tm_isdst; 00107 */ 00108 00113 time_t 00114 _mkgmtime(struct tm *ptm) 00115 { 00116 __time64_t time = mktime_worker(ptm, 1); 00117 return (time_t)((time > MAX_32BIT_TIME) ? -1 : time); 00118 } 00119 00120 time_t 00121 mktime(struct tm *ptm) 00122 { 00123 __time64_t time = mktime_worker(ptm, 0); 00124 return (time_t)((time > MAX_32BIT_TIME) ? -1 : time); 00125 } 00126 00127 __time32_t 00128 _mkgmtime32(struct tm *ptm) 00129 { 00130 __time64_t time = mktime_worker(ptm, 1); 00131 return (__time32_t)((time > MAX_32BIT_TIME) ? -1 : time); 00132 } 00133 00134 __time32_t 00135 _mktime32(struct tm *ptm) 00136 { 00137 __time64_t time = mktime_worker(ptm, 0); 00138 return (__time32_t)((time > MAX_32BIT_TIME) ? -1 : time); 00139 } 00140 00141 __time64_t 00142 _mkgmtime64(struct tm *ptm) 00143 { 00144 return mktime_worker(ptm, 1); 00145 } 00146 00147 __time64_t 00148 _mktime64(struct tm *ptm) 00149 { 00150 return mktime_worker(ptm, 0); 00151 } Generated on Fri May 25 2012 04:35:11 for ReactOS by
1.7.6.1
|