Thu, 13 Jan 2022 23:47:42 -0600
remove purple_str_to_time and other unused time utility functions
Testing Done:
Compiled, ran tests, and ran the program.
Reviewed at https://reviews.imfreedom.org/r/1226/
| ChangeLog.API | file | annotate | diff | comparison | revisions | |
| libpurple/tests/test_util.c | file | annotate | diff | comparison | revisions | |
| libpurple/util.c | file | annotate | diff | comparison | revisions | |
| libpurple/util.h | file | annotate | diff | comparison | revisions |
--- a/ChangeLog.API Thu Jan 13 23:13:13 2022 -0600 +++ b/ChangeLog.API Thu Jan 13 23:47:42 2022 -0600 @@ -59,7 +59,6 @@ * purple_roomlist_room_get_expanded_once * purple_roomlist_room_set_expanded_once * purple_roomlist_set_proto_data - * purple_time_parse_month * purple_whiteboard_get_account * purple_whiteboard_get_draw_list * purple_whiteboard_set_draw_list @@ -659,6 +658,8 @@ * purple_str_has_prefix. Use g_str_has_prefix instead * purple_str_has_suffix. Use g_str_has_suffix instead * purple_str_size_to_units. Use g_format_size() instead. + * purple_str_to_date_time + * purple_str_to_time * purple_time_build. Use g_date_time_new* instead. * purple_time_format. Use g_date_time_format instead. * purple_timeout_*. Use g_timeout_* or g_idle_* instead.
--- a/libpurple/tests/test_util.c Thu Jan 13 23:13:13 2022 -0600 +++ b/libpurple/tests/test_util.c Thu Jan 13 23:47:42 2022 -0600 @@ -136,66 +136,6 @@ } /****************************************************************************** - * str_to_time tests - *****************************************************************************/ -static void -test_util_str_to_time(void) { - struct tm tm; - glong tz_off; - const gchar *rest; - time_t timestamp; - - g_assert_cmpint(377182200, ==, purple_str_to_time("19811214T12:50:00", TRUE, NULL, NULL, NULL)); - g_assert_cmpint(1175919261, ==, purple_str_to_time("20070407T04:14:21", TRUE, NULL, NULL, NULL)); - g_assert_cmpint(1282941722, ==, purple_str_to_time("2010-08-27.204202", TRUE, NULL, NULL, NULL)); - - timestamp = purple_str_to_time("2010-08-27.134202-0700PDT", FALSE, &tm, &tz_off, &rest); - g_assert_cmpint(1282941722, ==, timestamp); - g_assert_cmpint((-7 * 60 * 60), ==, tz_off); - g_assert_cmpstr("PDT", ==, rest); -} - -/****************************************************************************** - * str_to_date_time tests - *****************************************************************************/ -static void -test_util_str_to_date_time(void) -{ - GDateTime *dt; - - dt = purple_str_to_date_time("19811214T12:50:00", TRUE); - g_assert_cmpint(377182200, ==, g_date_time_to_unix(dt)); - g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt)); - g_date_time_unref(dt); - - dt = purple_str_to_date_time("20070407T04:14:21.1234", TRUE); - g_assert_cmpint(1175919261, ==, g_date_time_to_unix(dt)); - g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt)); - g_assert_cmpint(123400, ==, g_date_time_get_microsecond(dt)); - g_date_time_unref(dt); - - dt = purple_str_to_date_time("2010-08-27.204202", TRUE); - g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt)); - g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt)); - g_date_time_unref(dt); - - dt = purple_str_to_date_time("2010-08-27.204202.123456", TRUE); - g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt)); - g_assert_cmpint(0, ==, g_date_time_get_utc_offset(dt)); - g_assert_cmpint(123456, ==, g_date_time_get_microsecond(dt)); - g_date_time_unref(dt); - - dt = purple_str_to_date_time("2010-08-27.134202-0700PDT", FALSE); - g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt)); - g_assert_cmpint((-7LL * 60 * 60 * G_USEC_PER_SEC), ==, g_date_time_get_utc_offset(dt)); - - dt = purple_str_to_date_time("2010-08-27.134202.1234-0700PDT", FALSE); - g_assert_cmpint(1282941722, ==, g_date_time_to_unix(dt)); - g_assert_cmpint(123400, ==, g_date_time_get_microsecond(dt)); - g_assert_cmpint((-7LL * 60 * 60 * G_USEC_PER_SEC), ==, g_date_time_get_utc_offset(dt)); -} - -/****************************************************************************** * UTF8 tests *****************************************************************************/ typedef struct { @@ -310,12 +250,6 @@ g_test_add_func("/util/email/is invalid", test_util_email_is_invalid); - g_test_add_func("/util/str to time", - test_util_str_to_time); - - g_test_add_func("/util/str to date time", - test_util_str_to_date_time); - g_test_add_func("/util/utf8/strip unprintables", test_util_utf8_strip_unprintables);
--- a/libpurple/util.c Thu Jan 13 23:13:13 2022 -0600 +++ b/libpurple/util.c Thu Jan 13 23:47:42 2022 -0600 @@ -87,354 +87,6 @@ return purple_utf8_strftime("%c", tm); } -/* originally taken from GLib trunk 1-6-11 */ -/* originally licensed as LGPL 2+ */ -static time_t -mktime_utc(struct tm *tm) -{ - time_t retval; - -#ifndef HAVE_TIMEGM - static const gint days_before[] = - { - 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 - }; -#endif - -#ifndef HAVE_TIMEGM - if (tm->tm_mon < 0 || tm->tm_mon > 11) - return (time_t) -1; - - retval = (tm->tm_year - 70) * 365; - retval += (tm->tm_year - 68) / 4; - retval += days_before[tm->tm_mon] + tm->tm_mday - 1; - - if (tm->tm_year % 4 == 0 && tm->tm_mon < 2) - retval -= 1; - - retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec; -#else - retval = timegm (tm); -#endif /* !HAVE_TIMEGM */ - - return retval; -} - -time_t -purple_str_to_time(const char *timestamp, gboolean utc, - struct tm *tm, long *tz_off, const char **rest) -{ - struct tm t; - const gchar *str; - gint year = 0; - long tzoff = PURPLE_NO_TZ_OFF; - time_t retval; - gboolean mktime_with_utc = FALSE; - - if (rest != NULL) - *rest = NULL; - - g_return_val_if_fail(timestamp != NULL, 0); - - memset(&t, 0, sizeof(struct tm)); - - str = timestamp; - - /* Strip leading whitespace */ - while (g_ascii_isspace(*str)) - str++; - - if (*str == '\0') { - if (rest != NULL) { - *rest = str; - } - - return 0; - } - - if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') { - if (rest != NULL && *str != '\0') - *rest = str; - - return 0; - } - - /* 4 digit year */ - if (sscanf(str, "%04d", &year) && year >= 1900) { - str += 4; - - if (*str == '-' || *str == '/') - str++; - - t.tm_year = year - 1900; - } - - /* 2 digit month */ - if (!sscanf(str, "%02d", &t.tm_mon)) { - if (rest != NULL && *str != '\0') - *rest = str; - - return 0; - } - - str += 2; - t.tm_mon -= 1; - - if (*str == '-' || *str == '/') - str++; - - /* 2 digit day */ - if (!sscanf(str, "%02d", &t.tm_mday)) { - if (rest != NULL && *str != '\0') - *rest = str; - - return 0; - } - - str += 2; - - /* Grab the year off the end if there's still stuff */ - if (*str == '/' || *str == '-') { - /* But make sure we don't read the year twice */ - if (year >= 1900) { - if (rest != NULL && *str != '\0') - *rest = str; - - return 0; - } - - str++; - - if (!sscanf(str, "%04d", &t.tm_year)) { - if (rest != NULL && *str != '\0') - *rest = str; - - return 0; - } - - t.tm_year -= 1900; - } else if (*str == 'T' || *str == '.') { - str++; - - /* Continue grabbing the hours/minutes/seconds */ - if ((sscanf(str, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 && - (str += 8)) || - (sscanf(str, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 && - (str += 6))) - { - gint sign, tzhrs, tzmins; - - if (*str == '.') { - /* Cut off those pesky micro-seconds */ - do { - str++; - } while (*str >= '0' && *str <= '9'); - } - - sign = (*str == '+') ? 1 : -1; - - /* Process the timezone */ - if (*str == '+' || *str == '-') { - str++; - - if (((sscanf(str, "%02d:%02d", &tzhrs, &tzmins) == 2 && (str += 5)) || - (sscanf(str, "%02d%02d", &tzhrs, &tzmins) == 2 && (str += 4)))) - { - mktime_with_utc = TRUE; - tzoff = tzhrs * 60 * 60 + tzmins * 60; - tzoff *= sign; - } - } else if (*str == 'Z') { - /* 'Z' = Zulu = UTC */ - str++; - mktime_with_utc = TRUE; - tzoff = 0; - } - - if (!mktime_with_utc) - { - /* No timezone specified. */ - - if (utc) { - mktime_with_utc = TRUE; - tzoff = 0; - } else { - /* Local Time */ - t.tm_isdst = -1; - } - } - } - } - - if (rest != NULL && *str != '\0') { - /* Strip trailing whitespace */ - while (g_ascii_isspace(*str)) - str++; - - if (*str != '\0') - *rest = str; - } - - if (mktime_with_utc) - retval = mktime_utc(&t); - else - retval = mktime(&t); - - if (tm != NULL) - *tm = t; - - if (tzoff != PURPLE_NO_TZ_OFF) - retval -= tzoff; - - if (tz_off != NULL) - *tz_off = tzoff; - - return retval; -} - -GDateTime * -purple_str_to_date_time(const char *timestamp, gboolean utc) -{ - const gchar *str; - gint year = 0; - gint month = 0; - gint day = 0; - gint hour = 0; - gint minute = 0; - gint seconds = 0; - gint microseconds = 0; - int chars = 0; - GTimeZone *tz = NULL; - GDateTime *retval; - - g_return_val_if_fail(timestamp != NULL, NULL); - - str = timestamp; - - /* Strip leading whitespace */ - while (g_ascii_isspace(*str)) - str++; - - if (*str == '\0') { - return NULL; - } - - if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') { - return NULL; - } - - /* 4 digit year */ - if (sscanf(str, "%04d", &year) && year > 0) { - str += 4; - - if (*str == '-' || *str == '/') - str++; - } - - /* 2 digit month */ - if (!sscanf(str, "%02d", &month)) { - return NULL; - } - - str += 2; - - if (*str == '-' || *str == '/') - str++; - - /* 2 digit day */ - if (!sscanf(str, "%02d", &day)) { - return NULL; - } - - str += 2; - - /* Grab the year off the end if there's still stuff */ - if (*str == '/' || *str == '-') { - /* But make sure we don't read the year twice */ - if (year > 0) { - return NULL; - } - - str++; - - if (!sscanf(str, "%04d", &year)) { - return NULL; - } - } else if (*str == 'T' || *str == '.') { - str++; - - /* Continue grabbing the hours/minutes/seconds */ - if ((sscanf(str, "%02d:%02d:%02d", &hour, &minute, &seconds) == 3 && - (str += 8)) || - (sscanf(str, "%02d%02d%02d", &hour, &minute, &seconds) == 3 && - (str += 6))) - { - if (*str == '.') { - str++; - if (sscanf(str, "%d%n", µseconds, &chars) == 1) { - str += chars; - } - } - - if (*str) { - const gchar *end = str; - if (*end == '+' || *end == '-') { - end++; - } - - while (isdigit(*end) || *end == ':') { - end++; - } - - if (str != end) { - /* Trim anything trailing a purely numeric time zone. */ - gchar *tzstr = g_strndup(str, end - str); - tz = g_time_zone_new_identifier(tzstr); - g_free(tzstr); - if (tz == NULL) { - tz = g_time_zone_new_identifier("UTC"); - } - } else { - /* Just try whatever is there. */ - tz = g_time_zone_new_identifier(str); - if (tz == NULL) { - tz = g_time_zone_new_identifier("UTC"); - } - } - } - } - } - - if (!tz) { - /* No timezone specified. */ - if (utc) { - tz = g_time_zone_new_utc(); - } else { - tz = g_time_zone_new_local(); - } - } - - retval = g_date_time_new(tz, year, month, day, hour, minute, - seconds + microseconds * pow(10, -chars)); - g_time_zone_unref(tz); - - return retval; -} - -gint purple_time_parse_month(const char *month_abbr) -{ - const char *months[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", - NULL}; - for (gint month = 0; months[month] != NULL; month++) { - if (purple_strequal(month_abbr, months[month])) { - return month + 1; - } - } - return 0; -} - /************************************************************************** * Path/Filename Functions **************************************************************************/
--- a/libpurple/util.h Thu Jan 13 23:13:13 2022 -0600 +++ b/libpurple/util.h Thu Jan 13 23:47:42 2022 -0600 @@ -139,55 +139,6 @@ */ #define PURPLE_NO_TZ_OFF -500000 -/** - * purple_str_to_time: - * @timestamp: The timestamp - * @utc: Assume UTC if no timezone specified - * @tm: If not %NULL, the caller can get a copy of the - * struct tm used to calculate the time_t return value. - * @tz_off: If not %NULL, the caller can get a copy of the - * timezone offset (from UTC) used to calculate the time_t - * return value. Note: Zero is a valid offset. As such, - * the value of the macro PURPLE_NO_TZ_OFF indicates no - * offset was specified (which means that the local - * timezone was used in the calculation). - * @rest: If not %NULL, the caller can get a pointer to the - * part of @timestamp left over after parsing is - * completed, if it's not the end of @timestamp. - * - * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns - * a time_t. - * - * Returns: A time_t. - */ -time_t purple_str_to_time(const char *timestamp, gboolean utc, - struct tm *tm, long *tz_off, const char **rest); - -/** - * purple_str_to_date_time: - * @timestamp: The timestamp - * @utc: Assume UTC if no timezone specified - * - * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns - * a GDateTime. - * - * Returns: (transfer full): A GDateTime. - */ -GDateTime *purple_str_to_date_time(const char *timestamp, gboolean utc); - -/** - * purple_time_parse_month: - * @month_abbr: The 3-letter month abbreviation - * - * Get month number suitable for GDateTime. If @month_abbr is unknown, - * returns 0. - * - * Returns: A month number or 0. - * - * Since: 3.0.0 - */ -gint purple_time_parse_month(const char *month_abbr); - /**************************************************************************/ /* Path/Filename Functions */ /**************************************************************************/