OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libc/] [time/] [current/] [src/] [timeutil.cxx] - Blame information for rev 810

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//========================================================================
2
//
3
//      timeutil.cxx
4
//
5
//      ISO C date and time implementation support functions
6
//
7
//========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):     jlarmour
43
// Contributors:  
44
// Date:          2000-04-28
45
// Purpose:       Provide support functions used by the ISO C date and time
46
//                implementation
47
// Description:   This file provides the functions:
48
//                  cyg_libc_time_normalize_structtm()
49
//                  cyg_libc_time_getzoneoffsets()
50
//                  cyg_libc_time_setzoneoffsets()
51
//                  cyg_libc_time_setdst()
52
//                  cyg_libc_time_itoa()
53
//                and the globals:
54
//                  cyg_libc_time_day_name
55
//                  cyg_libc_time_day_name_len
56
//                  cyg_libc_time_month_name
57
//                  cyg_libc_time_month_name_len
58
//                  cyg_libc_time_month_lengths
59
//                  cyg_libc_time_current_dst_stat
60
//                  cyg_libc_time_current_std_offset
61
//                  cyg_libc_time_current_dst_offset
62
// Usage:         
63
//
64
//####DESCRIPTIONEND####
65
//
66
//========================================================================
67
 
68
// CONFIGURATION
69
 
70
#include <pkgconf/libc_time.h>          // C library configuration
71
 
72
// INCLUDES
73
 
74
// define these functions as outline, not inline, functions in this file
75
#define CYGPRI_LIBC_TIME_GETZONEOFFSETS_INLINE extern "C"
76
#define CYGPRI_LIBC_TIME_SETZONEOFFSETS_INLINE extern "C"
77
#define CYGPRI_LIBC_TIME_SETDST_INLINE extern "C"
78
 
79
#include <cyg/infra/cyg_type.h>    // Common type definitions and support
80
#include <cyg/libc/time/timeutil.h>// Header for this file
81
#include <time.h>                  // Main date and time definitions
82
#include <stdlib.h>                // for div() and abs()
83
 
84
 
85
// GLOBALS
86
 
87
// FIXME: PR19440 - const char & -fwritable-strings don't mix
88
const char cyg_libc_time_day_name[7][10] = {
89
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
90
    "Saturday"
91
};
92
const cyg_uint8 cyg_libc_time_day_name_len[7] = { 6, 6, 7, 9, 8, 6, 8 };
93
 
94
// FIXME: PR19440 - const char & -fwritable-strings don't mix
95
const char cyg_libc_time_month_name[12][10] = {
96
    "January", "February", "March", "April", "May", "June",
97
    "July", "August", "September", "October", "November", "December"
98
};
99
const cyg_uint8 cyg_libc_time_month_name_len[12] = { 7, 8, 5, 5, 3, 4,
100
                                                     4, 6, 9, 7, 8, 8 };
101
 
102
const cyg_uint8 cyg_libc_time_month_lengths[2][12] = {
103
    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
104
    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
105
};
106
 
107
Cyg_libc_time_dst cyg_libc_time_current_dst_stat =
108
     (Cyg_libc_time_dst)CYGNUM_LIBC_TIME_DST_DEFAULT_STATE;
109
time_t cyg_libc_time_current_std_offset =
110
     (time_t)CYGNUM_LIBC_TIME_STD_DEFAULT_OFFSET;
111
time_t cyg_libc_time_current_dst_offset =
112
     (time_t)CYGNUM_LIBC_TIME_DST_DEFAULT_OFFSET;
113
 
114
 
115
// FUNCTIONS
116
 
117
////////////////////////////////////////
118
// cyg_libc_time_normalize_structtm() //
119
////////////////////////////////////////
120
//
121
// cyg_libc_time_normalize_structtm() will adjust the fields of a struct tm
122
// so that they are within the normal ranges expected.
123
// tm_wday, tm_yday and tm_isdst are ignored
124
 
125
externC void
126
cyg_libc_time_normalize_structtm( struct tm *timeptr )
127
{
128
    div_t t;
129
 
130
    CYG_REPORT_FUNCNAME("cyg_libc_time_normalize_structtm");
131
    CYG_REPORT_FUNCARG1("timeptr is at address %08x", timeptr);
132
 
133
    // normalize seconds to 0..59
134
    if ((timeptr->tm_sec < 0) || (timeptr->tm_sec > 59)) {
135
        t = div(timeptr->tm_sec, 60);
136
        while (t.rem < 0) {
137
            t.rem += 60;
138
            --t.quot;
139
        }
140
        timeptr->tm_min += t.quot;
141
        timeptr->tm_sec = t.rem;
142
    } // if
143
 
144
    // normalize minutes to 0..59
145
    if ((timeptr->tm_min < 0) || (timeptr->tm_min > 59)) {
146
        t = div(timeptr->tm_min, 60);
147
        while (t.rem < 0) {
148
            t.rem += 60;
149
            --t.quot;
150
        }
151
        timeptr->tm_hour += t.quot;
152
        timeptr->tm_min = t.rem;
153
    } // if
154
 
155
    // normalize hours to 0..23
156
    if ((timeptr->tm_hour < 0) || (timeptr->tm_hour > 23)) {
157
        t = div(timeptr->tm_hour, 24);
158
        while (t.rem < 0) {
159
            t.rem += 24;
160
            --t.quot;
161
        }
162
        timeptr->tm_mday += t.quot;
163
        timeptr->tm_hour = t.rem;
164
    } // if
165
 
166
    // we wait before normalizing tm_mday as per ISO C 7.12.2.3 (although
167
    // actually it only makes sense if you think about it
168
 
169
    // normalize months to 0..11
170
    if ((timeptr->tm_mon < 0) || (timeptr->tm_mon > 11)) {
171
        t = div(timeptr->tm_mon, 12);
172
        while (t.rem < 0) {
173
            t.rem += 12;
174
            --t.quot;
175
        }
176
        timeptr->tm_year += t.quot;
177
        timeptr->tm_mon = t.rem;
178
    } // if
179
 
180
    // now tm_mday which needs to go to 1..31
181
    cyg_bool leap = cyg_libc_time_year_is_leap(timeptr->tm_year);
182
 
183
    while (timeptr->tm_mday < 1) {
184
        // move back a month
185
 
186
        if (--timeptr->tm_mon < 0) {
187
            --timeptr->tm_year;
188
            timeptr->tm_mon = 11;
189
            leap = cyg_libc_time_year_is_leap(timeptr->tm_year);
190
        } // if
191
 
192
        // we move backward the number of days in the _new_ current month
193
        timeptr->tm_mday += cyg_libc_time_month_lengths[leap][timeptr->tm_mon];
194
 
195
    } // while
196
 
197
    while (timeptr->tm_mday >
198
           cyg_libc_time_month_lengths[leap][timeptr->tm_mon]) {
199
 
200
        // move forward a month
201
 
202
        // we move forward the number of days in the _old_ current month
203
        timeptr->tm_mday -= cyg_libc_time_month_lengths[leap][timeptr->tm_mon];
204
 
205
        if (++timeptr->tm_mon > 11) {
206
            ++timeptr->tm_year;
207
            timeptr->tm_mon = 0;
208
            leap = cyg_libc_time_year_is_leap(timeptr->tm_year);
209
        } // if
210
 
211
    } // while
212
 
213
    CYG_REPORT_RETURN();
214
 
215
} // cyg_libc_time_normalize_structtm()
216
 
217
 
218
//////////////////////////
219
// cyg_libc_time_itoa() //
220
//////////////////////////
221
//
222
// This converts num to a string and puts it into s padding with
223
// "0"'s if padzero is set, or spaces otherwise if necessary.
224
// The number of chars written to s is returned
225
//
226
 
227
// This implementation is probably suboptimal in terms of performance
228
// but there wouldn't be much in it with only 11 chars max to convert :-/.
229
// Actually FIXME: what if someone passes a width >11
230
 
231
externC cyg_ucount8
232
cyg_libc_time_itoa( cyg_uint8 *s, cyg_int32 num, cyg_uint8 width,
233
                    cyg_bool padzero )
234
{
235
    CYG_REPORT_FUNCNAMETYPE("cyg_libc_time_itoa", "returning %d");
236
    CYG_REPORT_FUNCARG4( "s=%08x, num=%d, width=%d, padzero=%d",
237
                         s, num, width, padzero );
238
 
239
    CYG_CHECK_DATA_PTR(s, "input string not a valid pointer");
240
 
241
    // special case for zero otherwise we'd have to treat it specially later
242
    // on anyway
243
 
244
    if (num==0) {
245
        cyg_ucount8 i;
246
 
247
        for (i=0; i<width; ++i)
248
            s[i] = padzero ? '0' : ' ';
249
        CYG_REPORT_RETVAL(i);
250
        return i;
251
    }
252
 
253
    // return value
254
    cyg_ucount8 ret=0;
255
 
256
    // Pre-fiddle for negative numbers
257
    if ((num < 0) && (width > 0)) {
258
        *s++ = '-';
259
        --width;
260
        num = abs(num);
261
        ++ret;
262
    }
263
 
264
    cyg_ucount32 i;
265
    cyg_bool reachednum = false;
266
    cyg_uint8 c, j;
267
 
268
    // i starts off with factor of 10 digits - which is the string length
269
    // of a positive 32-bit number
270
    for (i=1000000000, j=10; i>0; i/=10, --j) {
271
        c = (num / i);
272
 
273
        if (!reachednum && c==0) {
274
            if (j <= width) {
275
                *s++ = padzero ?  '0' : ' ';
276
                ++ret;
277
            }
278
        } // if
279
        else {
280
            *s++ = c + '0';
281
            ++ret;
282
            reachednum = true;
283
        }
284
        num %= i;
285
    } // for
286
 
287
    CYG_POSTCONDITION(ret >= width, "Didn't output enough chars!");
288
 
289
    CYG_REPORT_RETVAL(ret);
290
    return ret;
291
} // cyg_libc_time_itoa()
292
 
293
 
294
// EOF timeutil.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.