Mercurial > ecos
annotate packages/language/c/libc/current/src/time/timeutil.cxx @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 2 | 1 //======================================================================== |
| 2 // | |
| 3 // timeutil.cxx | |
| 4 // | |
| 5 // ISO C date and time implementation support functions | |
| 6 // | |
| 7 //======================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 2 | 30 //####COPYRIGHTEND#### |
| 31 //======================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jlarmour | |
| 35 // Contributors: jlarmour | |
| 36 // Date: 1999-02-26 | |
| 37 // Purpose: Provide support functions used by the ISO C date and time | |
| 38 // implementation | |
| 39 // Description: This file provides the functions: | |
| 40 // cyg_libc_time_normalize_structtm() | |
| 41 // cyg_libc_time_getzoneoffsets() | |
| 42 // cyg_libc_time_setzoneoffsets() | |
| 43 // cyg_libc_time_setdst() | |
| 44 // and the globals: | |
| 45 // cyg_libc_time_day_name | |
| 46 // cyg_libc_time_day_name_len | |
| 47 // cyg_libc_time_month_name | |
| 48 // cyg_libc_time_month_name_len | |
| 49 // cyg_libc_time_month_lengths | |
| 50 // cyg_libc_time_current_dst_stat | |
| 51 // cyg_libc_time_current_std_offset | |
| 52 // cyg_libc_time_current_dst_offset | |
| 53 // Usage: | |
| 54 // | |
| 55 //####DESCRIPTIONEND#### | |
| 56 // | |
| 57 //======================================================================== | |
| 58 | |
| 59 // CONFIGURATION | |
| 60 | |
| 61 #include <pkgconf/libc.h> // C library configuration | |
| 62 | |
| 63 // INCLUDES | |
| 64 | |
| 65 // define these functions as outline, not inline, functions in this file | |
| 66 #define CYGPRI_LIBC_TIME_GETZONEOFFSETS_INLINE extern "C" | |
| 67 #define CYGPRI_LIBC_TIME_SETZONEOFFSETS_INLINE extern "C" | |
| 68 #define CYGPRI_LIBC_TIME_SETDST_INLINE extern "C" | |
| 69 | |
| 70 #include <cyg/infra/cyg_type.h> // Common type definitions and support | |
| 71 #include <sys/timeutil.h> // Header for this file | |
| 72 #include <time.h> // Main date and time definitions | |
| 73 #include <stdlib.h> // for div() | |
| 74 | |
| 75 | |
| 76 // GLOBALS | |
| 77 | |
| 78 // FIXME: PR19440 - const char & -fwritable-strings don't mix | |
| 79 /*const*/ char cyg_libc_time_day_name[7][10] = { | |
| 80 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", | |
| 81 "Saturday" | |
| 82 }; | |
| 83 const cyg_uint8 cyg_libc_time_day_name_len[7] = { 6, 6, 7, 9, 8, 6, 8 }; | |
| 84 | |
| 85 // FIXME: PR19440 - const char & -fwritable-strings don't mix | |
| 86 /*const*/ char cyg_libc_time_month_name[12][10] = { | |
| 87 "January", "February", "March", "April", "May", "June", | |
| 88 "July", "August", "September", "October", "November", "December" | |
| 89 }; | |
| 90 const cyg_uint8 cyg_libc_time_month_name_len[12] = { 7, 8, 5, 5, 3, 4, | |
| 91 4, 6, 9, 7, 8, 8 }; | |
| 92 | |
| 93 const cyg_uint8 cyg_libc_time_month_lengths[2][12] = { | |
| 94 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, | |
| 95 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} | |
| 96 }; | |
| 97 | |
| 98 Cyg_libc_time_dst cyg_libc_time_current_dst_stat = | |
| 99 (Cyg_libc_time_dst)CYGNUM_LIBC_TIME_DST_DEFAULT_STATE; | |
| 100 time_t cyg_libc_time_current_std_offset = | |
| 101 (time_t)CYGNUM_LIBC_TIME_STD_DEFAULT_OFFSET; | |
| 102 time_t cyg_libc_time_current_dst_offset = | |
| 103 (time_t)CYGNUM_LIBC_TIME_DST_DEFAULT_OFFSET; | |
| 104 | |
| 105 | |
| 106 // FUNCTIONS | |
| 107 | |
| 108 //////////////////////////////////////// | |
| 109 // cyg_libc_time_normalize_structtm() // | |
| 110 //////////////////////////////////////// | |
| 111 // | |
| 112 // cyg_libc_time_normalize_structtm() will adjust the fields of a struct tm | |
| 113 // so that they are within the normal ranges expected. | |
| 114 // tm_wday, tm_yday and tm_isdst are ignored | |
| 115 | |
| 116 externC void | |
| 117 cyg_libc_time_normalize_structtm( struct tm *timeptr ) | |
| 118 { | |
| 119 div_t t; | |
| 120 | |
| 121 CYG_REPORT_FUNCNAME("cyg_libc_time_normalize_structtm"); | |
| 122 CYG_REPORT_FUNCARG1("timeptr is at address %08x", timeptr); | |
| 123 | |
| 124 // normalize seconds to 0..59 | |
| 125 if ((timeptr->tm_sec < 0) || (timeptr->tm_sec > 59)) { | |
| 126 t = div(timeptr->tm_sec, 60); | |
| 127 while (t.rem < 0) { | |
| 128 t.rem += 60; | |
| 129 --t.quot; | |
| 130 } | |
| 131 timeptr->tm_min += t.quot; | |
| 132 timeptr->tm_sec = t.rem; | |
| 133 } // if | |
| 134 | |
| 135 // normalize minutes to 0..59 | |
| 136 if ((timeptr->tm_min < 0) || (timeptr->tm_min > 59)) { | |
| 137 t = div(timeptr->tm_min, 60); | |
| 138 while (t.rem < 0) { | |
| 139 t.rem += 60; | |
| 140 --t.quot; | |
| 141 } | |
| 142 timeptr->tm_hour += t.quot; | |
| 143 timeptr->tm_min = t.rem; | |
| 144 } // if | |
| 145 | |
| 146 // normalize hours to 0..23 | |
| 147 if ((timeptr->tm_hour < 0) || (timeptr->tm_hour > 23)) { | |
| 148 t = div(timeptr->tm_hour, 24); | |
| 149 while (t.rem < 0) { | |
| 150 t.rem += 24; | |
| 151 --t.quot; | |
| 152 } | |
| 153 timeptr->tm_mday += t.quot; | |
| 154 timeptr->tm_hour = t.rem; | |
| 155 } // if | |
| 156 | |
| 157 // we wait before normalizing tm_mday as per ISO C 7.12.2.3 (although | |
| 158 // actually it only makes sense if you think about it | |
| 159 | |
| 160 // normalize months to 0..11 | |
| 161 if ((timeptr->tm_mon < 0) || (timeptr->tm_mon > 11)) { | |
| 162 t = div(timeptr->tm_mon, 12); | |
| 163 while (t.rem < 0) { | |
| 164 t.rem += 12; | |
| 165 --t.quot; | |
| 166 } | |
| 167 timeptr->tm_year += t.quot; | |
| 168 timeptr->tm_mon = t.rem; | |
| 169 } // if | |
| 170 | |
| 171 // now tm_mday which needs to go to 1..31 | |
| 172 cyg_bool leap = cyg_libc_time_year_is_leap(timeptr->tm_year); | |
| 173 | |
| 174 while (timeptr->tm_mday < 1) { | |
| 175 // move back a month | |
| 176 | |
| 177 if (--timeptr->tm_mon < 0) { | |
| 178 --timeptr->tm_year; | |
| 179 timeptr->tm_mon = 11; | |
| 180 leap = cyg_libc_time_year_is_leap(timeptr->tm_year); | |
| 181 } // if | |
| 182 | |
| 183 // we move backward the number of days in the _new_ current month | |
| 184 timeptr->tm_mday += cyg_libc_time_month_lengths[leap][timeptr->tm_mon]; | |
| 185 | |
| 186 } // while | |
| 187 | |
| 188 while (timeptr->tm_mday > | |
| 189 cyg_libc_time_month_lengths[leap][timeptr->tm_mon]) { | |
| 190 | |
| 191 // move forward a month | |
| 192 | |
| 193 // we move forward the number of days in the _old_ current month | |
| 194 timeptr->tm_mday -= cyg_libc_time_month_lengths[leap][timeptr->tm_mon]; | |
| 195 | |
| 196 if (++timeptr->tm_mon > 11) { | |
| 197 ++timeptr->tm_year; | |
| 198 timeptr->tm_mon = 0; | |
| 199 leap = cyg_libc_time_year_is_leap(timeptr->tm_year); | |
| 200 } // if | |
| 201 | |
| 202 } // while | |
| 203 | |
| 204 CYG_REPORT_RETURN(); | |
| 205 | |
| 206 } // cyg_libc_time_normalize_structtm() | |
| 207 | |
| 208 | |
| 209 // EOF timeutil.cxx |
