The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 1 | /* $OpenBSD: strptime.c,v 1.11 2005/08/08 08:05:38 espie Exp $ */ |
| 2 | /* $NetBSD: strptime.c,v 1.12 1998/01/20 21:39:40 mycroft Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * This code was contributed to The NetBSD Foundation by Klaus Klein. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. All advertising materials mentioning features or use of this software |
| 19 | * must display the following acknowledgement: |
| 20 | * This product includes software developed by the NetBSD |
| 21 | * Foundation, Inc. and its contributors. |
| 22 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
| 23 | * contributors may be used to endorse or promote products derived |
| 24 | * from this software without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 27 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 28 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 29 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 30 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | * POSSIBILITY OF SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | //#include <sys/localedef.h> |
| 40 | #include <ctype.h> |
| 41 | #include <locale.h> |
| 42 | #include <string.h> |
| 43 | #include <time.h> |
| 44 | #include "tzfile.h" |
| 45 | |
| 46 | static const struct { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 47 | const char *abday[7]; |
| 48 | const char *day[7]; |
| 49 | const char *abmon[12]; |
| 50 | const char *mon[12]; |
| 51 | const char *am_pm[2]; |
| 52 | const char *d_t_fmt; |
| 53 | const char *d_fmt; |
| 54 | const char *t_fmt; |
| 55 | const char *t_fmt_ampm; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | } _DefaultTimeLocale = { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 57 | { |
| 58 | "Sun","Mon","Tue","Wed","Thu","Fri","Sat", |
| 59 | }, |
| 60 | { |
| 61 | "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", |
| 62 | "Friday", "Saturday" |
| 63 | }, |
| 64 | { |
| 65 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 66 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 67 | }, |
| 68 | { |
| 69 | "January", "February", "March", "April", "May", "June", "July", |
| 70 | "August", "September", "October", "November", "December" |
| 71 | }, |
| 72 | { |
| 73 | "AM", "PM" |
| 74 | }, |
| 75 | "%a %b %d %H:%M:%S %Y", |
| 76 | "%m/%d/%y", |
| 77 | "%H:%M:%S", |
| 78 | "%I:%M:%S %p" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 81 | #define _ctloc(x) (_DefaultTimeLocale.x) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * We do not implement alternate representations. However, we always |
| 85 | * check whether a given modifier is allowed for a certain conversion. |
| 86 | */ |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 87 | #define _ALT_E 0x01 |
| 88 | #define _ALT_O 0x02 |
| 89 | #define _LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 90 | |
| 91 | |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 92 | struct century_relyear { |
| 93 | int century; |
| 94 | int relyear; |
| 95 | }; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 96 | static int _conv_num(const unsigned char **, int *, int, int); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 97 | static unsigned char *_strptime(const unsigned char *, const char *, struct tm *, |
| 98 | struct century_relyear *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | |
| 100 | |
| 101 | char * |
| 102 | strptime(const char *buf, const char *fmt, struct tm *tm) |
| 103 | { |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 104 | struct century_relyear cr; |
| 105 | cr.century = TM_YEAR_BASE; |
| 106 | cr.relyear = -1; |
| 107 | return (char*)(_strptime((const unsigned char*)buf, fmt, tm, &cr)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 108 | } |
| 109 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 110 | static unsigned char * |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 111 | _strptime(const unsigned char *buf, const char *fmt, struct tm *tm, struct century_relyear *cr) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 113 | unsigned char c; |
| 114 | const unsigned char *bp; |
| 115 | size_t len = 0; |
| 116 | int alt_format, i; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 117 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 118 | bp = (unsigned char *)buf; |
| 119 | while ((c = *fmt) != '\0') { |
| 120 | /* Clear `alternate' modifier prior to new conversion. */ |
| 121 | alt_format = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 123 | /* Eat up white-space. */ |
| 124 | if (isspace(c)) { |
| 125 | while (isspace(*bp)) |
| 126 | bp++; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 127 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 128 | fmt++; |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | if ((c = *fmt++) != '%') |
| 133 | goto literal; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 134 | |
| 135 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 136 | again: switch (c = *fmt++) { |
| 137 | case '%': /* "%%" is converted to "%". */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | literal: |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 139 | if (c != *bp++) |
| 140 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 141 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 142 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 143 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 144 | /* |
| 145 | * "Alternative" modifiers. Just set the appropriate flag |
| 146 | * and start over again. |
| 147 | */ |
| 148 | case 'E': /* "%E?" alternative conversion modifier. */ |
| 149 | _LEGAL_ALT(0); |
| 150 | alt_format |= _ALT_E; |
| 151 | goto again; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 152 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 153 | case 'O': /* "%O?" alternative conversion modifier. */ |
| 154 | _LEGAL_ALT(0); |
| 155 | alt_format |= _ALT_O; |
| 156 | goto again; |
| 157 | |
| 158 | /* |
| 159 | * "Complex" conversion rules, implemented through recursion. |
| 160 | */ |
| 161 | case 'c': /* Date and time, using the locale's format. */ |
| 162 | _LEGAL_ALT(_ALT_E); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 163 | if (!(bp = _strptime(bp, _ctloc(d_t_fmt), tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 164 | return (NULL); |
| 165 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 166 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 167 | case 'D': /* The date as "%m/%d/%y". */ |
| 168 | _LEGAL_ALT(0); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 169 | if (!(bp = _strptime(bp, "%m/%d/%y", tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 170 | return (NULL); |
| 171 | break; |
| 172 | |
| 173 | case 'R': /* The time as "%H:%M". */ |
| 174 | _LEGAL_ALT(0); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 175 | if (!(bp = _strptime(bp, "%H:%M", tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 176 | return (NULL); |
| 177 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 178 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 179 | case 'r': /* The time as "%I:%M:%S %p". */ |
| 180 | _LEGAL_ALT(0); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 181 | if (!(bp = _strptime(bp, "%I:%M:%S %p", tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 182 | return (NULL); |
| 183 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 184 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 185 | case 'T': /* The time as "%H:%M:%S". */ |
| 186 | _LEGAL_ALT(0); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 187 | if (!(bp = _strptime(bp, "%H:%M:%S", tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 188 | return (NULL); |
| 189 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 191 | case 'X': /* The time, using the locale's format. */ |
| 192 | _LEGAL_ALT(_ALT_E); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 193 | if (!(bp = _strptime(bp, _ctloc(t_fmt), tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 194 | return (NULL); |
| 195 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 196 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 197 | case 'x': /* The date, using the locale's format. */ |
| 198 | _LEGAL_ALT(_ALT_E); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 199 | if (!(bp = _strptime(bp, _ctloc(d_fmt), tm, cr))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 200 | return (NULL); |
| 201 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 202 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 203 | /* |
| 204 | * "Elementary" conversion rules. |
| 205 | */ |
| 206 | case 'A': /* The day of week, using the locale's form. */ |
| 207 | case 'a': |
| 208 | _LEGAL_ALT(0); |
| 209 | for (i = 0; i < 7; i++) { |
| 210 | /* Full name. */ |
| 211 | len = strlen(_ctloc(day[i])); |
| 212 | if (strncasecmp(_ctloc(day[i]), (const char*)bp, len) == 0) |
| 213 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 214 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 215 | /* Abbreviated name. */ |
| 216 | len = strlen(_ctloc(abday[i])); |
| 217 | if (strncasecmp(_ctloc(abday[i]), (const char*)bp, len) == 0) |
| 218 | break; |
| 219 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 220 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 221 | /* Nothing matched. */ |
| 222 | if (i == 7) |
| 223 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 224 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 225 | tm->tm_wday = i; |
| 226 | bp += len; |
| 227 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 228 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 229 | case 'B': /* The month, using the locale's form. */ |
| 230 | case 'b': |
| 231 | case 'h': |
| 232 | _LEGAL_ALT(0); |
| 233 | for (i = 0; i < 12; i++) { |
| 234 | /* Full name. */ |
| 235 | len = strlen(_ctloc(mon[i])); |
| 236 | if (strncasecmp(_ctloc(mon[i]), (const char*)bp, len) == 0) |
| 237 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 238 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 239 | /* Abbreviated name. */ |
| 240 | len = strlen(_ctloc(abmon[i])); |
| 241 | if (strncasecmp(_ctloc(abmon[i]), (const char*)bp, len) == 0) |
| 242 | break; |
| 243 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 244 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 245 | /* Nothing matched. */ |
| 246 | if (i == 12) |
| 247 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 248 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 249 | tm->tm_mon = i; |
| 250 | bp += len; |
| 251 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 252 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 253 | case 'C': /* The century number. */ |
| 254 | _LEGAL_ALT(_ALT_E); |
| 255 | if (!(_conv_num(&bp, &i, 0, 99))) |
| 256 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 257 | |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 258 | cr->century = i * 100; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 259 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 260 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 261 | case 'd': /* The day of month. */ |
| 262 | case 'e': |
| 263 | _LEGAL_ALT(_ALT_O); |
| 264 | if (!(_conv_num(&bp, &tm->tm_mday, 1, 31))) |
| 265 | return (NULL); |
| 266 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 267 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 268 | case 'k': /* The hour (24-hour clock representation). */ |
| 269 | _LEGAL_ALT(0); |
| 270 | /* FALLTHROUGH */ |
| 271 | case 'H': |
| 272 | _LEGAL_ALT(_ALT_O); |
| 273 | if (!(_conv_num(&bp, &tm->tm_hour, 0, 23))) |
| 274 | return (NULL); |
| 275 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 276 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 277 | case 'l': /* The hour (12-hour clock representation). */ |
| 278 | _LEGAL_ALT(0); |
| 279 | /* FALLTHROUGH */ |
| 280 | case 'I': |
| 281 | _LEGAL_ALT(_ALT_O); |
| 282 | if (!(_conv_num(&bp, &tm->tm_hour, 1, 12))) |
| 283 | return (NULL); |
| 284 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 285 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 286 | case 'j': /* The day of year. */ |
| 287 | _LEGAL_ALT(0); |
| 288 | if (!(_conv_num(&bp, &tm->tm_yday, 1, 366))) |
| 289 | return (NULL); |
| 290 | tm->tm_yday--; |
| 291 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 293 | case 'M': /* The minute. */ |
| 294 | _LEGAL_ALT(_ALT_O); |
| 295 | if (!(_conv_num(&bp, &tm->tm_min, 0, 59))) |
| 296 | return (NULL); |
| 297 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 298 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 299 | case 'm': /* The month. */ |
| 300 | _LEGAL_ALT(_ALT_O); |
| 301 | if (!(_conv_num(&bp, &tm->tm_mon, 1, 12))) |
| 302 | return (NULL); |
| 303 | tm->tm_mon--; |
| 304 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 305 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 306 | case 'p': /* The locale's equivalent of AM/PM. */ |
| 307 | _LEGAL_ALT(0); |
| 308 | /* AM? */ |
| 309 | len = strlen(_ctloc(am_pm[0])); |
| 310 | if (strncasecmp(_ctloc(am_pm[0]), (const char*)bp, len) == 0) { |
| 311 | if (tm->tm_hour > 12) /* i.e., 13:00 AM ?! */ |
| 312 | return (NULL); |
| 313 | else if (tm->tm_hour == 12) |
| 314 | tm->tm_hour = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 315 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 316 | bp += len; |
| 317 | break; |
| 318 | } |
| 319 | /* PM? */ |
| 320 | len = strlen(_ctloc(am_pm[1])); |
| 321 | if (strncasecmp(_ctloc(am_pm[1]), (const char*)bp, len) == 0) { |
| 322 | if (tm->tm_hour > 12) /* i.e., 13:00 PM ?! */ |
| 323 | return (NULL); |
| 324 | else if (tm->tm_hour < 12) |
| 325 | tm->tm_hour += 12; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 327 | bp += len; |
| 328 | break; |
| 329 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 330 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 331 | /* Nothing matched. */ |
| 332 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 333 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 334 | case 'S': /* The seconds. */ |
| 335 | _LEGAL_ALT(_ALT_O); |
| 336 | if (!(_conv_num(&bp, &tm->tm_sec, 0, 61))) |
| 337 | return (NULL); |
| 338 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 339 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 340 | case 'U': /* The week of year, beginning on sunday. */ |
| 341 | case 'W': /* The week of year, beginning on monday. */ |
| 342 | _LEGAL_ALT(_ALT_O); |
| 343 | /* |
| 344 | * XXX This is bogus, as we can not assume any valid |
| 345 | * information present in the tm structure at this |
| 346 | * point to calculate a real value, so just check the |
| 347 | * range for now. |
| 348 | */ |
| 349 | if (!(_conv_num(&bp, &i, 0, 53))) |
| 350 | return (NULL); |
| 351 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 352 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 353 | case 'w': /* The day of week, beginning on sunday. */ |
| 354 | _LEGAL_ALT(_ALT_O); |
| 355 | if (!(_conv_num(&bp, &tm->tm_wday, 0, 6))) |
| 356 | return (NULL); |
| 357 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 358 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 359 | case 'Y': /* The year. */ |
| 360 | _LEGAL_ALT(_ALT_E); |
| 361 | if (!(_conv_num(&bp, &i, 0, 9999))) |
| 362 | return (NULL); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 363 | |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 364 | cr->relyear = -1; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 365 | tm->tm_year = i - TM_YEAR_BASE; |
| 366 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 367 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 368 | case 'y': /* The year within the century (2 digits). */ |
| 369 | _LEGAL_ALT(_ALT_E | _ALT_O); |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 370 | if (!(_conv_num(&bp, &cr->relyear, 0, 99))) |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 371 | return (NULL); |
| 372 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 373 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 374 | /* |
| 375 | * Miscellaneous conversions. |
| 376 | */ |
| 377 | case 'n': /* Any kind of white-space. */ |
| 378 | case 't': |
| 379 | _LEGAL_ALT(0); |
| 380 | while (isspace(*bp)) |
| 381 | bp++; |
| 382 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 383 | |
| 384 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 385 | default: /* Unknown/unsupported conversion. */ |
| 386 | return (NULL); |
| 387 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 388 | |
| 389 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 390 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 391 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 392 | /* |
| 393 | * We need to evaluate the two digit year spec (%y) |
| 394 | * last as we can get a century spec (%C) at any time. |
| 395 | */ |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 396 | if (cr->relyear != -1) { |
| 397 | if (cr->century == TM_YEAR_BASE) { |
| 398 | if (cr->relyear <= 68) |
| 399 | tm->tm_year = cr->relyear + 2000 - TM_YEAR_BASE; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 400 | else |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 401 | tm->tm_year = cr->relyear + 1900 - TM_YEAR_BASE; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 402 | } else { |
Glenn Kasten | b138e4f | 2011-01-09 10:33:23 -0800 | [diff] [blame] | 403 | tm->tm_year = cr->relyear + cr->century - TM_YEAR_BASE; |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 406 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 407 | return (unsigned char*)bp; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | |
| 411 | static int |
| 412 | _conv_num(const unsigned char **buf, int *dest, int llim, int ulim) |
| 413 | { |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 414 | int result = 0; |
| 415 | int rulim = ulim; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 416 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 417 | if (**buf < '0' || **buf > '9') |
| 418 | return (0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 419 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 420 | /* we use rulim to break out of the loop when we run out of digits */ |
| 421 | do { |
| 422 | result *= 10; |
| 423 | result += *(*buf)++ - '0'; |
| 424 | rulim /= 10; |
| 425 | } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9'); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 426 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 427 | if (result < llim || result > ulim) |
| 428 | return (0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 429 | |
The Android Open Source Project | edbe7fc | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 430 | *dest = result; |
| 431 | return (1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 432 | } |