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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 786 skrzyp
#ifndef CYGONCE_LIBC_TIME_INL
2
#define CYGONCE_LIBC_TIME_INL
3
//===========================================================================
4
//
5
//      time.inl
6
//
7
//      Inline implementations of date and time routines from 
8
//
9
//===========================================================================
10
// ####ECOSGPLCOPYRIGHTBEGIN####
11
// -------------------------------------------
12
// This file is part of eCos, the Embedded Configurable Operating System.
13
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
14
//
15
// eCos is free software; you can redistribute it and/or modify it under
16
// the terms of the GNU General Public License as published by the Free
17
// Software Foundation; either version 2 or (at your option) any later
18
// version.
19
//
20
// eCos is distributed in the hope that it will be useful, but WITHOUT
21
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23
// for more details.
24
//
25
// You should have received a copy of the GNU General Public License
26
// along with eCos; if not, write to the Free Software Foundation, Inc.,
27
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
28
//
29
// As a special exception, if other files instantiate templates or use
30
// macros or inline functions from this file, or you compile this file
31
// and link it with other works to produce a work based on this file,
32
// this file does not by itself cause the resulting work to be covered by
33
// the GNU General Public License. However the source code for this file
34
// must still be made available in accordance with section (3) of the GNU
35
// General Public License v2.
36
//
37
// This exception does not invalidate any other reasons why a work based
38
// on this file might be covered by the GNU General Public License.
39
// -------------------------------------------
40
// ####ECOSGPLCOPYRIGHTEND####
41
//===========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    jlarmour
45
// Contributors:
46
// Date:         1999-02-25
47
// Purpose:      Provide inline implementations for some of the date and time
48
//               routines declared in  for ISO C section 7.12 and
49
//               POSIX 1003.1 8.3.4-8.3.7
50
// Description:
51
// Usage:        Do not include this file directly. Instead include 
52
//
53
//####DESCRIPTIONEND####
54
//
55
//===========================================================================
56
 
57
// CONFIGURATION
58
 
59
#include           // C library configuration
60
 
61
// INCLUDES
62
 
63
#include     // Common type definitions and support
64
#include                   // Header for this file
65
#include      // Assertion infrastructure
66
#include     // Tracing infrastructure
67
 
68
// DEFINES
69
 
70
// The following are overriden by the libc implementation to get a non-inline
71
// version to prevent duplication of code
72
 
73
#ifndef CYGPRI_LIBC_TIME_ASCTIME_R_INLINE
74
# define CYGPRI_LIBC_TIME_ASCTIME_R_INLINE extern __inline__
75
#endif
76
 
77
#ifndef CYGPRI_LIBC_TIME_CTIME_R_INLINE
78
# define CYGPRI_LIBC_TIME_CTIME_R_INLINE extern __inline__
79
#endif
80
 
81
#ifndef CYGPRI_LIBC_TIME_GMTIME_R_INLINE
82
# define CYGPRI_LIBC_TIME_GMTIME_R_INLINE extern __inline__
83
#endif
84
 
85
#ifndef CYGPRI_LIBC_TIME_LOCALTIME_R_INLINE
86
# define CYGPRI_LIBC_TIME_LOCALTIME_R_INLINE extern __inline__
87
#endif
88
 
89
#ifndef CYGPRI_LIBC_TIME_DIFFTIME_INLINE
90
# define CYGPRI_LIBC_TIME_DIFFTIME_INLINE extern __inline__
91
#endif
92
 
93
#ifndef CYGPRI_LIBC_TIME_MKTIME_INLINE
94
# define CYGPRI_LIBC_TIME_MKTIME_INLINE extern __inline__
95
#endif
96
 
97
#ifndef CYGPRI_LIBC_TIME_ASCTIME_INLINE
98
# define CYGPRI_LIBC_TIME_ASCTIME_INLINE extern __inline__
99
#endif
100
 
101
#ifndef CYGPRI_LIBC_TIME_CTIME_INLINE
102
# define CYGPRI_LIBC_TIME_CTIME_INLINE extern __inline__
103
#endif
104
 
105
#ifndef CYGPRI_LIBC_TIME_GMTIME_INLINE
106
# define CYGPRI_LIBC_TIME_GMTIME_INLINE extern __inline__
107
#endif
108
 
109
#ifndef CYGPRI_LIBC_TIME_LOCALTIME_INLINE
110
# define CYGPRI_LIBC_TIME_LOCALTIME_INLINE extern __inline__
111
#endif
112
 
113
#ifndef CYGPRI_LIBC_TIME_GETZONEOFFSETS_INLINE
114
# define CYGPRI_LIBC_TIME_GETZONEOFFSETS_INLINE extern __inline__
115
#endif
116
 
117
#ifndef CYGPRI_LIBC_TIME_SETZONEOFFSETS_INLINE
118
# define CYGPRI_LIBC_TIME_SETZONEOFFSETS_INLINE extern __inline__
119
#endif
120
 
121
#ifndef CYGPRI_LIBC_TIME_SETDST_INLINE
122
# define CYGPRI_LIBC_TIME_SETDST_INLINE extern __inline__
123
#endif
124
 
125
#define CYGNUM_LIBC_TIME_EPOCH_WDAY       4  // Jan 1st 1970 was a Thursday
126
 
127
#ifdef __cplusplus
128
extern "C" {
129
#endif
130
 
131
// EXTERNS
132
 
133
// These are used in the dst access functions below. Do not access these
134
// directly - use the functions declared in time.h instead
135
 
136
extern Cyg_libc_time_dst cyg_libc_time_current_dst_stat;
137
extern time_t cyg_libc_time_current_std_offset;
138
extern time_t cyg_libc_time_current_dst_offset;
139
 
140
// INLINE FUNCTIONS
141
 
142
//===========================================================================
143
//
144
// Utility functions
145
 
146
//////////////////////////////////
147
// cyg_libc_time_year_is_leap() //
148
//////////////////////////////////
149
//
150
// This returns true if the year is a leap year.
151
// The argument is of type int in line with struct tm
152
//
153
 
154
static __inline__ cyg_bool
155
cyg_libc_time_year_is_leap( int __year )
156
{
157
    cyg_bool _leap=false;
158
 
159
    if (!(__year % 400))
160
        _leap = true;
161
    else if (!(__year % 4) && (__year % 100))
162
        _leap = true;
163
    return _leap;
164
} // cyg_libc_time_year_is_leap()
165
 
166
 
167
////////////////////////////////////
168
// cyg_libc_time_getzoneoffsets() //
169
////////////////////////////////////
170
//
171
// This function retrieves the current state of the Daylight Savings Time
172
// and the offsets of both STD and DST
173
// The offsets are both in time_t's i.e. seconds
174
//
175
 
176
CYGPRI_LIBC_TIME_GETZONEOFFSETS_INLINE Cyg_libc_time_dst
177
cyg_libc_time_getzoneoffsets( time_t *__stdoffset, time_t *__dstoffset )
178
{
179
    CYG_REPORT_FUNCNAMETYPE("cyg_libc_time_getzoneoffsets",
180
                            "returning DST state %d");
181
    CYG_REPORT_FUNCARG2("__stdoffset is at address %08x, "
182
                        "__dstoffset is at %08x", __stdoffset, __dstoffset);
183
 
184
    CYG_CHECK_DATA_PTR(__stdoffset, "__stdoffset is not a valid pointer!");
185
    CYG_CHECK_DATA_PTR(__dstoffset, "__dstoffset is not a valid pointer!");
186
 
187
    *__stdoffset = cyg_libc_time_current_std_offset;
188
    *__dstoffset = cyg_libc_time_current_dst_offset;
189
 
190
    CYG_REPORT_RETVAL(cyg_libc_time_current_dst_stat);
191
 
192
    return cyg_libc_time_current_dst_stat;
193
} // cyg_libc_time_getzoneoffsets()
194
 
195
 
196
////////////////////////////////////
197
// cyg_libc_time_setzoneoffsets() //
198
////////////////////////////////////
199
//
200
// This function sets the offsets used when Daylight Savings Time is enabled
201
// or disabled. The offsets are in time_t's i.e. seconds
202
//
203
 
204
CYGPRI_LIBC_TIME_SETZONEOFFSETS_INLINE void
205
cyg_libc_time_setzoneoffsets( time_t __stdoffset, time_t __dstoffset )
206
{
207
    CYG_REPORT_FUNCNAME("cyg_libc_time_setzoneoffsets");
208
    CYG_REPORT_FUNCARG2DV(__stdoffset, __dstoffset);
209
 
210
    cyg_libc_time_current_std_offset = __stdoffset;
211
    cyg_libc_time_current_dst_offset = __dstoffset;
212
 
213
    CYG_REPORT_RETURN();
214
} // cyg_libc_time_setzoneoffsets()
215
 
216
 
217
////////////////////////////
218
// cyg_libc_time_setdst() //
219
////////////////////////////
220
//
221
// This function sets the state of Daylight Savings Time: on, off, or unknown
222
//
223
 
224
CYGPRI_LIBC_TIME_SETDST_INLINE void
225
cyg_libc_time_setdst( Cyg_libc_time_dst __state )
226
{
227
    CYG_REPORT_FUNCNAME("cyg_libc_time_setdst");
228
    CYG_REPORT_FUNCARG1("__state=%d", __state);
229
 
230
    cyg_libc_time_current_dst_stat = __state;
231
 
232
    CYG_REPORT_RETURN();
233
} // cyg_libc_time_setdst()
234
 
235
 
236
 
237
//===========================================================================
238
//
239
// POSIX 1003.1 functions
240
 
241
/////////////////////////////////
242
// asctime_r() - POSIX.1 8.3.4 //
243
/////////////////////////////////
244
//
245
// This returns a textual representation of a struct tm, and writes
246
// the string to return into __buf
247
//
248
 
249
#ifdef CYGFUN_LIBC_TIME_POSIX
250
# define __asctime_r asctime_r
251
#else
252
// prototype internal function
253
__externC char *
254
__asctime_r( const struct tm *__timeptr, char *__buf );
255
#endif
256
 
257
#ifdef CYGIMP_LIBC_TIME_ASCTIME_R_INLINE
258
 
259
#include  // for cyg_libc_time_{day,month}_name
260
                                    // and cyg_libc_time_itoa()
261
#include                  // for memcpy()
262
 
263
CYGPRI_LIBC_TIME_ASCTIME_R_INLINE char *
264
__asctime_r( const struct tm *__timeptr, char *__buf )
265
{
266
    cyg_ucount8 __i;
267
 
268
    // These initializers are [4] since C++ dictates you _must_ leave space
269
    // for the trailing '\0', even though ISO C says you don't need to!
270
 
271
    CYG_REPORT_FUNCNAMETYPE("asctime_r", "returning \"%s\"");
272
    CYG_REPORT_FUNCARG2("__timeptr = %08x, __buf = %08x", __timeptr, __buf);
273
 
274
    // paranoia - most of these aren't required but could be helpful to
275
    // a programmer debugging their own app.
276
    CYG_CHECK_DATA_PTR(__timeptr, "__timeptr is not a valid pointer!");
277
    CYG_CHECK_DATA_PTR(__buf, "__buf is not a valid pointer!");
278
 
279
    CYG_PRECONDITION((__timeptr->tm_sec >= 0) && (__timeptr->tm_sec < 62),
280
                     "__timeptr->tm_sec out of range!");
281
    CYG_PRECONDITION((__timeptr->tm_min >= 0) && (__timeptr->tm_min < 60),
282
                     "__timeptr->tm_min out of range!");
283
    CYG_PRECONDITION((__timeptr->tm_hour >= 0) && (__timeptr->tm_hour < 24),
284
                     "__timeptr->tm_hour out of range!");
285
    // Currently I don't check _actual_ numbers of days in each month here
286
    // FIXME: No reason why not though
287
    CYG_PRECONDITION((__timeptr->tm_mday >= 1) && (__timeptr->tm_mday < 32),
288
                     "__timeptr->tm_mday out of range!");
289
    CYG_PRECONDITION((__timeptr->tm_mon >= 0) && (__timeptr->tm_mon < 12),
290
                     "__timeptr->tm_mon out of range!");
291
    CYG_PRECONDITION((__timeptr->tm_wday >= 0) && (__timeptr->tm_wday < 7),
292
                     "__timeptr->tm_wday out of range!");
293
    CYG_PRECONDITION((__timeptr->tm_yday >= 0) && (__timeptr->tm_yday < 366),
294
                     "__timeptr->tm_yday out of range!");
295
    CYG_PRECONDITION((__timeptr->tm_year > -1900) &&
296
                     (__timeptr->tm_year < 8100),
297
                     "__timeptr->tm_year out of range!");
298
 
299
    // we can't use strftime because ISO C is stupid enough not to allow
300
    // the strings in asctime() to be localized. Duh.
301
 
302
    // day of the week
303
    memcpy(&__buf[0], cyg_libc_time_day_name[__timeptr->tm_wday], 3);
304
    __buf[3] = ' ';
305
 
306
    // month
307
    memcpy(&__buf[4], cyg_libc_time_month_name[__timeptr->tm_mon], 3);
308
    __buf[7] = ' ';
309
 
310
    __i = 8;
311
 
312
    // day of the month
313
    __i += cyg_libc_time_itoa( (cyg_uint8 *)&__buf[__i], __timeptr->tm_mday, 2,
314
                               true);
315
    __buf[__i++] = ' ';
316
 
317
    // hour
318
    __i += cyg_libc_time_itoa( (cyg_uint8 *)&__buf[__i], __timeptr->tm_hour, 2,
319
                               true);
320
    __buf[__i++] = ':';
321
 
322
    // minute
323
    __i += cyg_libc_time_itoa( (cyg_uint8 *)&__buf[__i], __timeptr->tm_min, 2,
324
                               true);
325
    __buf[__i++] = ':';
326
 
327
    // second
328
    __i += cyg_libc_time_itoa((cyg_uint8 *) &__buf[__i], __timeptr->tm_sec, 2,
329
                              true);
330
    __buf[__i++] = ' ';
331
 
332
    // year
333
    __i += cyg_libc_time_itoa( (cyg_uint8 *)&__buf[__i],
334
                               1900+__timeptr->tm_year, 0, true);
335
 
336
    __buf[__i++] = '\n';
337
    __buf[__i++] = '\0';
338
 
339
    CYG_REPORT_RETVAL(__buf);
340
    return __buf;
341
} // asctime_r()
342
 
343
#endif // ifdef CYGIMP_LIBC_TIME_ASCTIME_R_INLINE
344
 
345
////////////////////////////////
346
// gmtime_r() - POSIX.1 8.3.6 //
347
////////////////////////////////
348
//
349
// This converts a time_t into a struct tm expressed in UTC, and stores
350
// the result in the space occupied by __result
351
//
352
 
353
#ifdef CYGFUN_LIBC_TIME_POSIX
354
# define __gmtime_r gmtime_r
355
#else
356
// prototype internal function
357
__externC struct tm *
358
__gmtime_r( const time_t *__timer, struct tm *__result );
359
#endif
360
 
361
#ifdef CYGIMP_LIBC_TIME_GMTIME_R_INLINE
362
 
363
#include    // for cyg_libc_time_month_lengths
364
 
365
CYGPRI_LIBC_TIME_GMTIME_R_INLINE struct tm *
366
__gmtime_r( const time_t *__timer, struct tm *__result )
367
{
368
    time_t _tim;
369
    const cyg_uint8 *_months_p;
370
 
371
    CYG_REPORT_FUNCNAMETYPE("gmtime_r", "returning %08x");
372
    CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
373
    CYG_CHECK_DATA_PTR(__result, "__result is not a valid pointer!");
374
    CYG_REPORT_FUNCARG2("*__timer=%d, __result is at %08x",
375
                        *__timer, __result);
376
 
377
#define CYGNUM_LIBC_TIME_SECSPERDAY       (60*60*24)
378
#define CYGNUM_LIBC_TIME_SECSPERYEAR      (CYGNUM_LIBC_TIME_SECSPERDAY * 365)
379
#define CYGNUM_LIBC_TIME_SECSPERLEAPYEAR  (CYGNUM_LIBC_TIME_SECSPERDAY * 366)
380
 
381
    _tim = *__timer;
382
 
383
    // First, work out year. Start off with 1970 and work forwards or backwards
384
    // depending on the sign of _tim
385
    __result->tm_year = 70;
386
 
387
    // we also work out the day of the week of the start of the year as we
388
    // go along
389
    __result->tm_wday = CYGNUM_LIBC_TIME_EPOCH_WDAY;
390
 
391
    while (_tim < 0) {
392
        // Work backwards
393
 
394
        --__result->tm_year;
395
 
396
        // Check for a leap year.
397
        if (cyg_libc_time_year_is_leap(1900 + __result->tm_year)) {
398
            _tim += CYGNUM_LIBC_TIME_SECSPERLEAPYEAR;
399
            __result->tm_wday -= 366;
400
        } // if
401
        else {
402
            _tim += CYGNUM_LIBC_TIME_SECSPERYEAR;
403
            __result->tm_wday -= 365;
404
        } // else
405
 
406
    } // while
407
 
408
    while (_tim >= CYGNUM_LIBC_TIME_SECSPERYEAR) {
409
        // Work forwards
410
 
411
        if (cyg_libc_time_year_is_leap(1900 + __result->tm_year)) {
412
 
413
            // But if this is a leap year, its possible that we are in the
414
            // middle of the last "extra" day
415
            if (_tim < CYGNUM_LIBC_TIME_SECSPERLEAPYEAR)
416
                break;
417
 
418
            _tim -= CYGNUM_LIBC_TIME_SECSPERLEAPYEAR;
419
            __result->tm_wday += 366;
420
        } // if
421
        else {
422
            _tim -= CYGNUM_LIBC_TIME_SECSPERYEAR;
423
            __result->tm_wday += 365;
424
        }
425
        ++__result->tm_year;
426
    } // while
427
 
428
    // Day of the year. We know _tim is +ve now
429
    CYG_ASSERT(_tim >= 0,
430
               "Number of seconds since start of year is negative!");
431
    __result->tm_yday = _tim / CYGNUM_LIBC_TIME_SECSPERDAY;
432
 
433
    // Day of the week. Normalize to be 0..6, and note that it might
434
    // be negative, so we have to deal with the modulus being
435
    // implementation-defined for -ve numbers (ISO C 6.3.5)
436
    __result->tm_wday = (((__result->tm_wday + __result->tm_yday)%7)+7)%7;
437
 
438
    // Month and Day of the month
439
 
440
    _months_p = cyg_libc_time_month_lengths[
441
        cyg_libc_time_year_is_leap(1900 + __result->tm_year) ? 1 : 0 ];
442
 
443
    __result->tm_mday = __result->tm_yday+1;
444
 
445
    for (__result->tm_mon = 0;
446
         __result->tm_mday > _months_p[__result->tm_mon];
447
         ++__result->tm_mon) {
448
 
449
        __result->tm_mday -= _months_p[__result->tm_mon];
450
 
451
    } // for
452
 
453
    _tim -= __result->tm_yday*CYGNUM_LIBC_TIME_SECSPERDAY;
454
 
455
    // hours, mins secs
456
    __result->tm_hour = (int) (_tim / 3600);
457
    _tim %= 3600;
458
    __result->tm_min  = (int) (_tim / 60);
459
    __result->tm_sec  = (int) (_tim % 60);
460
 
461
    __result->tm_isdst = 0; // gmtime always returns non-DST
462
 
463
    CYG_REPORT_RETVAL(__result);
464
 
465
    return __result;
466
} // gmtime_r()
467
 
468
#endif // ifdef CYGIMP_LIBC_TIME_GMTIME_R_INLINE
469
 
470
///////////////////////////////////
471
// localtime_r() - POSIX.1 8.3.7 //
472
///////////////////////////////////
473
//
474
// This converts a time_t into a struct tm expressed in local time, and
475
// stores the result in the space occupied by __result
476
//
477
 
478
#ifdef CYGFUN_LIBC_TIME_POSIX
479
# define __localtime_r localtime_r
480
#else
481
// prototype internal function
482
__externC struct tm *
483
__localtime_r( const time_t *__timer, struct tm *__result );
484
#endif
485
 
486
#ifdef CYGIMP_LIBC_TIME_LOCALTIME_R_INLINE
487
 
488
#include   // for cyg_libc_time_normalize_structtm()
489
 
490
CYGPRI_LIBC_TIME_LOCALTIME_R_INLINE struct tm *
491
__localtime_r( const time_t *__timer, struct tm *__result )
492
{
493
    time_t __stdoffset, __dstoffset;
494
    CYG_REPORT_FUNCNAMETYPE("localtime_r", "returning %08x");
495
    CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
496
    CYG_CHECK_DATA_PTR(__result, "__result is not a valid pointer!");
497
    CYG_REPORT_FUNCARG2("*__timer=%d, __result is at %08x",
498
                        *__timer, __result);
499
 
500
    __gmtime_r(__timer, __result);
501
 
502
    // Adjust for STD/DST
503
    __result->tm_isdst = cyg_libc_time_getzoneoffsets(&__stdoffset,
504
                                                      &__dstoffset);
505
 
506
    if (__result->tm_isdst == 0) { // STD
507
        __result->tm_sec += __stdoffset;
508
        cyg_libc_time_normalize_structtm( __result );
509
    } // if
510
    else if (__result->tm_isdst > 0) { // DST
511
        __result->tm_sec += __dstoffset;
512
        cyg_libc_time_normalize_structtm( __result );
513
    } // if
514
    // Don't do anything for tm_isdst == -1
515
 
516
    CYG_REPORT_RETVAL(__result);
517
 
518
    return __result;
519
} // localtime_r()
520
 
521
#endif // ifdef CYGIMP_LIBC_TIME_LOCALTIME_R_INLINE
522
 
523
 
524
///////////////////////////////
525
// ctime_r() - POSIX.1 8.3.5 //
526
///////////////////////////////
527
//
528
// This returns the equivalent of ctime() but writes to __buf
529
// to store the returned string
530
//
531
 
532
#ifdef CYGFUN_LIBC_TIME_POSIX
533
# define __ctime_r ctime_r
534
#else
535
// prototype internal function
536
__externC char *
537
__ctime_r( const time_t *__timer, char *__buf );
538
#endif
539
 
540
#ifdef CYGIMP_LIBC_TIME_CTIME_R_INLINE
541
 
542
CYGPRI_LIBC_TIME_CTIME_R_INLINE char *
543
__ctime_r( const time_t *__timer, char *__buf )
544
{
545
    struct tm _mytm;
546
 
547
    CYG_REPORT_FUNCNAMETYPE("ctime_r", "returning \"%s\"");
548
 
549
    CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
550
    CYG_CHECK_DATA_PTR(__buf, "__buf is not a valid pointer!");
551
 
552
    CYG_REPORT_FUNCARG2("*__timer = %d, __buf=%08x", *__timer, __buf);
553
 
554
    __localtime_r( __timer, &_mytm );
555
 
556
    __asctime_r(&_mytm, __buf);
557
 
558
    CYG_REPORT_RETVAL(__buf);
559
 
560
    return __buf;
561
} // ctime_r()
562
 
563
#endif // ifdef CYGIMP_LIBC_TIME_CTIME_R_INLINE
564
 
565
 
566
//===========================================================================
567
//
568
// ISO C functions
569
 
570
// Time manipulation functions - ISO C 7.12.2
571
 
572
/////////////////////////////////
573
// difftime() - ISO C 7.12.2.2 //
574
/////////////////////////////////
575
//
576
// This returns (__time1 - __time0) in seconds
577
//
578
 
579
#ifdef CYGIMP_LIBC_TIME_DIFFTIME_INLINE
580
 
581
CYGPRI_LIBC_TIME_DIFFTIME_INLINE double
582
difftime( time_t __time1, time_t __time0 )
583
{
584
    double _ret;
585
 
586
    CYG_REPORT_FUNCNAMETYPE("difftime", "returning %f");
587
    CYG_REPORT_FUNCARG2("__time1=%d, __time0=%d", __time1, __time0);
588
 
589
    _ret = (double)(__time1 - __time0);
590
 
591
    CYG_REPORT_RETVAL(_ret);
592
 
593
    return _ret;
594
} // difftime()
595
 
596
#endif // ifdef CYGIMP_LIBC_TIME_DIFFTIME_INLINE
597
 
598
///////////////////////////////
599
// mktime() - ISO C 7.12.2.3 //
600
///////////////////////////////
601
//
602
// This converts a "struct tm" to a "time_t"
603
//
604
 
605
#ifdef CYGIMP_LIBC_TIME_MKTIME_INLINE
606
 
607
#include   // for cyg_libc_time_normalize_structtm()
608
                                     // and cyg_libc_time_month_lengths
609
 
610
CYGPRI_LIBC_TIME_MKTIME_INLINE time_t
611
mktime( struct tm *__timeptr )
612
{
613
    time_t _ret;
614
    cyg_count16 _i;
615
    cyg_count32 _daycount;
616
    cyg_bool _leap;
617
 
618
    CYG_REPORT_FUNCNAMETYPE("mktime", "returning %d");
619
    CYG_REPORT_FUNCARG1( "__timeptr is at address %08x", __timeptr);
620
 
621
    CYG_CHECK_DATA_PTR(__timeptr, "__timeptr is not a valid pointer!");
622
 
623
    // First deal with STD/DST. If tm_isdst==-1 (the "autodetect" value)
624
    // we assume its already in UTC. FIXME: is this correct behaviour? Hmm....
625
 
626
#if 0
627
// FIXME: This doesn't seem to be the way to go
628
    if (__timeptr->tm_isdst == 0) { // STD
629
        // take _off_ the std offset to get us back to UTC from localtime
630
        __timeptr->tm_sec -= (int)cyg_libc_time_current_std_offset;
631
    } // if
632
    else if (__timeptr->tm_isdst > 0) { // DST
633
        // take _off_ the dst offset to get us back to UTC from localtime
634
        __timeptr->tm_sec -= (int)cyg_libc_time_current_dst_offset;
635
    } // if
636
#endif
637
 
638
    cyg_libc_time_normalize_structtm(__timeptr);
639
 
640
    // check if a time_t can hold the year. FIXME: we assume it is
641
    // 32 bits which gives the year range 1902 - 2038
642
    if ( (__timeptr->tm_year <= 2) || (__timeptr->tm_year >= 138) ) {
643
        CYG_REPORT_RETVAL(-1);
644
        return (time_t)-1;
645
    }
646
 
647
    // fill in the rest of the struct tm i.e. tm_wday and tm_yday
648
 
649
    _leap = cyg_libc_time_year_is_leap(1900 + __timeptr->tm_year);
650
 
651
    for (_i=0, _daycount=0; _i<12; ++_i) {
652
        if (_i == __timeptr->tm_mon) {
653
            _daycount += __timeptr->tm_mday - 1;
654
            break;
655
        } // if
656
        else {
657
            _daycount += cyg_libc_time_month_lengths[_leap][_i];
658
        } // else
659
    } // for
660
 
661
    CYG_ASSERT(_i<12, "Reached end of year. __timeptr->tm_mon must be bad");
662
 
663
    __timeptr->tm_yday = _daycount;
664
 
665
    // now tm_wday
666
 
667
    if (__timeptr->tm_year > 70) {
668
        for (_i=70; _i < __timeptr->tm_year; ++_i)
669
            _daycount += (cyg_libc_time_year_is_leap(1900 + _i) ? 366 : 365);
670
    } // if
671
    else if (__timeptr->tm_year < 70) {
672
        for (_i=70; _i > __timeptr->tm_year; --_i)
673
            _daycount -= (cyg_libc_time_year_is_leap(1900 + _i-1) ? 366 : 365);
674
    } // else if
675
 
676
    __timeptr->tm_wday = (_daycount + CYGNUM_LIBC_TIME_EPOCH_WDAY) % 7;
677
 
678
    // if _daycount was negative, on some targets the modulo operator will
679
    // return negative, so we adjust for that
680
 
681
    if (__timeptr->tm_wday < 0)
682
        __timeptr->tm_wday += 7;
683
 
684
    // now finally work out return value
685
 
686
    _ret = __timeptr->tm_sec + 60*__timeptr->tm_min + 60*60*__timeptr->tm_hour;
687
    _ret += _daycount*24*60*60;
688
 
689
    CYG_REPORT_RETVAL(_ret);
690
 
691
    return _ret;
692
} // mktime()
693
 
694
#endif // ifdef CYGIMP_LIBC_TIME_MKTIME_INLINE
695
 
696
 
697
// Time conversion functions - ISO C 7.12.3
698
 
699
////////////////////////////////
700
// asctime() - ISO C 7.12.3.1 //
701
////////////////////////////////
702
//
703
// This returns a textual representation of a struct tm
704
//
705
 
706
#ifdef CYGIMP_LIBC_TIME_ASCTIME_INLINE
707
 
708
extern char cyg_libc_time_asctime_buf[];
709
 
710
CYGPRI_LIBC_TIME_ASCTIME_INLINE char *
711
asctime( const struct tm *__timeptr )
712
{
713
    CYG_REPORT_FUNCNAMETYPE("__asctime", "returning \"%s\"");
714
    CYG_REPORT_FUNCARG1("__timeptr = %08x", __timeptr);
715
 
716
    // paranoia
717
    CYG_CHECK_DATA_PTR(__timeptr, "__timeptr is not a valid pointer!");
718
 
719
    (void)__asctime_r( __timeptr, cyg_libc_time_asctime_buf );
720
 
721
    CYG_REPORT_RETVAL(cyg_libc_time_asctime_buf);
722
 
723
    return cyg_libc_time_asctime_buf;
724
} // asctime()
725
 
726
#endif // ifdef CYGIMP_LIBC_TIME_ASCTIME_INLINE
727
 
728
 
729
///////////////////////////////
730
// gmtime() - ISO C 7.12.3.3 //
731
///////////////////////////////
732
//
733
// This converts a time_t into a struct tm expressed in UTC
734
//
735
 
736
#ifdef CYGIMP_LIBC_TIME_GMTIME_INLINE
737
 
738
extern struct tm cyg_libc_time_gmtime_buf;
739
 
740
CYGPRI_LIBC_TIME_GMTIME_INLINE struct tm *
741
gmtime( const time_t *__timer )
742
{
743
    CYG_REPORT_FUNCNAMETYPE("gmtime", "returning %08x");
744
    CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
745
    CYG_REPORT_FUNCARG1("*__timer=%d", *__timer);
746
 
747
    __gmtime_r(__timer, &cyg_libc_time_gmtime_buf);
748
 
749
    CYG_REPORT_RETVAL(&cyg_libc_time_gmtime_buf);
750
 
751
    return &cyg_libc_time_gmtime_buf;
752
} // gmtime()
753
 
754
#endif // ifdef CYGIMP_LIBC_TIME_GMTIME_INLINE
755
 
756
 
757
//////////////////////////////////
758
// localtime() - ISO C 7.12.3.4 //
759
//////////////////////////////////
760
//
761
// This converts a time_t into a struct tm expressed in local time
762
//
763
 
764
#ifdef CYGIMP_LIBC_TIME_LOCALTIME_INLINE
765
 
766
extern struct tm cyg_libc_time_localtime_buf;
767
 
768
CYGPRI_LIBC_TIME_LOCALTIME_INLINE struct tm *
769
localtime( const time_t *__timer )
770
{
771
    CYG_REPORT_FUNCNAMETYPE("localtime", "returning %08x");
772
    CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
773
    CYG_REPORT_FUNCARG1("*__timer=%d", *__timer);
774
 
775
    __localtime_r(__timer, &cyg_libc_time_localtime_buf);
776
 
777
    CYG_REPORT_RETVAL(&cyg_libc_time_localtime_buf);
778
 
779
    return &cyg_libc_time_localtime_buf;
780
} // localtime()
781
 
782
#endif // ifdef CYGIMP_LIBC_TIME_LOCALTIME_INLINE
783
 
784
 
785
//////////////////////////////
786
// ctime() - ISO C 7.12.3.2 //
787
//////////////////////////////
788
//
789
// This returns asctime(localtime(__timeptr))
790
//
791
 
792
#ifdef CYGIMP_LIBC_TIME_CTIME_INLINE
793
 
794
CYGPRI_LIBC_TIME_CTIME_INLINE char *
795
ctime( const time_t *__timer )
796
{
797
    char *_str;
798
 
799
    CYG_REPORT_FUNCNAMETYPE("ctime", "returning \"%s\"");
800
    CYG_CHECK_DATA_PTR( __timer, "__timer is not a valid pointer!");
801
    CYG_REPORT_FUNCARG1("*__timer = %d", *__timer);
802
 
803
    _str = asctime(localtime(__timer));
804
 
805
    CYG_REPORT_RETVAL(_str);
806
 
807
    return _str;
808
} // ctime()
809
 
810
#endif // ifdef CYGIMP_LIBC_TIME_CTIME_INLINE
811
 
812
 
813
#ifdef __cplusplus
814
} // extern "C"
815
#endif
816
 
817
#endif // CYGONCE_LIBC_TIME_INL multiple inclusion protection
818
 
819
// EOF time.inl

powered by: WebSVN 2.1.0

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