File: mktime.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (192 lines) | stat: -rw-r--r-- 6,724 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*****************************************************************************/
/*                                                                           */
/*                                 mktime.c                                  */
/*                                                                           */
/*           Make calendar time from broken down time and cleanup            */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/* (C) 2002      Ullrich von Bassewitz                                       */
/*               Wacholderweg 14                                             */
/*               D-70597 Stuttgart                                           */
/* EMail:        uz@musoftware.de                                            */
/*                                                                           */
/*                                                                           */
/* This software is provided 'as-is', without any expressed or implied       */
/* warranty.  In no event will the authors be held liable for any damages    */
/* arising from the use of this software.                                    */
/*                                                                           */
/* Permission is granted to anyone to use this software for any purpose,     */
/* including commercial applications, and to alter it and redistribute it    */
/* freely, subject to the following restrictions:                            */
/*                                                                           */
/* 1. The origin of this software must not be misrepresented; you must not   */
/*    claim that you wrote the original software. If you use this software   */
/*    in a product, an acknowledgment in the product documentation would be  */
/*    appreciated but is not required.                                       */
/* 2. Altered source versions must be plainly marked as such, and must not   */
/*    be misrepresented as being the original software.                      */
/* 3. This notice may not be removed or altered from any source              */
/*    distribution.                                                          */
/*                                                                           */
/*****************************************************************************/



#include <limits.h>
#include <stdlib.h>
#include <time.h>



/*****************************************************************************/
/*                                   Data                                    */
/*****************************************************************************/



#define JANUARY         0
#define FEBRUARY        1
#define DECEMBER       11
#define JAN_1_1970      4               /* 1/1/1970 is a thursday */



static const unsigned char MonthLength [] = {
    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static const unsigned MonthDays [] = {
    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};



/*****************************************************************************/
/*                                   Code                                    */
/*****************************************************************************/



static unsigned char __fastcall__ IsLeapYear (unsigned Year)
/* Returns 1 if the given year is a leap year */
{
    return (((Year % 4) == 0) && ((Year % 100) != 0 || (Year % 400) == 0));
}



time_t __fastcall__ mktime (register struct tm* TM)
/* Make a time in seconds since 1/1/1970 from the broken down time in TM.
** A call to mktime does also correct the time in TM to contain correct
** values.
*/
{
    register div_t D;
    int Max;
    unsigned DayCount;

    /* Check if TM is valid */
    if (TM == 0) {
        /* Invalid data */
        goto Error;
    }

    /* Adjust seconds. */
    D = div (TM->tm_sec, 60);
    TM->tm_sec = D.rem;

    /* Adjust minutes */
    if (TM->tm_min + D.quot < 0) {
        goto Error;
    }
    TM->tm_min += D.quot;
    D = div (TM->tm_min, 60);
    TM->tm_min = D.rem;

    /* Adjust hours */
    if (TM->tm_hour + D.quot < 0) {
        goto Error;
    }
    TM->tm_hour += D.quot;
    D = div (TM->tm_hour, 24);
    TM->tm_hour = D.rem;

    /* Adjust days */
    if (TM->tm_mday + D.quot < 0) {
        goto Error;
    }
    TM->tm_mday += D.quot;

    /* Adjust month and year. This is an iterative process, since changing
    ** the month will change the allowed days for this month.
    */
    while (1) {

        /* Make sure, month is in the range 0..11 */
        D = div (TM->tm_mon, 12);
        TM->tm_mon = D.rem;
        if (TM->tm_year + D.quot < 0) {
            goto Error;
        }
        TM->tm_year += D.quot;

        /* Now check if mday is in the correct range, if not, correct month
        ** and eventually year and repeat the process.
        */
        if (TM->tm_mon == FEBRUARY && IsLeapYear (TM->tm_year + 1900)) {
            Max = 29;
        } else {
            Max = MonthLength[TM->tm_mon];
        }
        if (TM->tm_mday > Max) {
            /* Must correct month and eventually, year */
            if (TM->tm_mon == DECEMBER) {
                TM->tm_mon = JANUARY;
                ++TM->tm_year;
            } else {
                ++TM->tm_mon;
            }
            TM->tm_mday -= Max;
        } else {
            /* Done */
            break;
        }
    }

    /* Ok, all time/date fields are now correct. Calculate the days in this
    ** year.
    */
    TM->tm_yday = MonthDays[TM->tm_mon] + TM->tm_mday - 1;
    if (TM->tm_mon > FEBRUARY && IsLeapYear (TM->tm_year + 1900)) {
        ++TM->tm_yday;
    }

    /* Calculate days since 1/1/1970. In the complete epoch (1/1/1970 to
    ** somewhere in 2038) all years dividable by 4 are leap years, so
    ** dividing by 4 gives the days that must be added cause of leap years.
    ** (and the last leap year before 1970 was 1968)
    */
    DayCount = ((unsigned) (TM->tm_year-70)) * 365U +
               (((unsigned) (TM->tm_year-(68+1))) / 4) +
               TM->tm_yday;

    /* Calculate the weekday */
    TM->tm_wday = (JAN_1_1970 + DayCount) % 7;

    /* No (US) daylight saving (for now) */
    TM->tm_isdst = 0;

    /* Return seconds since 1970 */
    return DayCount * 86400UL +
           ((unsigned) TM->tm_hour) * 3600UL +
           ((unsigned) TM->tm_min) * 60U +
           ((unsigned) TM->tm_sec) -
           _tz.timezone;

Error:
    /* Error exit */
    return (time_t) -1L;
}