ReactOS  0.4.15-dev-506-ga3ec01c
asctime.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: LGPL, See LGPL.txt in the top level directory
3  * PROJECT: ReactOS CRT library
4  * FILE: lib/sdk/crt/time/asctime.c
5  * PURPOSE: Implementation of asctime(), _asctime_s()
6  * PROGRAMERS: Timo Kreuzer
7  */
8 #include <precomp.h>
9 #include <tchar.h>
10 #include <time.h>
11 #include "bitsfixup.h"
12 
13 #define DAYSPERWEEK 7
14 #define MONSPERYEAR 12
15 #define HUNDREDYEAROFFSET 19
16 
17 static const _TCHAR wday_name[DAYSPERWEEK][5] =
18 {
19  _T("Sun "), _T("Mon "), _T("Tue "), _T("Wed "),
20  _T("Thu "), _T("Fri "), _T("Sat ")
21 };
22 
23 static const _TCHAR mon_name[MONSPERYEAR][5] =
24 {
25  _T("Jan "), _T("Feb "), _T("Mar "), _T("Apr "), _T("May "), _T("Jun "),
26  _T("Jul "), _T("Aug "), _T("Sep "), _T("Oct "), _T("Nov "), _T("Dec ")
27 };
28 
29 #ifdef _UNICODE
30 typedef unsigned long long _TCHAR4;
31 typedef unsigned long _TCHAR2;
32 #else
33 typedef unsigned long _TCHAR4;
34 typedef unsigned short _TCHAR2;
35 #endif
36 
37 #pragma pack(push,1)
38 typedef union
39 {
40  _TCHAR text[26];
41  struct
42  {
53  _TCHAR2 Year[2];
56  };
57 } timebuf_t;
58 #pragma pack(pop)
59 
61 _TCHAR2
63 {
64  union
65  {
66  _TCHAR2 char2;
67  _TCHAR array[2];
68  } u;
69 
70  u.array[0] = '0' + (x / 10);
71  u.array[1] = '0' + (x % 10);
72 
73  return u.char2;
74 }
75 
76 static __inline
77 void
78 FillBuf(timebuf_t *buf, const struct tm *ptm)
79 {
80  /* Format looks like this:
81  * "Sun Mar 01 12:34:56 1902\n\0" */
82  buf->WeekDay = *(_TCHAR4*)wday_name[ptm->tm_wday];
83  buf->Month = *(_TCHAR4*)mon_name[ptm->tm_mon];
84  buf->Day = IntToChar2(ptm->tm_mday);
85  buf->Space1 = ' ';
86  buf->Hour = IntToChar2(ptm->tm_hour);
87  buf->Sep1 = ':';
88  buf->Minute = IntToChar2(ptm->tm_min);
89  buf->Sep2 = ':';
90  buf->Second = IntToChar2(ptm->tm_sec);
91  buf->Space2 = ' ';
92  buf->Year[0] = IntToChar2(ptm->tm_year / 100 + HUNDREDYEAROFFSET);
93  buf->Year[1] = IntToChar2(ptm->tm_year % 100);
94  buf->lb = '\n';
95  buf->zt = '\0';
96 }
97 
98 /******************************************************************************
99  * \name _tasctime_s
100  * \brief Converts a local time into a string and returns a pointer to it.
101  * \param buffer Buffer that receives the string (26 characters).
102  * \param numberOfElements Size of the buffer in characters.
103  * \param time Pointer to the UTC time.
104  */
105 errno_t
107  _TCHAR* buffer,
108  size_t numberOfElements,
109  const struct tm *ptm)
110 {
111  /* Validate parameters */
112  if (!buffer || numberOfElements < 26 || !ptm ||
113  (unsigned int)ptm->tm_sec > 59 ||
114  (unsigned int)ptm->tm_min > 59 ||
115  (unsigned int)ptm->tm_hour > 23 ||
116  (unsigned int)ptm->tm_mday > 31 ||
117  (unsigned int)ptm->tm_mon > 11 ||
118  (unsigned int)ptm->tm_year > 2038 ||
119  (unsigned int)ptm->tm_wday > 6 ||
120  (unsigned int)ptm->tm_yday > 365)
121  {
122 #if 0
124 #ifdef UNICODE
125  L"_wasctime",
126 #else
127  L"asctime",
128 #endif
129  _CRT_WIDE(__FILE__),
130  __LINE__,
131  0);
132 #endif
133  return EINVAL;
134  }
135 
136  /* Fill the buffer */
137  FillBuf((timebuf_t*)buffer, ptm);
138 
139  return 0;
140 }
141 
142 /******************************************************************************
143  * \name _tasctime
144  * \brief Converts a UTC time into a string and returns a pointer to it.
145  * \param ptm Pointer to the UTC time.
146  * \remarks The string is stored in thread local buffer, shared between
147  * ctime, gmtime and localtime (32 and 64 bit versions).
148  */
149 _TCHAR *
150 _tasctime(const struct tm *ptm)
151 {
153  _TCHAR *pstr;
154 
155 #ifndef _UNICODE
156  pstr = data->asctime_buffer;
157 #else
158  pstr = data->wasctime_buffer;
159 #endif
160 
161  if(!pstr)
162  pstr = malloc(sizeof(struct tm));
163 
164  /* Fill the buffer */
165  FillBuf((timebuf_t*)pstr, ptm);
166 
167  return pstr;
168 }
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
#define MONSPERYEAR
Definition: asctime.c:14
_TCHAR2 Day
Definition: asctime.c:45
wchar_t UNICODE
Definition: ms-dtyp.idl:111
int tm_min
Definition: time.h:78
_TCHAR2 Minute
Definition: asctime.c:49
_TCHAR4 WeekDay
Definition: asctime.c:43
_TCHAR Space2
Definition: asctime.c:52
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
_TCHAR4 Month
Definition: asctime.c:44
_TCHAR lb
Definition: asctime.c:54
static __inline void FillBuf(timebuf_t *buf, const struct tm *ptm)
Definition: asctime.c:78
FORCEINLINE _TCHAR2 IntToChar2(int x)
Definition: asctime.c:62
const WCHAR * text
Definition: package.c:1827
Definition: arc.h:39
#define HUNDREDYEAROFFSET
Definition: asctime.c:15
int tm_mday
Definition: time.h:80
GLuint buffer
Definition: glext.h:5915
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
unsigned short _TCHAR2
Definition: asctime.c:34
_TCHAR * _tasctime(const struct tm *ptm)
Definition: asctime.c:150
int tm_year
Definition: time.h:82
_TCHAR2 Second
Definition: asctime.c:51
unsigned long _TCHAR4
Definition: asctime.c:33
static size_t numberOfElements
Definition: string.c:87
static const _TCHAR wday_name[DAYSPERWEEK][5]
Definition: asctime.c:17
_TCHAR Sep2
Definition: asctime.c:50
smooth NULL
Definition: ftsmooth.c:416
#define FORCEINLINE
Definition: ntbasedef.h:221
int tm_mon
Definition: time.h:81
int errno_t
Definition: crtdefs.h:353
char _TCHAR
Definition: tchar.h:1392
#define _T(x)
Definition: vfdio.h:22
#define DAYSPERWEEK
Definition: asctime.c:13
_TCHAR zt
Definition: asctime.c:55
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
errno_t _tasctime_s(_TCHAR *buffer, size_t numberOfElements, const struct tm *ptm)
Definition: asctime.c:106
static const WCHAR L[]
Definition: oid.c:1250
Definition: time.h:76
thread_data_t * msvcrt_get_thread_data(void)
Definition: tls.c:31
int tm_wday
Definition: time.h:83
int tm_yday
Definition: time.h:84
_TCHAR Sep1
Definition: asctime.c:48
_TCHAR2 Hour
Definition: asctime.c:47
static const _TCHAR mon_name[MONSPERYEAR][5]
Definition: asctime.c:23
int tm_sec
Definition: time.h:77
_TCHAR Space1
Definition: asctime.c:46
int tm_hour
Definition: time.h:79
#define malloc
Definition: debug_ros.c:4
#define _CRT_WIDE(_String)
Definition: crtdefs.h:43
void _invalid_parameter(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t pReserved)
Definition: errno.c:137