strftime.c (28021) | strftime.c (30089) |
---|---|
1/* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18#ifdef LIBC_RCS 19static const char rcsid[] = | 1/* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18#ifdef LIBC_RCS 19static const char rcsid[] = |
20 "$Id: strftime.c,v 1.17 1997/02/22 15:03:19 peter Exp $"; | 20 "$Id: strftime.c,v 1.18 1997/08/09 15:43:53 joerg Exp $"; |
21#endif 22 23#ifndef lint 24#ifndef NOID 25static const char elsieid[] = "@(#)strftime.c 7.38"; 26/* 27** Based on the UCB version with the ID appearing below. 28** This is ANSIish only when "multibyte character == plain character". --- 200 unchanged lines hidden (view full) --- 229 ** From Arnold Robbins' strftime version 3.0: 230 ** "ISO 8601: Weekday as a decimal number 231 ** [1 (Monday) - 7]" 232 ** (ado, 5/24/93) 233 */ 234 pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, 235 "%d", pt, ptlim); 236 continue; | 21#endif 22 23#ifndef lint 24#ifndef NOID 25static const char elsieid[] = "@(#)strftime.c 7.38"; 26/* 27** Based on the UCB version with the ID appearing below. 28** This is ANSIish only when "multibyte character == plain character". --- 200 unchanged lines hidden (view full) --- 229 ** From Arnold Robbins' strftime version 3.0: 230 ** "ISO 8601: Weekday as a decimal number 231 ** [1 (Monday) - 7]" 232 ** (ado, 5/24/93) 233 */ 234 pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday, 235 "%d", pt, ptlim); 236 continue; |
237 case 'V': 238 /* 239 ** From Arnold Robbins' strftime version 3.0: 240 ** "the week number of the year (the first 241 ** Monday as the first day of week 1) as a 242 ** decimal number (01-53). The method for 243 ** determining the week number is as specified 244 ** by ISO 8601 (to wit: if the week containing 245 ** January 1 has four or more days in the new 246 ** year, then it is week 1, otherwise it is 247 ** week 53 of the previous year and the next 248 ** week is week 1)." 249 ** (ado, 5/24/93) 250 */ 251 /* 252 ** XXX--If January 1 falls on a Friday, 253 ** January 1-3 are part of week 53 of the 254 ** previous year. By analogy, if January 255 ** 1 falls on a Thursday, are December 29-31 256 ** of the PREVIOUS year part of week 1??? 257 ** (ado 5/24/93) 258 */ 259 /* 260 ** You are understood not to expect this. 261 */ | 237 case 'V': /* ISO 8601 week number */ 238 case 'G': /* ISO 8601 year (four digits) */ 239 case 'g': /* ISO 8601 year (two digits) */ 240/* 241** From Arnold Robbins' strftime version 3.0: "the week number of the 242** year (the first Monday as the first day of week 1) as a decimal number 243** (01-53)." 244** (ado, 1993-05-24) 245** 246** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn: 247** "Week 01 of a year is per definition the first week which has the 248** Thursday in this year, which is equivalent to the week which contains 249** the fourth day of January. In other words, the first week of a new year 250** is the week which has the majority of its days in the new year. Week 01 251** might also contain days from the previous year and the week before week 252** 01 of a year is the last week (52 or 53) of the previous year even if 253** it contains days from the new year. A week starts with Monday (day 1) 254** and ends with Sunday (day 7). For example, the first week of the year 255** 1997 lasts from 1996-12-30 to 1997-01-05..." 256** (ado, 1996-01-02) 257*/ |
262 { | 258 { |
263 int i; | 259 int year; 260 int yday; 261 int wday; 262 int w; |
264 | 263 |
265 i = (t->tm_yday + 10 - (t->tm_wday ? 266 (t->tm_wday - 1) : 6)) / 7; 267 if (i == 0) { | 264 year = t->tm_year + TM_YEAR_BASE; 265 yday = t->tm_yday; 266 wday = t->tm_wday; 267 for ( ; ; ) { 268 int len; 269 int bot; 270 int top; 271 272 len = isleap(year) ? 273 DAYSPERLYEAR : 274 DAYSPERNYEAR; |
268 /* | 275 /* |
269 ** What day of the week does 270 ** January 1 fall on? | 276 ** What yday (-3 ... 3) does 277 ** the ISO year begin on? |
271 */ | 278 */ |
272 i = t->tm_wday - 273 (t->tm_yday - 1); | 279 bot = ((yday + 11 - wday) % 280 DAYSPERWEEK) - 3; |
274 /* | 281 /* |
275 ** Fri Jan 1: 53 276 ** Sun Jan 1: 52 277 ** Sat Jan 1: 53 if previous 278 ** year a leap 279 ** year, else 52 | 282 ** What yday does the NEXT 283 ** ISO year begin on? |
280 */ | 284 */ |
281 if (i == TM_FRIDAY) 282 i = 53; 283 else if (i == TM_SUNDAY) 284 i = 52; 285 else i = isleap(t->tm_year + 286 TM_YEAR_BASE) ? 287 53 : 52; | 285 top = bot - 286 (len % DAYSPERWEEK); 287 if (top < -3) 288 top += DAYSPERWEEK; 289 top += len; 290 if (yday >= top) { 291 ++year; 292 w = 1; 293 break; 294 } 295 if (yday >= bot) { 296 w = 1 + ((yday - bot) / 297 DAYSPERWEEK); 298 break; 299 } 300 --year; 301 yday += isleap(year) ? 302 DAYSPERLYEAR : 303 DAYSPERNYEAR; 304 } |
288#ifdef XPG4_1994_04_09 | 305#ifdef XPG4_1994_04_09 |
289 /* 290 ** As of 4/9/94, though, 291 ** XPG4 calls for 53 292 ** unconditionally. 293 */ 294 i = 53; | 306 if ((w == 52 307 && t->tm_mon == TM_JANUARY) 308 || (w == 1 309 && t->tm_mon == TM_DECEMBER)) 310 w = 53; |
295#endif /* defined XPG4_1994_04_09 */ | 311#endif /* defined XPG4_1994_04_09 */ |
296 } 297 pt = _conv(i, "%02d", pt, ptlim); | 312 if (*format == 'V') 313 pt = _conv(w, "%02d", 314 pt, ptlim); 315 else if (*format == 'g') { 316 pt = _conv(year % 100, "%02d", 317 pt, ptlim); 318 } else pt = _conv(year, "%04d", 319 pt, ptlim); |
298 } 299 continue; 300 case 'v': 301 /* 302 ** From Arnold Robbins' strftime version 3.0: 303 ** "date as dd-bbb-YYYY" 304 ** (ado, 5/24/93) 305 */ --- 94 unchanged lines hidden --- | 320 } 321 continue; 322 case 'v': 323 /* 324 ** From Arnold Robbins' strftime version 3.0: 325 ** "date as dd-bbb-YYYY" 326 ** (ado, 5/24/93) 327 */ --- 94 unchanged lines hidden --- |