File: _PDCLIB_localsub.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 (164 lines) | stat: -rw-r--r-- 3,837 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
/* _PDCLIB_localsub( struct state const *, time_t const *, int_fast32_t, struct tm * const )

   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"

/* The easy way to behave "as if no library function calls" localtime
   is to not call it, so we drop its guts into "localsub", which can be
   freely called. (And no, the PANS doesn't require the above behavior,
   but it *is* desirable.)

   If successful and SETNAME is nonzero,
   set the applicable parts of tzname, timezone and altzone;
   however, it's OK to omit this step if the timezone is POSIX-compatible,
   since in that case tzset should have already done this step correctly.
   SETNAME's type is intfast32_t for compatibility with gmtsub,
   but it is actually a boolean and its value should be 0 or 1.
*/

/*ARGSUSED*/
struct tm * _PDCLIB_localsub( struct state const * sp, time_t const * timep, int_fast32_t setname, struct tm * const tmp )
{
    const struct ttinfo * ttisp;
    int                   i;
    struct tm *           result;
    const time_t          t = *timep;

    if ( sp == NULL )
    {
        /* Don't bother to set tzname etc.; tzset has already done it.  */
        return _PDCLIB_gmtsub( &_PDCLIB_gmtmem, timep, 0, tmp );
    }

    if ( ( sp->goback && t < sp->ats[ 0 ] ) || ( sp->goahead && t > sp->ats[ sp->timecnt - 1 ] ) )
    {
        time_t newt = t;
        time_t seconds;
        time_t years;

        if ( t < sp->ats[ 0 ] )
        {
            seconds = sp->ats[ 0 ] - t;
        }
        else
        {
            seconds = t - sp->ats[ sp->timecnt - 1 ];
        }

        --seconds;
        years = ( seconds / SECSPERREPEAT + 1 ) * YEARSPERREPEAT;
        seconds = years * AVGSECSPERYEAR;

        if ( t < sp->ats[ 0 ] )
        {
            newt += seconds;
        }
        else
        {
            newt -= seconds;
        }

        if ( newt < sp->ats[ 0 ] || newt > sp->ats[ sp->timecnt - 1 ] )
        {
            return NULL;    /* "cannot happen" */
        }

        result = _PDCLIB_localsub( sp, &newt, setname, tmp );

        if ( result )
        {
            int_fast64_t newy;

            newy = result->tm_year;

            if ( t < sp->ats[ 0 ] )
            {
                newy -= years;
            }
            else
            {
                newy += years;
            }

            if ( ! ( _PDCLIB_INT_MIN <= newy && newy <= _PDCLIB_INT_MAX ) )
            {
                return NULL;
            }

            result->tm_year = newy;
        }

        return result;
    }

    if ( sp->timecnt == 0 || t < sp->ats[ 0 ] )
    {
        i = sp->defaulttype;
    }
    else
    {
        int lo = 1;
        int hi = sp->timecnt;

        while ( lo < hi )
        {
            int mid = ( lo + hi ) >> 1;

            if ( t < sp->ats[ mid ] )
            {
                hi = mid;
            }
            else
            {
                lo = mid + 1;
            }
        }

        i = (int) sp->types[ lo - 1 ];
    }

    ttisp = &sp->ttis[ i ];

    /* To get (wrong) behavior that's compatible with System V Release 2.0
       you'd replace the statement below with
       t += ttisp->utoff;
       timesub( &t, 0L, sp, tmp );
    */
    result = _PDCLIB_timesub( &t, ttisp->utoff, sp, tmp );

    if ( result )
    {
        result->tm_isdst = ttisp->isdst;
#ifdef TM_ZONE
        result->TM_ZONE = (char *) &sp->chars[ ttisp->desigidx ];
#endif /* defined TM_ZONE */

        if ( setname )
        {
            _PDCLIB_update_tzname_etc( sp, ttisp );
        }
    }

    return result;
}

#endif

#ifdef TEST

#include "_PDCLIB_test.h"

int main( void )
{
#ifndef REGTEST
#endif

    return TEST_RESULTS;
}

#endif