Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenasctime.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/ftime.c 00005 * PURPOSE: Implementation of asctime(), _asctime_s() 00006 * PROGRAMERS: Timo Kreuzer 00007 */ 00008 #include <precomp.h> 00009 #include <tchar.h> 00010 #include <time.h> 00011 #include "bitsfixup.h" 00012 00013 #define DAYSPERWEEK 7 00014 #define MONSPERYEAR 12 00015 #define HUNDREDYEAROFFSET 19 00016 00017 static const _TCHAR wday_name[DAYSPERWEEK][5] = 00018 { 00019 _T("Sun "), _T("Mon "), _T("Tue "), _T("Wed "), 00020 _T("Thu "), _T("Fri "), _T("Sat ") 00021 }; 00022 00023 static const _TCHAR mon_name[MONSPERYEAR][5] = 00024 { 00025 _T("Jan "), _T("Feb "), _T("Mar "), _T("Apr "), _T("May "), _T("Jun "), 00026 _T("Jul "), _T("Aug "), _T("Sep "), _T("Oct "), _T("Nov "), _T("Dec ") 00027 }; 00028 00029 #ifdef _UNICODE 00030 typedef unsigned long long _TCHAR4; 00031 typedef unsigned long _TCHAR2; 00032 #else 00033 typedef unsigned long _TCHAR4; 00034 typedef unsigned short _TCHAR2; 00035 #endif 00036 00037 #pragma pack(push,1) 00038 typedef union 00039 { 00040 _TCHAR text[26]; 00041 struct 00042 { 00043 _TCHAR4 WeekDay; 00044 _TCHAR4 Month; 00045 _TCHAR2 Day; 00046 _TCHAR Space1; 00047 _TCHAR2 Hour; 00048 _TCHAR Sep1; 00049 _TCHAR2 Minute; 00050 _TCHAR Sep2; 00051 _TCHAR2 Second; 00052 _TCHAR Space2; 00053 _TCHAR2 Year[2]; 00054 _TCHAR lb; 00055 _TCHAR zt; 00056 }; 00057 } timebuf_t; 00058 #pragma pack(pop) 00059 00060 FORCEINLINE 00061 _TCHAR2 00062 IntToChar2(int x) 00063 { 00064 union 00065 { 00066 _TCHAR2 char2; 00067 _TCHAR array[2]; 00068 } u; 00069 00070 u.array[0] = '0' + (x / 10); 00071 u.array[1] = '0' + (x % 10); 00072 00073 return u.char2; 00074 } 00075 00076 static __inline 00077 void 00078 FillBuf(timebuf_t *buf, const struct tm *ptm) 00079 { 00080 /* Format looks like this: 00081 * "Sun Mar 01 12:34:56 1902\n\0" */ 00082 buf->WeekDay = *(_TCHAR4*)wday_name[ptm->tm_wday]; 00083 buf->Month = *(_TCHAR4*)mon_name[ptm->tm_mon]; 00084 buf->Day = IntToChar2(ptm->tm_mday); 00085 buf->Space1 = ' '; 00086 buf->Hour = IntToChar2(ptm->tm_hour); 00087 buf->Sep1 = ':'; 00088 buf->Minute = IntToChar2(ptm->tm_min); 00089 buf->Sep2 = ':'; 00090 buf->Second = IntToChar2(ptm->tm_sec); 00091 buf->Space2 = ' '; 00092 buf->Year[0] = IntToChar2(ptm->tm_year / 100 + HUNDREDYEAROFFSET); 00093 buf->Year[1] = IntToChar2(ptm->tm_year % 100); 00094 buf->lb = '\n'; 00095 buf->zt = '\0'; 00096 } 00097 00098 /****************************************************************************** 00099 * \name _tasctime_s 00100 * \brief Converts a local time into a string and returns a pointer to it. 00101 * \param buffer Buffer that receives the string (26 characters). 00102 * \param numberOfElements Size of the buffer in characters. 00103 * \param time Pointer to the UTC time. 00104 */ 00105 errno_t 00106 _tasctime_s( 00107 _TCHAR* buffer, 00108 size_t numberOfElements, 00109 const struct tm *ptm) 00110 { 00111 /* Validate parameters */ 00112 if (!buffer || numberOfElements < 26 || !ptm || 00113 (unsigned int)ptm->tm_sec > 59 || 00114 (unsigned int)ptm->tm_min > 59 || 00115 (unsigned int)ptm->tm_hour > 23 || 00116 (unsigned int)ptm->tm_mday > 31 || 00117 (unsigned int)ptm->tm_mon > 11 || 00118 (unsigned int)ptm->tm_year > 2038 || 00119 (unsigned int)ptm->tm_wday > 6 || 00120 (unsigned int)ptm->tm_yday > 365) 00121 { 00122 #if 0 00123 _invalid_parameter(NULL, 00124 #ifdef UNICODE 00125 L"_wasctime", 00126 #else 00127 L"asctime", 00128 #endif 00129 _CRT_WIDE(__FILE__), 00130 __LINE__, 00131 0); 00132 #endif 00133 return EINVAL; 00134 } 00135 00136 /* Fill the buffer */ 00137 FillBuf((timebuf_t*)buffer, ptm); 00138 00139 return 0; 00140 } 00141 00142 /****************************************************************************** 00143 * \name _tasctime 00144 * \brief Converts a UTC time into a string and returns a pointer to it. 00145 * \param ptm Pointer to the UTC time. 00146 * \remarks The string is stored in thread local buffer, shared between 00147 * ctime, gmtime and localtime (32 and 64 bit versions). 00148 */ 00149 _TCHAR * 00150 _tasctime(const struct tm *ptm) 00151 { 00152 thread_data_t *data = msvcrt_get_thread_data(); 00153 _TCHAR *pstr; 00154 00155 #ifndef _UNICODE 00156 pstr = data->asctime_buffer; 00157 #else 00158 pstr = data->wasctime_buffer; 00159 #endif 00160 00161 if(!pstr) 00162 pstr = malloc(sizeof(struct tm)); 00163 00164 /* Fill the buffer */ 00165 FillBuf((timebuf_t*)pstr, ptm); 00166 00167 return pstr; 00168 } Generated on Fri May 25 2012 04:35:10 for ReactOS by
1.7.6.1
|