1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* soup-date.c: Date/time handling
*
* Copyright (C) 2005, Novell, Inc.
* Copyright (C) 2007, Red Hat, Inc.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "soup-date.h"
#include "soup.h"
/**
* SoupDate:
* @year: the year, 1 to 9999
* @month: the month, 1 to 12
* @day: day of the month, 1 to 31
* @hour: hour of the day, 0 to 23
* @minute: minute, 0 to 59
* @second: second, 0 to 59 (or up to 61 in the case of leap seconds)
* @utc: %TRUE if the date is in UTC
* @offset: offset from UTC
* A date and time. The date is assumed to be in the (proleptic)
* Gregorian calendar. The time is in UTC if @utc is %TRUE. Otherwise,
* the time is a local time, and @offset gives the offset from UTC in
* minutes (such that adding @offset to the time would give the
* correct UTC time). If @utc is %FALSE and @offset is 0, then the
* %SoupDate represents a "floating" time with no associated timezone
* information.
**/
/* Do not internationalize */
static const char *const months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
/* Do not internationalize */
static const char *const days[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const int nonleap_days_in_month[] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static const int nonleap_days_before[] = {
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
static inline gboolean
is_leap_year (int year)
{
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
/* Computes the number of days since proleptic Gregorian 0000-12-31.
* (That is, 0001-01-01 is "1", and 1970-01-01 is 719163.
*/
static int
rata_die_day (SoupDate *date)
{
int day;
day = (date->year - 1) * 365 + ((date->year - 1) / 4) -
((date->year - 1) / 100) + ((date->year - 1) / 400);
day += nonleap_days_before[date->month] + date->day;
if (is_leap_year (date->year) && date->month > 2)
day++;
return day;
}
#define TIME_T_EPOCH_RATA_DIE_DAY 719163
static inline int
days_in_month (int month, int year)
{
if (month == 2 && is_leap_year (year))
return 29;
else
return nonleap_days_in_month[month];
}
G_DEFINE_BOXED_TYPE (SoupDate, soup_date, soup_date_copy, soup_date_free)
static void
soup_date_fixup (SoupDate *date)
{
/* We only correct date->second if it's negative or too high
* to be a leap second.
*/
if (date->second < 0 || date->second > 61) {
date->minute += date->second / 60;
date->second %= 60;
if (date->second < 0)
date->second += 60;
}
if (date->minute < 0 || date->minute > 59) {
date->hour += date->minute / 60;
date->minute %= 60;
if (date->minute < 0)
date->minute += 60;
}
if (date->hour < 0 || date->hour > 23) {
date->day += date->hour / 24;
date->hour %= 24;
if (date->hour < 0)
date->hour += 24;
}
/* Have to make sure month is valid before we can look at the
* day.
*/
if (date->month < 1 || date->month > 12) {
date->year += ((date->month - 1) / 12) + 1;
date->month = ((date->month - 1) % 12) + 1;
if (date->month < 1)
date->month += 12;
}
if (date->day < 0) {
while (date->day < 0) {
if (date->month == 1) {
date->month = 12;
date->year--;
} else
date->month--;
date->day += days_in_month (date->month, date->year);
}
} else {
while (date->day > days_in_month (date->month, date->year)) {
date->day -= days_in_month (date->month, date->year);
if (date->month == 12) {
date->month = 1;
date->year++;
} else
date->month++;
}
}
}
/**
* soup_date_new:
* @year: the year (1-9999)
* @month: the month (1-12)
* @day: the day of the month (1-31, as appropriate for @month)
* @hour: the hour (0-23)
* @minute: the minute (0-59)
* @second: the second (0-59, or up to 61 for leap seconds)
*
* Creates a #SoupDate representing the indicated time, UTC.
*
* Return value: a new #SoupDate
**/
SoupDate *
soup_date_new (int year, int month, int day,
int hour, int minute, int second)
{
SoupDate *date = g_slice_new (SoupDate);
date->year = year;
date->month = month;
date->day = day;
date->hour = hour;
date->minute = minute;
date->second = second;
date->utc = TRUE;
date->offset = 0;
return date;
}
/**
* soup_date_new_from_now:
* @offset_seconds: offset from current time
*
* Creates a #SoupDate representing a time @offset_seconds after the
* current time (or before it, if @offset_seconds is negative). If
* offset_seconds is 0, returns the current time.
*
* If @offset_seconds would indicate a time not expressible as a
* <type>time_t</type>, the return value will be clamped into range.
*
* Return value: a new #SoupDate
**/
SoupDate *
soup_date_new_from_now (int offset_seconds)
{
time_t now = time (NULL);
time_t then = now + offset_seconds;
if (sizeof (time_t) == 4) {
if (offset_seconds < 0 && then > now)
return soup_date_new_from_time_t (-G_MAXINT);
else if (offset_seconds > 0 && then < now)
return soup_date_new_from_time_t (G_MAXINT);
}
return soup_date_new_from_time_t (then);
}
static gboolean
parse_iso8601_date (SoupDate *date, const char *date_string)
{
gulong val;
if (strlen (date_string) < 15)
return FALSE;
if (date_string[4] == '-' &&
date_string[7] == '-' &&
date_string[10] == 'T') {
/* YYYY-MM-DD */
date->year = atoi (date_string);
date->month = atoi (date_string + 5);
date->day = atoi (date_string + 8);
date_string += 11;
} else if (date_string[8] == 'T') {
/* YYYYMMDD */
val = atoi (date_string);
date->year = val / 10000;
date->month = (val % 10000) / 100;
date->day = val % 100;
date_string += 9;
} else
return FALSE;
if (strlen (date_string) >= 8 &&
date_string[2] == ':' && date_string[5] == ':') {
/* HH:MM:SS */
date->hour = atoi (date_string);
date->minute = atoi (date_string + 3);
date->second = atoi (date_string + 6);
date_string += 8;
} else if (strlen (date_string) >= 6) {
/* HHMMSS */
val = strtoul (date_string, (char **)&date_string, 10);
date->hour = val / 10000;
date->minute = (val % 10000) / 100;
date->second = val % 100;
} else
return FALSE;
if (*date_string == '.' || *date_string == ',')
(void) strtoul (date_string + 1, (char **)&date_string, 10);
if (*date_string == 'Z') {
date_string++;
date->utc = TRUE;
date->offset = 0;
} else if (*date_string == '+' || *date_string == '-') {
int sign = (*date_string == '+') ? -1 : 1;
val = strtoul (date_string + 1, (char **)&date_string, 10);
if (*date_string == ':')
val = 60 * val + strtoul (date_string + 1, (char **)&date_string, 10);
else
val = 60 * (val / 100) + (val % 100);
date->offset = sign * val;
date->utc = !val;
} else {
date->offset = 0;
date->utc = FALSE;
}
return !*date_string;
}
static inline gboolean
parse_day (SoupDate *date, const char **date_string)
{
char *end;
date->day = strtoul (*date_string, &end, 10);
if (end == (char *)*date_string)
return FALSE;
while (*end == ' ' || *end == '-')
end++;
*date_string = end;
return TRUE;
}
static inline gboolean
parse_month (SoupDate *date, const char **date_string)
{
int i;
for (i = 0; i < G_N_ELEMENTS (months); i++) {
if (!g_ascii_strncasecmp (*date_string, months[i], 3)) {
date->month = i + 1;
*date_string += 3;
while (**date_string == ' ' || **date_string == '-')
(*date_string)++;
return TRUE;
}
}
return FALSE;
}
static inline gboolean
parse_year (SoupDate *date, const char **date_string)
{
char *end;
date->year = strtoul (*date_string, &end, 10);
if (end == (char *)*date_string)
return FALSE;
if (end == (char *)*date_string + 2) {
if (date->year < 70)
date->year += 2000;
else
date->year += 1900;
} else if (end == (char *)*date_string + 3)
date->year += 1900;
while (*end == ' ' || *end == '-')
end++;
*date_string = end;
return TRUE;
}
static inline gboolean
parse_time (SoupDate *date, const char **date_string)
{
char *p, *end;
date->hour = strtoul (*date_string, &end, 10);
if (end == (char *)*date_string || *end++ != ':')
return FALSE;
p = end;
date->minute = strtoul (p, &end, 10);
if (end == p || *end++ != ':')
return FALSE;
p = end;
date->second = strtoul (p, &end, 10);
if (end == p)
return FALSE;
p = end;
while (*p == ' ')
p++;
*date_string = p;
return TRUE;
}
static inline gboolean
parse_timezone (SoupDate *date, const char **date_string)
{
if (!**date_string) {
date->utc = FALSE;
date->offset = 0;
} else if (**date_string == '+' || **date_string == '-') {
gulong val;
int sign = (**date_string == '+') ? -1 : 1;
val = strtoul (*date_string + 1, (char **)date_string, 10);
if (**date_string == ':')
val = 60 * val + strtoul (*date_string + 1, (char **)date_string, 10);
else
val = 60 * (val / 100) + (val % 100);
date->offset = sign * val;
date->utc = (sign == -1) && !val;
} else if (**date_string == 'Z') {
date->offset = 0;
date->utc = TRUE;
(*date_string)++;
} else if (!strcmp (*date_string, "GMT") ||
!strcmp (*date_string, "UTC")) {
date->offset = 0;
date->utc = TRUE;
(*date_string) += 3;
} else if (strchr ("ECMP", **date_string) &&
((*date_string)[1] == 'D' || (*date_string)[1] == 'S') &&
(*date_string)[2] == 'T') {
date->offset = -60 * (5 * strcspn ("ECMP", *date_string));
if ((*date_string)[1] == 'D')
date->offset += 60;
date->utc = FALSE;
} else
return FALSE;
return TRUE;
}
static gboolean
parse_textual_date (SoupDate *date, const char *date_string)
{
/* If it starts with a word, it must be a weekday, which we skip */
if (g_ascii_isalpha (*date_string)) {
while (g_ascii_isalpha (*date_string))
date_string++;
if (*date_string == ',')
date_string++;
while (g_ascii_isspace (*date_string))
date_string++;
}
/* If there's now another word, this must be an asctime-date */
if (g_ascii_isalpha (*date_string)) {
/* (Sun) Nov 6 08:49:37 1994 */
if (!parse_month (date, &date_string) ||
!parse_day (date, &date_string) ||
!parse_time (date, &date_string) ||
!parse_year (date, &date_string))
return FALSE;
/* There shouldn't be a timezone, but check anyway */
parse_timezone (date, &date_string);
} else {
/* Non-asctime date, so some variation of
* (Sun,) 06 Nov 1994 08:49:37 GMT
*/
if (!parse_day (date, &date_string) ||
!parse_month (date, &date_string) ||
!parse_year (date, &date_string) ||
!parse_time (date, &date_string))
return FALSE;
/* This time there *should* be a timezone, but we
* survive if there isn't.
*/
parse_timezone (date, &date_string);
}
return TRUE;
}
/**
* SoupDateFormat:
* @SOUP_DATE_HTTP: RFC 1123 format, used by the HTTP "Date" header. Eg
* "Sun, 06 Nov 1994 08:49:37 GMT"
* @SOUP_DATE_COOKIE: The format for the "Expires" timestamp in the
* Netscape cookie specification. Eg, "Sun, 06-Nov-1994 08:49:37 GMT".
* @SOUP_DATE_RFC2822: RFC 2822 format, eg "Sun, 6 Nov 1994 09:49:37 -0100"
* @SOUP_DATE_ISO8601_COMPACT: ISO 8601 date/time with no optional
* punctuation. Eg, "19941106T094937-0100".
* @SOUP_DATE_ISO8601_FULL: ISO 8601 date/time with all optional
* punctuation. Eg, "1994-11-06T09:49:37-01:00".
* @SOUP_DATE_ISO8601_XMLRPC: ISO 8601 date/time as used by XML-RPC.
* Eg, "19941106T09:49:37".
* @SOUP_DATE_ISO8601: An alias for @SOUP_DATE_ISO8601_FULL.
*
* Date formats that soup_date_to_string() can use.
*
* @SOUP_DATE_HTTP and @SOUP_DATE_COOKIE always coerce the time to
* UTC. @SOUP_DATE_ISO8601_XMLRPC uses the time as given, ignoring the
* offset completely. @SOUP_DATE_RFC2822 and the other ISO 8601
* variants use the local time, appending the offset information if
* available.
*
* This enum may be extended with more values in future releases.
**/
/**
* soup_date_new_from_string:
* @date_string: the date in some plausible format
*
* Parses @date_string and tries to extract a date from it. This
* recognizes all of the "HTTP-date" formats from RFC 2616, all ISO
* 8601 formats containing both a time and a date, RFC 2822 dates,
* and reasonable approximations thereof. (Eg, it is lenient about
* whitespace, leading "0"s, etc.)
*
* Return value: (nullable): a new #SoupDate, or %NULL if @date_string
* could not be parsed.
**/
SoupDate *
soup_date_new_from_string (const char *date_string)
{
SoupDate *date;
gboolean success;
g_return_val_if_fail (date_string != NULL, NULL);
date = g_slice_new (SoupDate);
while (g_ascii_isspace (*date_string))
date_string++;
/* If it starts with a digit, it's either an ISO 8601 date, or
* an RFC2822 date without the optional weekday; in the later
* case, there will be a month name later on, so look for one
* of the month-start letters.
*/
if (g_ascii_isdigit (*date_string) &&
!strpbrk (date_string, "JFMASOND"))
success = parse_iso8601_date (date, date_string);
else
success = parse_textual_date (date, date_string);
if (!success) {
g_slice_free (SoupDate, date);
return NULL;
}
if (date->year < 1 || date->year > 9999 ||
date->month < 1 || date->month > 12 ||
date->day < 1 ||
date->day > days_in_month (date->month, date->year) ||
date->hour < 0 || date->hour > 24 ||
date->minute < 0 || date->minute > 59 ||
date->second < 0 || date->second > 61) {
soup_date_free (date);
return NULL;
}
if (date->hour == 24) {
/* ISO8601 allows this explicitly. We allow it for
* other types as well just for simplicity.
*/
if (date->minute == 0 && date->second == 0)
soup_date_fixup (date);
else {
soup_date_free (date);
return NULL;
}
}
return date;
}
/**
* soup_date_new_from_time_t:
* @when: a <type>time_t</type>
*
* Creates a #SoupDate corresponding to @when
*
* Return value: a new #SoupDate
**/
SoupDate *
soup_date_new_from_time_t (time_t when)
{
struct tm tm;
#ifdef HAVE_GMTIME_R
gmtime_r (&when, &tm);
#else
tm = *gmtime (&when);
#endif
return soup_date_new (tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
}
static const char *
soup_date_weekday (SoupDate *date)
{
/* Proleptic Gregorian 0001-01-01 was a Monday, which
* corresponds to 1 in the days[] array.
*/
return days[rata_die_day (date) % 7];
}
/**
* soup_date_to_string:
* @date: a #SoupDate
* @format: the format to generate the date in
*
* Converts @date to a string in the format described by @format.
*
* Return value: @date as a string
**/
char *
soup_date_to_string (SoupDate *date, SoupDateFormat format)
{
g_return_val_if_fail (date != NULL, NULL);
if (format == SOUP_DATE_HTTP || format == SOUP_DATE_COOKIE) {
/* HTTP and COOKIE formats require UTC timestamp, so coerce
* @date if it's non-UTC.
*/
SoupDate utcdate;
if (date->offset != 0) {
memcpy (&utcdate, date, sizeof (SoupDate));
utcdate.minute += utcdate.offset;
utcdate.offset = 0;
utcdate.utc = TRUE;
soup_date_fixup (&utcdate);
date = &utcdate;
}
switch (format) {
case SOUP_DATE_HTTP:
/* "Sun, 06 Nov 1994 08:49:37 GMT" */
return g_strdup_printf (
"%s, %02d %s %04d %02d:%02d:%02d GMT",
soup_date_weekday (date), date->day,
months[date->month - 1], date->year,
date->hour, date->minute, date->second);
case SOUP_DATE_COOKIE:
/* "Sun, 06-Nov-1994 08:49:37 GMT" */
return g_strdup_printf (
"%s, %02d-%s-%04d %02d:%02d:%02d GMT",
soup_date_weekday (date), date->day,
months[date->month - 1], date->year,
date->hour, date->minute, date->second);
default:
g_return_val_if_reached (NULL);
}
} else if (format == SOUP_DATE_ISO8601_XMLRPC) {
/* Always "floating", ignore offset */
return g_strdup_printf ("%04d%02d%02dT%02d:%02d:%02d",
date->year, date->month, date->day,
date->hour, date->minute, date->second);
} else {
int hour_offset, minute_offset;
char zone[8], sign;
/* For other ISO8601 formats or RFC2822, use the
* offset given in @date. For ISO8601 formats, use "Z"
* for UTC, +-offset for non-UTC, and nothing for
* floating. For RFC2822, use +-offset for UTC or
* non-UTC, and -0000 for floating.
*/
hour_offset = abs (date->offset) / 60;
minute_offset = abs (date->offset) - hour_offset * 60;
switch (format) {
case SOUP_DATE_ISO8601_COMPACT:
/* "19941106T084937[zone]" */
if (date->utc)
strcpy (zone, "Z");
else if (date->offset) {
g_snprintf (zone, sizeof (zone), "%c%02d%02d",
date->offset > 0 ? '-' : '+',
hour_offset, minute_offset);
} else
*zone = '\0';
return g_strdup_printf (
"%04d%02d%02dT%02d%02d%02d%s",
date->year, date->month, date->day,
date->hour, date->minute, date->second,
zone);
case SOUP_DATE_ISO8601_FULL:
/* "1994-11-06T08:49:37[zone]" */
if (date->utc)
strcpy (zone, "Z");
else if (date->offset) {
g_snprintf (zone, sizeof (zone), "%c%02d:%02d",
date->offset > 0 ? '-' : '+',
hour_offset, minute_offset);
} else
*zone = '\0';
return g_strdup_printf (
"%04d-%02d-%02dT%02d:%02d:%02d%s",
date->year, date->month, date->day,
date->hour, date->minute, date->second,
zone);
case SOUP_DATE_RFC2822:
/* "Sun, 6 Nov 1994 09:49:37 -0100" */
if (date->offset)
sign = (date->offset > 0) ? '-' : '+';
else
sign = date->utc ? '+' : '-';
return g_strdup_printf (
"%s, %d %s %04d %02d:%02d:%02d %c%02d%02d",
soup_date_weekday (date), date->day,
months[date->month - 1], date->year,
date->hour, date->minute, date->second,
sign, hour_offset, minute_offset);
default:
return NULL;
}
}
}
/**
* soup_date_to_time_t:
* @date: a #SoupDate
*
* Converts @date to a <type>time_t</type>, assumming it to be in
* UTC.
*
* If @date is not representable as a <type>time_t</type>, it will be
* clamped into range. (In particular, some HTTP cookies have
* expiration dates after "Y2.038k" (2038-01-19T03:14:07Z).)
*
* Return value: @date as a <type>time_t</type>
**/
time_t
soup_date_to_time_t (SoupDate *date)
{
GDateTime *datetime;
gint64 seconds;
g_return_val_if_fail (date != NULL, 0);
if (date->year < 1970)
return 0;
/* If the year is later than 2038, we're guaranteed to
* overflow a 32-bit time_t. (If it's exactly 2038, we'll
* *probably* overflow, but only by a little, and it's easiest
* to just clamp down the value if it's above G_MAXINT32.
*/
if (sizeof (time_t) == 4 && date->year > 2038)
return (time_t)G_MAXINT32;
datetime = g_date_time_new_utc (date->year,
date->month,
date->day,
date->hour,
date->minute,
date->second);
seconds = g_date_time_to_unix (datetime);
g_date_time_unref (datetime);
return (time_t) (sizeof (time_t) == 4 ? MIN(seconds, G_MAXINT32) : seconds);
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/**
* soup_date_to_timeval:
* @date: a #SoupDate
* @time: (out): a #GTimeVal structure in which to store the converted time.
*
* Converts @date to a #GTimeVal.
*
* Deprecated: Do not use #GTimeVal, as it's not Y2038-safe.
*
* Since: 2.24
*/
void
soup_date_to_timeval (SoupDate *date, GTimeVal *time)
{
g_return_if_fail (date != NULL);
g_return_if_fail (time != NULL);
/* FIXME: offset, etc */
time->tv_sec = rata_die_day (date) - TIME_T_EPOCH_RATA_DIE_DAY;
time->tv_sec = ((((time->tv_sec * 24) + date->hour) * 60) + date->minute) * 60 + date->second;
time->tv_usec = 0;
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* soup_date_is_past:
* @date: a #SoupDate
*
* Determines if @date is in the past.
*
* Return value: %TRUE if @date is in the past
*
* Since: 2.24
**/
gboolean
soup_date_is_past (SoupDate *date)
{
g_return_val_if_fail (date != NULL, TRUE);
/* optimization */
if (date->year < 2020)
return TRUE;
return soup_date_to_time_t (date) < time (NULL);
}
/**
* soup_date_get_year:
* @date: a #SoupDate
*
* Gets @date's year.
*
* Return value: @date's year
*
* Since: 2.32
**/
int
soup_date_get_year (SoupDate *date)
{
return date->year;
}
/**
* soup_date_get_month:
* @date: a #SoupDate
*
* Gets @date's month.
*
* Return value: @date's month
*
* Since: 2.32
**/
int
soup_date_get_month (SoupDate *date)
{
return date->month;
}
/**
* soup_date_get_day:
* @date: a #SoupDate
*
* Gets @date's day.
*
* Return value: @date's day
*
* Since: 2.32
**/
int
soup_date_get_day (SoupDate *date)
{
return date->day;
}
/**
* soup_date_get_hour:
* @date: a #SoupDate
*
* Gets @date's hour.
*
* Return value: @date's hour
*
* Since: 2.32
**/
int
soup_date_get_hour (SoupDate *date)
{
return date->hour;
}
/**
* soup_date_get_minute:
* @date: a #SoupDate
*
* Gets @date's minute.
*
* Return value: @date's minute
*
* Since: 2.32
**/
int
soup_date_get_minute (SoupDate *date)
{
return date->minute;
}
/**
* soup_date_get_second:
* @date: a #SoupDate
*
* Gets @date's second.
*
* Return value: @date's second
*
* Since: 2.32
**/
int
soup_date_get_second (SoupDate *date)
{
return date->second;
}
/**
* soup_date_get_utc:
* @date: a #SoupDate
*
* Gets @date's UTC flag
*
* Return value: %TRUE if @date is UTC.
*
* Since: 2.32
**/
gboolean
soup_date_get_utc (SoupDate *date)
{
return date->utc;
}
/**
* soup_date_get_offset:
* @date: a #SoupDate
*
* Gets @date's offset from UTC.
*
* Return value: @date's offset from UTC. If soup_date_get_utc()
* returns %FALSE but soup_date_get_offset() returns 0, that means the
* date is a "floating" time with no associated offset information.
*
* Since: 2.32
**/
int
soup_date_get_offset (SoupDate *date)
{
return date->offset;
}
/**
* soup_date_copy:
* @date: a #SoupDate
*
* Copies @date.
*
* Since: 2.24
**/
SoupDate *
soup_date_copy (SoupDate *date)
{
SoupDate *copy;
g_return_val_if_fail (date != NULL, NULL);
copy = g_slice_new (SoupDate);
memcpy (copy, date, sizeof (SoupDate));
return copy;
}
/**
* soup_date_free:
* @date: a #SoupDate
*
* Frees @date.
*
* Since: 2.24
**/
void
soup_date_free (SoupDate *date)
{
g_return_if_fail (date != NULL);
g_slice_free (SoupDate, date);
}
|