diff options
Diffstat (limited to 'src/libical/icaltime.c')
-rw-r--r-- | src/libical/icaltime.c | 100 |
1 files changed, 41 insertions, 59 deletions
diff --git a/src/libical/icaltime.c b/src/libical/icaltime.c index b8b0f11b..1b1821d8 100644 --- a/src/libical/icaltime.c +++ b/src/libical/icaltime.c @@ -2,20 +2,11 @@ FILE: icaltime.c CREATOR: eric 02 June 2000 - (C) COPYRIGHT 2000, Eric Busboom <eric@civicknowledge.com> + SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com> The timegm code is Copyright (c) 2001-2006, NLnet Labs. All rights reserved. - This library is free software; you can redistribute it and/or modify - it under the terms of either: - - The LGPL as published by the Free Software Foundation, version - 2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html - - Or: - - The Mozilla Public License Version 2.0. You may obtain a copy of - the License at https://www.mozilla.org/MPL/ + SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0 The Original Code is eric. The Initial Developer of the Original Code is Eric Busboom @@ -77,16 +68,16 @@ static int icaltime_leap_days(int y1, int y2) /* * Code adapted from Python 2.4.1 sources (Lib/calendar.py). */ -static time_t icaltime_timegm(const struct tm *tm) +static icaltime_t icaltime_timegm(const struct tm *tm) { int year; - time_t days; - time_t hours; - time_t minutes; - time_t seconds; + icaltime_t days; + icaltime_t hours; + icaltime_t minutes; + icaltime_t seconds; year = 1900 + tm->tm_year; - days = (time_t)(365 * (year - 1970) + icaltime_leap_days(1970, year)); + days = (icaltime_t)(365 * (year - 1970) + icaltime_leap_days(1970, year)); days += days_in_year_passed_month[0][tm->tm_mon]; if (tm->tm_mon > 1 && icaltime_is_leap_year(year)) @@ -102,49 +93,49 @@ static time_t icaltime_timegm(const struct tm *tm) /* * Function to convert a struct tm time specification - * to an ANSI time_t using the specified time zone. + * to an ANSI-compatible icaltime_t using the specified time zone. * This is different from the standard mktime() function * in that we don't want the automatic adjustments for * local daylight savings time applied to the result. * This function expects well-formed input. */ -static time_t make_time(struct tm *tm, int tzm) +static icaltime_t make_time(struct tm *tm, int tzm) { - time_t tim; + icaltime_t tim; int febs; - static int days[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 }; + static const int days[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 }; /* check that month specification within range */ if (tm->tm_mon < 0 || tm->tm_mon > 11) - return ((time_t) - 1); + return ((icaltime_t) - 1); if (tm->tm_year < 2) - return ((time_t)-1); + return ((icaltime_t)-1); -#if (SIZEOF_TIME_T == 4) +#if (SIZEOF_ICALTIME_T == 4) /* check that year specification within range */ if (tm->tm_year > 138) - return ((time_t) - 1); + return ((icaltime_t) - 1); /* check for upper bound of Jan 17, 2038 (to avoid possibility of 32-bit arithmetic overflow) */ if (tm->tm_year == 138) { if (tm->tm_mon > 0) { - return ((time_t) - 1); + return ((icaltime_t) - 1); } else if (tm->tm_mday > 17) { - return ((time_t) - 1); + return ((icaltime_t) - 1); } } #else /* We don't support years >= 10000, because the function has not been tested at this range. */ if (tm->tm_year >= 8100) { - return ((time_t)-1); + return ((icaltime_t)-1); } -#endif /* SIZEOF_TIME_T */ +#endif /* SIZEOF_ICALTIME_T */ /* * calculate elapsed days since start of the epoch (midnight Jan @@ -152,7 +143,7 @@ static time_t make_time(struct tm *tm, int tzm) * (number of leap days to subtract) */ - tim = (time_t) ((tm->tm_year - 70) * 365 + ((tm->tm_year - 1) / 4) - 17); + tim = (icaltime_t) ((tm->tm_year - 70) * 365 + ((tm->tm_year - 1) / 4) - 17); /* adjust: no leap days every 100 years, except every 400 years. */ @@ -194,7 +185,7 @@ static time_t make_time(struct tm *tm, int tzm) return (tim); } -struct icaltimetype icaltime_from_timet_with_zone(const time_t tm, const int is_date, +struct icaltimetype icaltime_from_timet_with_zone(const icaltime_t tm, const int is_date, const icaltimezone *zone) { struct icaltimetype tt; @@ -203,8 +194,8 @@ struct icaltimetype icaltime_from_timet_with_zone(const time_t tm, const int is_ utc_zone = icaltimezone_get_utc_timezone(); - /* Convert the time_t to a struct tm in UTC time. We can trust gmtime for this. */ - if (!gmtime_r(&tm, &t)) + /* Convert the icaltime_t to a struct tm in UTC time. We can trust gmtime for this. */ + if (!icalgmtime_r(&tm, &t)) return is_date ? icaltime_null_date () : icaltime_null_time (); tt.year = t.tm_year + 1900; @@ -234,18 +225,18 @@ struct icaltimetype icaltime_from_timet_with_zone(const time_t tm, const int is_ struct icaltimetype icaltime_current_time_with_zone(const icaltimezone *zone) { - return icaltime_from_timet_with_zone(time(NULL), 0, zone); + return icaltime_from_timet_with_zone(icaltime(NULL), 0, zone); } struct icaltimetype icaltime_today(void) { - return icaltime_from_timet_with_zone(time(NULL), 1, NULL); + return icaltime_from_timet_with_zone(icaltime(NULL), 1, NULL); } -time_t icaltime_as_timet(const struct icaltimetype tt) +icaltime_t icaltime_as_timet(const struct icaltimetype tt) { struct tm stm; - time_t t; + icaltime_t t; /* If the time is the special null time, return 0. */ if (icaltime_is_null_time(tt)) { @@ -273,11 +264,11 @@ time_t icaltime_as_timet(const struct icaltimetype tt) return t; } -time_t icaltime_as_timet_with_zone(const struct icaltimetype tt, const icaltimezone *zone) +icaltime_t icaltime_as_timet_with_zone(const struct icaltimetype tt, const icaltimezone *zone) { icaltimezone *utc_zone; struct tm stm; - time_t t; + icaltime_t t; struct icaltimetype local_tt; utc_zone = icaltimezone_get_utc_timezone(); @@ -474,38 +465,32 @@ int icaltime_days_in_month(const int month, const int year) /* 1-> Sunday, 7->Saturday */ int icaltime_day_of_week(const struct icaltimetype t) { - UTinstant jt; + UTinstantInt jt; - memset(&jt, 0, sizeof(UTinstant)); + memset(&jt, 0, sizeof(UTinstantInt)); jt.year = t.year; jt.month = t.month; jt.day = t.day; - jt.i_hour = 0; - jt.i_minute = 0; - jt.i_second = 0; - (void)juldat(&jt); + juldat_int(&jt); return jt.weekday + 1; } int icaltime_start_doy_week(const struct icaltimetype t, int fdow) { - UTinstant jt; + UTinstantInt jt; int delta; - memset(&jt, 0, sizeof(UTinstant)); + memset(&jt, 0, sizeof(UTinstantInt)); jt.year = t.year; jt.month = t.month; jt.day = t.day; - jt.i_hour = 0; - jt.i_minute = 0; - jt.i_second = 0; - (void)juldat(&jt); - (void)caldat(&jt); + juldat_int(&jt); + caldat_int(&jt); delta = jt.weekday - (fdow - 1); if (delta < 0) { @@ -516,19 +501,16 @@ int icaltime_start_doy_week(const struct icaltimetype t, int fdow) int icaltime_week_number(const struct icaltimetype ictt) { - UTinstant jt; + UTinstantInt jt; - memset(&jt, 0, sizeof(UTinstant)); + memset(&jt, 0, sizeof(UTinstantInt)); jt.year = ictt.year; jt.month = ictt.month; jt.day = ictt.day; - jt.i_hour = 0; - jt.i_minute = 0; - jt.i_second = 0; - (void)juldat(&jt); - (void)caldat(&jt); + juldat_int(&jt); + caldat_int(&jt); return (jt.day_of_year - jt.weekday) / 7; } |