File: _PDCLIB_timesub.c

package info (click to toggle)
libconvert-binary-c-perl 0.86-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,264 kB
  • sloc: ansic: 47,836; perl: 4,980; yacc: 2,143; makefile: 61
file content (190 lines) | stat: -rw-r--r-- 4,391 bytes parent folder | download | duplicates (3)
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
/* _PDCLIB_timesub( const time_t *, int_fast32_t, const struct state *, struct tm * )

   This file is part of the Public Domain C Library (PDCLib).
   Permission is granted to use, modify, and / or redistribute at will.
*/

#ifndef REGTEST

#include "pdclib/_PDCLIB_tzcode.h"

/* Return the number of leap years through the end of the given year
   where, to make the math easy, the answer for year zero is defined as zero.
*/
static int leaps_thru_end_of_nonneg( int y )
{
    return y / 4 - y / 100 + y / 400;
}

static int leaps_thru_end_of( const int y )
{
    return ( y < 0
        ? -1 - leaps_thru_end_of_nonneg( -1 - y )
        : leaps_thru_end_of_nonneg( y ) );
}

struct tm * _PDCLIB_timesub( const time_t * timep, int_fast32_t offset, const struct state * sp, struct tm * tmp )
{
    const struct lsinfo * lp;
    time_t                tdays;
    int                   idays;  /* unsigned would be so 2003 */
    int_fast64_t          rem;
    int                   y;
    const int *           ip;
    int_fast64_t          corr;
    bool                  hit;
    int                   i;

    corr = 0;
    hit = false;
    i = ( sp == NULL ) ? 0 : sp->leapcnt;

    while ( --i >= 0 )
    {
        lp = &sp->lsis[ i ];
        if ( *timep >= lp->trans )
        {
            corr = lp->corr;
            hit = ( *timep == lp->trans && ( i == 0 ? 0 : lp[ -1 ].corr ) < corr );
            break;
        }
    }

    y = EPOCH_YEAR;
    tdays = *timep / SECSPERDAY;
    rem = *timep % SECSPERDAY;

    while ( tdays < 0 || tdays >= year_lengths[ _PDCLIB_is_leap( y ) ] )
    {
        int    newy;
        time_t tdelta;
        int    idelta;
        int    leapdays;

        tdelta = tdays / DAYSPERLYEAR;

        if ( ! ( ( ! _PDCLIB_TYPE_SIGNED( time_t ) || _PDCLIB_INT_MIN <= tdelta ) && tdelta <= _PDCLIB_INT_MAX ) )
        {
            goto out_of_range;
        }

        idelta = tdelta;

        if ( idelta == 0 )
        {
            idelta = ( tdays < 0 ) ? -1 : 1;
        }

        newy = y;

        if ( _PDCLIB_increment_overflow( &newy, idelta ) )
        {
            goto out_of_range;
        }

        leapdays = leaps_thru_end_of( newy - 1 ) - leaps_thru_end_of( y - 1 );
        tdays -= ( (time_t)newy - y ) * DAYSPERNYEAR;
        tdays -= leapdays;
        y = newy;
    }

    /* Given the range, we can now fearlessly cast... */
    idays = tdays;
    rem += offset - corr;

    while ( rem < 0 )
    {
        rem += SECSPERDAY;
        --idays;
    }

    while ( rem >= SECSPERDAY )
    {
        rem -= SECSPERDAY;
        ++idays;
    }

    while ( idays < 0 )
    {
        if ( _PDCLIB_increment_overflow( &y, -1 ) )
        {
            goto out_of_range;
        }

        idays += year_lengths[ _PDCLIB_is_leap( y ) ];
    }

    while ( idays >= year_lengths[ _PDCLIB_is_leap( y ) ] )
    {
        idays -= year_lengths[ _PDCLIB_is_leap( y ) ];

        if ( _PDCLIB_increment_overflow( &y, 1 ) )
        {
            goto out_of_range;
        }
    }

    tmp->tm_year = y;

    if ( _PDCLIB_increment_overflow( &tmp->tm_year, -TM_YEAR_BASE ) )
    {
        goto out_of_range;
    }

    tmp->tm_yday = idays;
    /* The "extra" mods below avoid overflow problems. */
    tmp->tm_wday = EPOCH_WDAY +
        ( ( y - EPOCH_YEAR ) % DAYSPERWEEK ) *
        ( DAYSPERNYEAR % DAYSPERWEEK ) +
        leaps_thru_end_of( y - 1 ) -
        leaps_thru_end_of( EPOCH_YEAR - 1 ) +
        idays;
    tmp->tm_wday %= DAYSPERWEEK;

    if ( tmp->tm_wday < 0 )
    {
        tmp->tm_wday += DAYSPERWEEK;
    }

    tmp->tm_hour = (int)( rem / SECSPERHOUR );
    rem %= SECSPERHOUR;
    tmp->tm_min = (int)( rem / SECSPERMIN );

    /* A positive leap second requires a special
       representation. This uses "... ??:59:60" et seq.
    */
    tmp->tm_sec = (int) ( rem % SECSPERMIN ) + hit;
    ip = mon_lengths[ _PDCLIB_is_leap( y ) ];

    for ( tmp->tm_mon = 0; idays >= ip[ tmp->tm_mon ]; ++( tmp->tm_mon ) )
    {
        idays -= ip[ tmp->tm_mon ];
    }

    tmp->tm_mday = (int)( idays + 1 );
    tmp->tm_isdst = 0;
#ifdef TM_GMTOFF
    tmp->TM_GMTOFF = offset;
#endif /* defined TM_GMTOFF */
    return tmp;

 out_of_range:
    *_PDCLIB_errno_func() = _PDCLIB_EOVERFLOW;
    return NULL;
}

#endif

#ifdef TEST

#include "_PDCLIB_test.h"

int main( void )
{
#ifndef REGTEST
#endif

    return TEST_RESULTS;
}

#endif