Mercurial > ecos
annotate packages/language/c/libc/current/src/time/strftime.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 // strftime.cxx | |
| 4 // | |
| 5 // ISO C date and time implementation for strftime() | |
| 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 implementation of the ISO C function strftime() | |
| 38 // from ISO C section 7.12.3.5 | |
| 39 // Description: This file provides the implementation of strftime() | |
| 40 // Usage: | |
| 41 // | |
| 42 //####DESCRIPTIONEND#### | |
| 43 // | |
| 44 //======================================================================== | |
| 45 | |
| 46 // CONFIGURATION | |
| 47 | |
| 48 #include <pkgconf/libc.h> // C library configuration | |
| 49 | |
| 50 // INCLUDES | |
| 51 | |
| 52 #include <cyg/infra/cyg_type.h> // Common type definitions and support | |
| 53 #include <cyg/infra/cyg_ass.h> // Assertion infrastructure | |
| 54 #include <cyg/infra/cyg_trac.h> // Tracing infrastructure | |
| 55 #include <time.h> // Main date and time definitions | |
| 56 #include <sys/timeutil.h> // For cyg_libc_time_{day,month}_name{,_len} | |
| 57 | |
| 58 // FUNCTION PROTOTYPES | |
| 59 | |
| 60 // forward declaration for do_format() | |
| 61 externC size_t | |
| 62 __strftime( char *s, size_t maxsize, const char *format, | |
| 63 const struct tm *timeptr); | |
| 64 | |
| 65 | |
| 66 // FIXME: This all needs to be internationalized and localized, both in | |
| 67 // terms of reading and writing multibyte characters, and that it should | |
| 68 // refer to the settings of LC_TIME | |
| 69 | |
| 70 // FUNCTIONS | |
| 71 | |
| 72 // This helper function actually processes the format. Which format to insert | |
| 73 // is indicated by fmtchar, sizeleft is the maximum space allowed to be used | |
| 74 // in buf. The number of bytes written is returned, or -1 if there was no | |
| 75 // space | |
| 76 | |
| 77 static cyg_int8 | |
| 78 do_format(cyg_uint8 fmtchar, cyg_ucount32 sizeleft, char *buf, | |
| 79 const struct tm *timeptr) | |
| 80 { | |
| 81 cyg_count8 i; | |
| 82 | |
| 83 switch (fmtchar) { | |
| 84 case 'a': | |
| 85 if (sizeleft<3) | |
| 86 return -1; | |
| 87 buf[0] = cyg_libc_time_day_name[timeptr->tm_wday][0]; | |
| 88 buf[1] = cyg_libc_time_day_name[timeptr->tm_wday][1]; | |
| 89 buf[2] = cyg_libc_time_day_name[timeptr->tm_wday][2]; | |
| 90 return 3; | |
| 91 case 'A': | |
| 92 if (sizeleft < cyg_libc_time_day_name_len[timeptr->tm_wday]) | |
| 93 return -1; | |
| 94 for (i=0; i<cyg_libc_time_day_name_len[timeptr->tm_wday]; ++i) | |
| 95 buf[i] = cyg_libc_time_day_name[timeptr->tm_wday][i]; | |
| 96 return i; | |
| 97 case 'b': | |
| 98 if (sizeleft<3) | |
| 99 return -1; | |
| 100 buf[0] = cyg_libc_time_month_name[timeptr->tm_mon][0]; | |
| 101 buf[1] = cyg_libc_time_month_name[timeptr->tm_mon][1]; | |
| 102 buf[2] = cyg_libc_time_month_name[timeptr->tm_mon][2]; | |
| 103 return 3; | |
| 104 case 'B': | |
| 105 if (sizeleft < cyg_libc_time_month_name_len[timeptr->tm_mon]) | |
| 106 return -1; | |
| 107 for (i=0; i<cyg_libc_time_month_name_len[timeptr->tm_mon]; ++i) | |
| 108 buf[i] = cyg_libc_time_month_name[timeptr->tm_mon][i]; | |
| 109 return i; | |
| 110 case 'c': | |
| 111 if (sizeleft < 26) | |
| 112 return -1; | |
| 113 | |
| 114 // Recurse! Note that we know that we will have left room for the | |
| 115 // trailing NULL in the strftime body | |
| 116 | |
| 117 i = __strftime( buf, sizeleft+1, "%a %b %d %I:%M:%S%p %Y", timeptr); | |
| 118 | |
| 119 return ((0==i) ? -1 : i); | |
| 120 case 'd': | |
| 121 if (sizeleft < 2) | |
| 122 return -1; | |
| 123 buf[0] = (timeptr->tm_mday / 10) + '0'; | |
| 124 buf[1] = (timeptr->tm_mday % 10) + '0'; | |
| 125 return 2; | |
| 126 case 'H': | |
| 127 if (sizeleft < 2) | |
| 128 return -1; | |
| 129 buf[0] = (timeptr->tm_hour / 10) + '0'; | |
| 130 buf[1] = (timeptr->tm_hour % 10) + '0'; | |
| 131 return 2; | |
| 132 case 'I': | |
| 133 if (sizeleft < 2) | |
| 134 return -1; | |
| 135 buf[0] = ((timeptr->tm_hour%12 + 1) / 10) + '0'; | |
| 136 buf[1] = ((timeptr->tm_hour%12 + 1) % 10) + '0'; | |
| 137 return 2; | |
| 138 case 'j': | |
| 139 if (sizeleft < 3) | |
| 140 return -1; | |
| 141 buf[0] = (timeptr->tm_yday / 100) + '0'; | |
| 142 buf[1] = ((timeptr->tm_yday % 100) / 10) + '0'; | |
| 143 buf[2] = (timeptr->tm_yday % 10) + '0'; | |
| 144 return 3; | |
| 145 case 'm': | |
| 146 if (sizeleft < 2) | |
| 147 return -1; | |
| 148 buf[0] = (timeptr->tm_mon / 10) + '0'; | |
| 149 buf[1] = (timeptr->tm_mon % 10) + '0'; | |
| 150 return 2; | |
| 151 case 'M': | |
| 152 if (sizeleft < 2) | |
| 153 return -1; | |
| 154 buf[0] = (timeptr->tm_min / 10) + '0'; | |
| 155 buf[1] = (timeptr->tm_min % 10) + '0'; | |
| 156 return 2; | |
| 157 case 'p': | |
| 158 if (sizeleft < 2) | |
| 159 return -1; | |
| 160 buf[0] = (timeptr->tm_hour > 11) ? 'p' : 'a'; | |
| 161 buf[1] = 'm'; | |
| 162 return 2; | |
| 163 case 'S': | |
| 164 if (sizeleft < 2) | |
| 165 return -1; | |
| 166 buf[0] = (timeptr->tm_sec / 10) + '0'; | |
| 167 buf[1] = (timeptr->tm_sec % 10) + '0'; | |
| 168 return 2; | |
| 169 case 'U': | |
| 170 if (sizeleft < 2) | |
| 171 return -1; | |
| 172 i = (timeptr->tm_yday - timeptr->tm_wday + 7) / 7; | |
| 173 buf[0] = (i / 10) + '0'; | |
| 174 buf[1] = (i % 10) + '0'; | |
| 175 return 2; | |
| 176 case 'w': | |
| 177 // Don't need to check size - we'll always be called with sizeleft > 0 | |
| 178 buf[0] = timeptr->tm_wday + '0'; | |
| 179 return 1; | |
| 180 case 'W': | |
| 181 if (sizeleft < 2) | |
| 182 return -1; | |
| 183 i = (timeptr->tm_yday + ((8-timeptr->tm_wday) % 7)) / 7; | |
| 184 buf[0] = (i / 10) + '0'; | |
| 185 buf[1] = (i % 10) + '0'; | |
| 186 return 2; | |
| 187 case 'x': | |
| 188 if (sizeleft < 15) | |
| 189 return -1; | |
| 190 | |
| 191 // Recurse! Note that we know that we will have left room for the | |
| 192 // trailing NULL in the strftime body | |
| 193 | |
| 194 i = __strftime( buf, sizeleft+1, "%a %b %d %Y", timeptr); | |
| 195 | |
| 196 return (0==i) ? -1 : i; | |
| 197 case 'X': | |
| 198 if (sizeleft < 10) | |
| 199 return -1; | |
| 200 | |
| 201 // Recurse! Note that we know that we will have left room for the | |
| 202 // trailing NULL in the strftime body | |
| 203 | |
| 204 i = __strftime( buf, sizeleft+1, "%I:%M:%S%p", timeptr); | |
| 205 | |
| 206 return (0==i) ? -1 : i; | |
| 207 case 'y': | |
| 208 if (sizeleft < 2) | |
| 209 return -1; | |
| 210 buf[0] = ((timeptr->tm_year % 100) / 10) + '0'; | |
| 211 buf[1] = ((timeptr->tm_year % 100) % 10) + '0'; | |
| 212 return 2; | |
| 213 case 'Y': | |
| 214 if (sizeleft < 4) | |
| 215 return -1; | |
| 216 buf[0] = ((1900+timeptr->tm_year) / 1000) + '0'; | |
| 217 buf[1] = (((1900+timeptr->tm_year) % 1000) / 100) + '0'; | |
| 218 buf[2] = ((timeptr->tm_year % 100) / 10) + '0'; | |
| 219 buf[3] = (timeptr->tm_year % 10) + '0'; | |
| 220 return 4; | |
| 221 case 'Z': | |
| 222 // FIXME: Should store zone in timeptr->tm_zone, or failing that | |
| 223 // read TZ env variable using tzset() | |
| 224 return 0; | |
| 225 case '%': | |
| 226 // Don't need to check size - we'll always be called with sizeleft > 0 | |
| 227 buf[0] = '%'; | |
| 228 return 1; | |
| 229 default: | |
| 230 CYG_FAIL("invalid format character!"); | |
| 231 return -1; | |
| 232 } // switch | |
| 233 | |
| 234 } // do_format() | |
| 235 | |
| 236 externC size_t | |
| 237 __strftime( char *s, size_t maxsize, const char *format, | |
| 238 const struct tm *timeptr) | |
| 239 { | |
| 240 CYG_REPORT_FUNCNAMETYPE("__strftime", "returning string length %d"); | |
| 241 CYG_CHECK_DATA_PTR(s, "string s is not a valid address!"); | |
| 242 CYG_CHECK_DATA_PTR(format, "format string is not at a valid address!"); | |
| 243 CYG_CHECK_DATA_PTR(timeptr, | |
| 244 "struct tm argument is not at a valid address!"); | |
| 245 CYG_REPORT_FUNCARG4("s is at address %08x, maxsize=%d, format=\"%s\", " | |
| 246 "timeptr is at address %08x", | |
| 247 s, maxsize, format, timeptr); | |
| 248 | |
| 249 CYG_PRECONDITION((timeptr->tm_sec >= 0) && (timeptr->tm_sec < 62), | |
| 250 "timeptr->tm_sec out of range!"); | |
| 251 CYG_PRECONDITION((timeptr->tm_min >= 0) && (timeptr->tm_min < 60), | |
| 252 "timeptr->tm_min out of range!"); | |
| 253 CYG_PRECONDITION((timeptr->tm_hour >= 0) && (timeptr->tm_hour < 24), | |
| 254 "timeptr->tm_hour out of range!"); | |
| 255 // Currently I don't check _actual_ numbers of days in each month here | |
| 256 // FIXME: No reason why not though | |
| 257 CYG_PRECONDITION((timeptr->tm_mday >= 1) && (timeptr->tm_mday < 32), | |
| 258 "timeptr->tm_mday out of range!"); | |
| 259 CYG_PRECONDITION((timeptr->tm_mon >= 0) && (timeptr->tm_mon < 12), | |
| 260 "timeptr->tm_mon out of range!"); | |
| 261 CYG_PRECONDITION((timeptr->tm_wday >= 0) && (timeptr->tm_wday < 7), | |
| 262 "timeptr->tm_wday out of range!"); | |
| 263 CYG_PRECONDITION((timeptr->tm_yday >= 0) && (timeptr->tm_yday < 366), | |
| 264 "timeptr->tm_yday out of range!"); | |
| 265 CYG_PRECONDITION((timeptr->tm_year > -1900) && | |
| 266 (timeptr->tm_year < 8100), | |
| 267 "timeptr->tm_year out of range!"); | |
| 268 | |
| 269 if (!maxsize) { | |
| 270 CYG_REPORT_RETVAL(0); | |
| 271 return 0; | |
| 272 } // if | |
| 273 | |
| 274 // lets leave the room for the trailing null up front | |
| 275 --maxsize; | |
| 276 | |
| 277 cyg_ucount32 i, spos; | |
| 278 cyg_int8 dof_ret; | |
| 279 | |
| 280 for (i=0, spos=0; (spos<maxsize) && (format[i] != '\0'); ++i) { | |
| 281 if (format[i] == '%') { | |
| 282 if (format[++i] == '\0') | |
| 283 break; | |
| 284 dof_ret = do_format(format[i], maxsize - spos, &s[spos], timeptr); | |
| 285 // was there room to write _anything_? | |
| 286 if (dof_ret < 0) { | |
| 287 spos=0; // ISO says we must return 0 and the contents of s | |
| 288 // are indeterminate | |
| 289 break; | |
| 290 } | |
| 291 spos += dof_ret; | |
| 292 } | |
| 293 else { | |
| 294 s[spos++] = format[i]; | |
| 295 } // else | |
| 296 } // for | |
| 297 | |
| 298 s[spos] = '\0'; | |
| 299 | |
| 300 CYG_REPORT_RETVAL(spos); | |
| 301 return spos; | |
| 302 | |
| 303 } // __strftime() | |
| 304 | |
| 305 | |
| 306 // SYMBOL DEFINITIONS | |
| 307 | |
| 308 externC size_t | |
| 309 strftime( char *s, size_t maxsize, const char *format, | |
| 310 const struct tm *timeptr) CYGBLD_ATTRIB_WEAK_ALIAS(__strftime); | |
| 311 | |
| 312 // EOF strftime.cxx |
