xref: /reactos/sdk/lib/crt/time/localtime.c (revision 34593d93)
1 /*
2  * COPYRIGHT:   LGPL, See LGPL.txt in the top level directory
3  * PROJECT:     ReactOS CRT library
4  * FILE:        lib/sdk/crt/time/localtime.c
5  * PURPOSE:     Implementation of localtime, localtime_s
6  * PROGRAMERS:  Timo Kreuzer
7  *              Samuel Serapi�n
8  */
9 #include <precomp.h>
10 #include <time.h>
11 #include "bitsfixup.h"
12 
13 //fix me: header?
14 #define _MAX__TIME64_T     0x793406fffLL     /* number of seconds from
15                                                  00:00:00, 01/01/1970 UTC to
16                                                  23:59:59. 12/31/3000 UTC */
17 
18 errno_t
localtime_s(struct tm * _tm,const time_t * ptime)19 localtime_s(struct tm* _tm, const time_t *ptime)
20 {
21     /* check for NULL */
22     if (!_tm || !ptime )
23     {
24         if(_tm) memset(_tm, 0xFF, sizeof(struct tm));
25         _invalid_parameter(NULL,
26                            0,//__FUNCTION__,
27                            _CRT_WIDE(__FILE__),
28                            __LINE__,
29                            0);
30         _set_errno(EINVAL);
31         return EINVAL;
32     }
33 
34     /* Validate input */
35     if (*ptime < 0 || *ptime > _MAX__TIME64_T)
36     {
37         memset(_tm, 0xFF, sizeof(struct tm));
38         _set_errno(EINVAL);
39         return EINVAL;
40     }
41 
42     _tm = localtime(ptime);
43     return 0;
44 }
45 
46 extern char _tz_is_set;
47 
48 struct tm *
localtime(const time_t * ptime)49 localtime(const time_t *ptime)
50 {
51     time_t time = *ptime;
52     struct tm * ptm;
53 
54     /* Check for invalid time value */
55     if (time < 0)
56     {
57         return 0;
58     }
59 
60     /* Never without */
61     if (!_tz_is_set)
62         _tzset();
63 
64     /* Check for overflow */
65 
66     /* Correct for timezone */
67     time -= _timezone;
68 #if 0
69     /* Correct for daylight saving */
70     if (_isdstime(time))
71     {
72         ptm->tm_isdst = 1;
73         time -= _dstbias;
74     }
75 #endif
76     ptm = gmtime(&time);
77 
78     return ptm;
79 }
80 
81