Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygengmtime.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/gmtime.c 00005 * PURPOSE: Implementation of gmtime, _gmtime32, _gmtime64 00006 * PROGRAMERS: Timo Kreuzer 00007 */ 00008 #include <precomp.h> 00009 00010 unsigned int g_monthdays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; 00011 unsigned int g_lpmonthdays[13] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}; 00012 00013 struct tm * 00014 _gmtime_worker(struct tm *ptm, __time64_t time, int do_dst) 00015 { 00016 unsigned int days, daystoyear, dayinyear, leapdays, leapyears, years, month; 00017 unsigned int secondinday, secondinhour; 00018 unsigned int *padays; 00019 00020 if (time < 0) 00021 { 00022 return 0; 00023 } 00024 00025 /* Divide into date and time */ 00026 days = (unsigned int)(time / SECONDSPERDAY); 00027 secondinday = time % SECONDSPERDAY; 00028 00029 /* Shift to days from 1.1.1601 */ 00030 days += DIFFDAYS; 00031 00032 /* Calculate leap days passed till today */ 00033 leapdays = leapdays_passed(days); 00034 00035 /* Calculate number of full leap years passed */ 00036 leapyears = leapyears_passed(days); 00037 00038 /* Are more leap days passed than leap years? */ 00039 if (leapdays > leapyears) 00040 { 00041 /* Yes, we're in a leap year */ 00042 padays = g_lpmonthdays; 00043 } 00044 else 00045 { 00046 /* No, normal year */ 00047 padays = g_monthdays; 00048 } 00049 00050 /* Calculate year */ 00051 years = (days - leapdays) / 365; 00052 ptm->tm_year = years - 299; 00053 00054 /* Calculate number of days till 1.1. of this year */ 00055 daystoyear = years * 365 + leapyears; 00056 00057 /* Calculate the day in this year */ 00058 dayinyear = days - daystoyear; 00059 00060 /* Shall we do DST corrections? */ 00061 ptm->tm_isdst = 0; 00062 if (do_dst) 00063 { 00064 int yeartime = dayinyear * SECONDSPERDAY + secondinday ; 00065 if (yeartime >= dst_begin && yeartime <= dst_end) // FIXME! DST in winter 00066 { 00067 time -= _dstbias; 00068 days = (unsigned int)(time / SECONDSPERDAY + DIFFDAYS); 00069 dayinyear = days - daystoyear; 00070 ptm->tm_isdst = 1; 00071 } 00072 } 00073 00074 ptm->tm_yday = dayinyear; 00075 00076 /* dayinyear < 366 => terminates with i <= 11 */ 00077 for (month = 0; dayinyear >= padays[month+1]; month++) 00078 ; 00079 00080 /* Set month and day in month */ 00081 ptm->tm_mon = month; 00082 ptm->tm_mday = 1 + dayinyear - padays[month]; 00083 00084 /* Get weekday */ 00085 ptm->tm_wday = (days + 1) % 7; 00086 00087 /* Calculate hour and second in hour */ 00088 ptm->tm_hour = secondinday / SECONDSPERHOUR; 00089 secondinhour = secondinday % SECONDSPERHOUR; 00090 00091 /* Calculate minute and second */ 00092 ptm->tm_min = secondinhour / 60; 00093 ptm->tm_sec = secondinhour % 60; 00094 00095 return ptm; 00096 } 00097 00098 /****************************************************************************** 00099 * \name _gmtime64 00100 * \brief 00101 * \param ptime Pointer to a variable of type __time64_t containing the time. 00102 */ 00103 struct tm * 00104 _gmtime64(const __time64_t * ptime) 00105 { 00106 thread_data_t *data = msvcrt_get_thread_data(); 00107 00108 /* Validate parameters */ 00109 if (!ptime || *ptime < 0) 00110 { 00111 return NULL; 00112 } 00113 00114 if(!data->time_buffer) 00115 data->time_buffer = malloc(sizeof(struct tm)); 00116 00117 /* Use _gmtime_worker to do the real work */ 00118 return _gmtime_worker(data->time_buffer, *ptime, 0); 00119 } 00120 00121 errno_t 00122 _gmtime64_s( 00123 struct tm* ptm, 00124 const __time64_t* ptime) 00125 { 00126 __time64_t time = *ptime; 00127 if (!ptm) 00128 { 00129 _set_errno(ERROR_BAD_COMMAND); 00130 MSVCRT_INVALID_PMT("ptm == NULL"); 00131 return ERROR_BAD_COMMAND; 00132 } 00133 00134 if (!ptime) 00135 { 00136 _set_errno(ERROR_BAD_COMMAND); 00137 MSVCRT_INVALID_PMT("ptime == NULL"); 00138 return ERROR_BAD_COMMAND; 00139 } 00140 00141 _gmtime_worker(ptm, time, 0); 00142 00143 return ERROR_SUCCESS; 00144 } 00145 00146 /****************************************************************************** 00147 * \name _gmtime32 00148 * \brief 00149 * \param ptime Pointer to a variable of type __time32_t containing the time. 00150 */ 00151 struct tm * 00152 _gmtime32(const __time32_t * ptime) 00153 { 00154 __time64_t time64; 00155 00156 if (!ptime) 00157 return NULL; 00158 time64 = *ptime; 00159 return _gmtime64(&time64); 00160 } 00161 00162 errno_t 00163 _gmtime32_s( 00164 struct tm* ptm, 00165 const __time32_t* ptime) 00166 { 00167 __time64_t time = *ptime; 00168 if (!ptm) 00169 { 00170 _set_errno(ERROR_BAD_COMMAND); 00171 MSVCRT_INVALID_PMT("ptm == NULL"); 00172 return ERROR_BAD_COMMAND; 00173 } 00174 00175 if (!ptime) 00176 { 00177 _set_errno(ERROR_BAD_COMMAND); 00178 MSVCRT_INVALID_PMT("ptime == NULL"); 00179 return ERROR_BAD_COMMAND; 00180 } 00181 00182 _gmtime_worker(ptm, time, 0); 00183 00184 return ERROR_SUCCESS; 00185 } 00186 00187 /****************************************************************************** 00188 * \name gmtime 00189 * \brief 00190 * \param ptime Pointer to a variable of type time_t containing the time. 00191 */ 00192 struct tm * 00193 gmtime(const time_t * ptime) 00194 { 00195 __time64_t time64; 00196 00197 if (!ptime) 00198 return NULL; 00199 time64 = *ptime; 00200 return _gmtime64(&time64); 00201 } Generated on Sat May 26 2012 04:35:36 for ReactOS by
1.7.6.1
|