ReactOS 0.4.15-dev-7906-g1b85a5f
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
17static 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
23static 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
30typedef unsigned long long _TCHAR4;
31typedef unsigned long _TCHAR2;
32#else
33typedef unsigned long _TCHAR4;
34typedef unsigned short _TCHAR2;
35#endif
36
37#pragma pack(push,1)
38typedef union
39{
41 struct
42 {
53 _TCHAR2 Year[2];
56 };
57} timebuf_t;
58#pragma pack(pop)
59
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
76static __inline
77void
78FillBuf(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 */
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}
#define EINVAL
Definition: acclib.h:90
#define MONSPERYEAR
Definition: asctime.c:14
static __inline void FillBuf(timebuf_t *buf, const struct tm *ptm)
Definition: asctime.c:78
#define HUNDREDYEAROFFSET
Definition: asctime.c:15
static const _TCHAR wday_name[DAYSPERWEEK][5]
Definition: asctime.c:17
#define DAYSPERWEEK
Definition: asctime.c:13
unsigned short _TCHAR2
Definition: asctime.c:34
unsigned long _TCHAR4
Definition: asctime.c:33
FORCEINLINE _TCHAR2 IntToChar2(int x)
Definition: asctime.c:62
static const _TCHAR mon_name[MONSPERYEAR][5]
Definition: asctime.c:23
#define _CRT_WIDE(_String)
Definition: crtdefs.h:55
int errno_t
Definition: crtdefs.h:374
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
const WCHAR * text
Definition: package.c:1799
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLuint buffer
Definition: glext.h:5915
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
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
char _TCHAR
Definition: tchar.h:1392
#define _tasctime
Definition: tchar.h:653
#define _tasctime_s
Definition: tchar.h:665
thread_data_t * msvcrt_get_thread_data(void)
Definition: tls.c:31
static size_t numberOfElements
Definition: string.c:87
wchar_t UNICODE
Definition: ms-dtyp.idl:111
#define L(x)
Definition: ntvdm.h:50
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
Definition: time.h:68
int tm_mon
Definition: time.h:73
int tm_year
Definition: time.h:74
int tm_hour
Definition: time.h:71
int tm_sec
Definition: time.h:69
int tm_yday
Definition: time.h:76
int tm_mday
Definition: time.h:72
int tm_min
Definition: time.h:70
int tm_wday
Definition: time.h:75
_TCHAR lb
Definition: asctime.c:54
_TCHAR4 WeekDay
Definition: asctime.c:43
_TCHAR Sep2
Definition: asctime.c:50
_TCHAR Space2
Definition: asctime.c:52
_TCHAR2 Day
Definition: asctime.c:45
_TCHAR2 Minute
Definition: asctime.c:49
_TCHAR4 Month
Definition: asctime.c:44
_TCHAR zt
Definition: asctime.c:55
_TCHAR Space1
Definition: asctime.c:46
_TCHAR Sep1
Definition: asctime.c:48
_TCHAR2 Second
Definition: asctime.c:51
_TCHAR2 Hour
Definition: asctime.c:47
#define _T(x)
Definition: vfdio.h:22
#define FORCEINLINE
Definition: wdftypes.h:67