Mercurial > pidgin
comparison src/util.c @ 13152:4bb701a8736f
[gaim-migrate @ 15515]
gaim_utf8_strftime() will now provide %z itself if your C library doesn't have it. I'm going to use this shortly.
committer: Tailor Script <tailor@pidgin.im>
| author | Richard Laager <rlaager@wiktel.com> |
|---|---|
| date | Tue, 07 Feb 2006 07:25:45 +0000 |
| parents | f62d14ba98f6 |
| children | b230ed49c5d1 |
comparison
equal
deleted
inserted
replaced
| 13151:1646cd4f00ad | 13152:4bb701a8736f |
|---|---|
| 482 | 482 |
| 483 | 483 |
| 484 /************************************************************************** | 484 /************************************************************************** |
| 485 * Date/Time Functions | 485 * Date/Time Functions |
| 486 **************************************************************************/ | 486 **************************************************************************/ |
| 487 | |
| 488 #ifndef HAVE_STRFTIME_Z_FORMAT | |
| 489 static const char *get_tmoff(const struct tm *tm) | |
| 490 { | |
| 491 static char buf[6]; | |
| 492 long off; | |
| 493 gint8 min; | |
| 494 gint8 hrs; | |
| 495 struct tm new_tm = *tm; | |
| 496 | |
| 497 mktime(&new_tm); | |
| 498 | |
| 499 if (new_tm.tm_isdst < 0) | |
| 500 g_return_val_if_reached(""); | |
| 501 | |
| 502 #ifdef _WIN32 | |
| 503 TIME_ZONE_INFORMATION tzi; | |
| 504 DWORD ret; | |
| 505 if ((ret = GetTimeZoneInformation(&tzi)) != TIME_ZONE_ID_INVALID) | |
| 506 { | |
| 507 off = tzi.Bias * 60; | |
| 508 if (ret == TIME_ZONE_ID_DAYLIGHT) | |
| 509 off -= tzi.DaylightBias * 60; | |
| 510 } | |
| 511 else | |
| 512 return ""; | |
| 513 #else | |
| 514 # ifdef HAVE_TM_GMTOFF | |
| 515 off = new_tm.tm_gmtoff; | |
| 516 # else | |
| 517 # ifdef HAVE_TIMEZONE | |
| 518 tzset(); | |
| 519 off = -timezone; | |
| 520 # endif /* HAVE_TIMEZONE */ | |
| 521 # endif /* !HAVE_TM_GMTOFF */ | |
| 522 #endif /* _WIN32 */ | |
| 523 | |
| 524 min = (off / 60) % 60; | |
| 525 hrs = ((off / 60) - min) / 60; | |
| 526 | |
| 527 if (g_snprintf(buf, sizeof(buf), "%+03d%02d", hrs, ABS(min)) > 5) | |
| 528 g_return_val_if_reached(""); | |
| 529 | |
| 530 return buf; | |
| 531 } | |
| 532 | |
| 533 static size_t gaim_internal_strftime(char *s, size_t max, const char *format, const struct tm *tm) | |
| 534 { | |
| 535 const char *start; | |
| 536 const char *c; | |
| 537 char *fmt = NULL; | |
| 538 | |
| 539 /* Yes, this is checked in gaim_utf8_strftime(), | |
| 540 * but better safe than sorry. -- rlaager */ | |
| 541 g_return_val_if_fail(format != NULL, 0); | |
| 542 | |
| 543 /* This is fairly efficient, and it only gets | |
| 544 * executed if the underlying system doesn't | |
| 545 * support the %z format string for strftime(), | |
| 546 * so I think it's good enough. -- rlaager */ | |
| 547 for (c = start = format; *c ; c++) | |
| 548 { | |
| 549 if (*c != '%') | |
| 550 continue; | |
| 551 | |
| 552 c++; | |
| 553 | |
| 554 if (*c == 'z') | |
| 555 { | |
| 556 char *tmp = g_strdup_printf("%s%.*s%s", | |
| 557 fmt ? fmt : "", | |
| 558 c - start - 1, | |
| 559 start, | |
| 560 get_tmoff(tm)); | |
| 561 g_free(fmt); | |
| 562 fmt = tmp; | |
| 563 start = c + 1; | |
| 564 } | |
| 565 } | |
| 566 | |
| 567 if (fmt != NULL) | |
| 568 { | |
| 569 size_t ret; | |
| 570 | |
| 571 if (*start) | |
| 572 { | |
| 573 char *tmp = g_strconcat(fmt, start, NULL); | |
| 574 g_free(fmt); | |
| 575 fmt = tmp; | |
| 576 } | |
| 577 | |
| 578 ret = strftime(s, max, fmt, tm); | |
| 579 g_free(fmt); | |
| 580 | |
| 581 return ret; | |
| 582 } | |
| 583 | |
| 584 return strftime(s, max, format, tm); | |
| 585 } | |
| 586 #else /* !HAVE_STRFTIME_Z_FORMAT */ | |
| 587 #define gaim_internal_strftime strftime | |
| 588 #endif | |
| 589 | |
| 487 const char * | 590 const char * |
| 488 gaim_utf8_strftime(const char *format, const struct tm *tm) | 591 gaim_utf8_strftime(const char *format, const struct tm *tm) |
| 489 { | 592 { |
| 490 static char buf[128]; | 593 static char buf[128]; |
| 491 char *utf8; | 594 char *utf8; |
| 500 | 603 |
| 501 /* A return value of 0 is either an error (in | 604 /* A return value of 0 is either an error (in |
| 502 * which case, the contents of the buffer are | 605 * which case, the contents of the buffer are |
| 503 * undefined) or the empty string (in which | 606 * undefined) or the empty string (in which |
| 504 * case, no harm is done here). */ | 607 * case, no harm is done here). */ |
| 505 if (strftime(buf, sizeof(buf), format, tm) == 0) | 608 if (gaim_internal_strftime(buf, sizeof(buf), format, tm) == 0) |
| 506 { | 609 { |
| 507 buf[0] = '\0'; | 610 buf[0] = '\0'; |
| 508 return buf; | 611 return buf; |
| 509 } | 612 } |
| 510 | 613 |
