1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*++
6
7
8
9Module Name:
10
11 cruntime/misctls.ccpp
12
13Abstract:
14
15 Implementation of C runtime functions that don't fit anywhere else
16 and depend on per-thread data
17
18
19
20--*/
21
22#include "pal/thread.hpp"
23#include "pal/palinternal.h"
24
25extern "C"
26{
27#include "pal/dbgmsg.h"
28#include "pal/misc.h"
29}
30
31#include <errno.h>
32/* <stdarg.h> needs to be included after "palinternal.h" to avoid name
33 collision for va_start and va_end */
34#include <stdarg.h>
35#include <time.h>
36#if HAVE_CRT_EXTERNS_H
37#include <crt_externs.h>
38#endif // HAVE_CRT_EXTERNS_H
39
40using namespace CorUnix;
41
42SET_DEFAULT_DEBUG_CHANNEL(CRT);
43
44/*++
45Function:
46
47 localtime
48
49See MSDN for more details.
50--*/
51
52struct PAL_tm *
53__cdecl
54PAL_localtime(const PAL_time_t *clock)
55{
56 CPalThread *pThread = NULL;
57 struct tm tmpResult;
58 struct PAL_tm *result = NULL;
59
60 PERF_ENTRY(localtime);
61 ENTRY( "localtime( clock=%p )\n",clock );
62
63 /* Get the per-thread buffer from the thread structure. */
64 pThread = InternalGetCurrentThread();
65
66 result = &pThread->crtInfo.localtimeBuffer;
67
68 localtime_r(reinterpret_cast<const time_t*>(clock), &tmpResult);
69
70 // Copy the result into the Windows struct.
71 result->tm_sec = tmpResult.tm_sec;
72 result->tm_min = tmpResult.tm_min;
73 result->tm_hour = tmpResult.tm_hour;
74 result->tm_mday = tmpResult.tm_mday;
75 result->tm_mon = tmpResult.tm_mon;
76 result->tm_year = tmpResult.tm_year;
77 result->tm_wday = tmpResult.tm_wday;
78 result->tm_yday = tmpResult.tm_yday;
79 result->tm_isdst = tmpResult.tm_isdst;
80
81 LOGEXIT( "localtime returned %p\n", result );
82 PERF_EXIT(localtime);
83
84 return result;
85}
86
87/*++
88Function:
89
90 ctime
91
92 There appears to be a difference between the FreeBSD and windows
93 implementations. FreeBSD gives Wed Dec 31 18:59:59 1969 for a
94 -1 param, and Windows returns NULL
95
96See MSDN for more details.
97--*/
98char *
99__cdecl
100PAL_ctime( const PAL_time_t *clock )
101{
102 CPalThread *pThread = NULL;
103 char * retval = NULL;
104
105 PERF_ENTRY(ctime);
106 ENTRY( "ctime( clock=%p )\n",clock );
107 if(*clock < 0)
108 {
109 /*If the input param is less than zero the value
110 *returned is less than the Unix epoch
111 *1st of January 1970*/
112 WARN("The input param is less than zero");
113 goto done;
114 }
115
116 /* Get the per-thread buffer from the thread structure. */
117 pThread = InternalGetCurrentThread();
118
119 retval = pThread->crtInfo.ctimeBuffer;
120
121 ctime_r(reinterpret_cast<const time_t*>(clock),retval);
122
123done:
124
125 LOGEXIT( "ctime() returning %p (%s)\n",retval,retval);
126 PERF_EXIT(ctime);
127
128 return retval;
129}