diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2024-01-05 19:04:27 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2024-01-05 19:09:19 +0100 |
commit | f6ba789c3d33bdc1bcc6c00e9660ffbd9f2ba514 (patch) | |
tree | 3b1a78e26a482a306df8515d8a4385a095221037 /src | |
parent | e88882f0ae031c2c3400a517b751c53a66b1c79f (diff) | |
download | weechat-f6ba789c3d33bdc1bcc6c00e9660ffbd9f2ba514.zip |
api: add function util_parse_time (issue #649)
Diffstat (limited to 'src')
-rw-r--r-- | src/core/wee-util.c | 237 | ||||
-rw-r--r-- | src/core/wee-util.h | 1 | ||||
-rw-r--r-- | src/plugins/plugin.c | 1 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 5 |
4 files changed, 243 insertions, 1 deletions
diff --git a/src/core/wee-util.c b/src/core/wee-util.c index b885a9e00..abd149ae1 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -237,6 +237,243 @@ util_strftimeval (char *string, int max, const char *format, struct timeval *tv) } /* + * Parses a date/time string, which can be one of these formats: + * "2024-01-04" -> date at midnight + * "2024-01-04T22:01:02" -> ISO 8601, local time + * "2024-01-04T22:01:02.123" -> ISO 8601, local time, with milliseconds + * "2024-01-04T22:01:02.123456" -> ISO 8601, local time, with microseconds + * "2024-01-04T21:01:02Z" -> ISO 8601, UTC + * "2024-01-04T21:01:02.123Z" -> ISO 8601, UTC, with milliseconds + * "2024-01-04T21:01:02.123456Z" -> ISO 8601, UTC, with microseconds + * "22:01:02" -> current date, local time + * "22:01:02.123" -> current date, local time with milliseconds + * "22.01:02.123456" -> current date, local time with microseconds + * "21:01:02Z" -> current date, UTC + * "21:01:02.123Z" -> current date, UTC, with milliseconds + * "21.01:02.123456Z" -> current date, UTC, with microseconds + * "1704402062" -> timestamp date + * "1704402062.123" -> timestamp date, with milliseconds + * "1704402062,123" -> timestamp date, with milliseconds + * "1704402062.123456" -> timestamp date, with microseconds + * "1704402062,123456" -> timestamp date, with microseconds + * + * Returns: + * 1: OK + * 0: error + */ + +int +util_parse_time (const char *datetime, struct timeval *tv) +{ + char *string, *pos, *pos2, str_usec[16], *error, str_date[128]; + struct tm tm_date, tm_date_gm, tm_date_local, *local_time; + time_t time_now, time_gm, time_local; + long value; + int rc, length, use_local_time, timezone_offset, offset_factor, hour, min; + + if (!datetime || !datetime[0] || !tv) + return 0; + + rc = 0; + + tv->tv_sec = 0; + tv->tv_usec = 0; + + use_local_time = 1; + timezone_offset = 0; + offset_factor = 1; + + string = strdup (datetime); + if (!string) + return 0; + + /* extract microseconds and remove them from string2 */ + pos = strchr (string, '.'); + if (!pos) + pos = strchr (string, ','); + if (pos) + { + pos2 = pos + 1; + while (isdigit ((unsigned char)pos2[0])) + { + pos2++; + } + length = pos2 - pos - 1; + if (length > 0) + { + if (length > 6) + length = 6; + memcpy (str_usec, pos + 1, length); + str_usec[length] = '\0'; + while (strlen (str_usec) < 6) + { + strcat (str_usec, "0"); + } + error = NULL; + value = strtol (str_usec, &error, 10); + if (error && !error[0]) + { + if (value < 0) + value = 0; + else if (value > 999999) + value = 999999; + tv->tv_usec = (int)value; + } + } + memmove (pos, pos2, strlen (pos2) + 1); + } + + /* extract timezone and remove it from string2 */ + pos = strchr (string, 'Z'); + if (pos) + { + pos[0] = '\0'; + use_local_time = 0; + timezone_offset = 0; + } + else + { + pos = strchr (string, 'T'); + if (pos) + { + pos2 = strchr (pos, '+'); + if (pos2) + { + pos2[0] = '\0'; + pos2++; + offset_factor = 1; + } + else + { + pos2 = strchr (pos, '-'); + if (pos2) + { + pos2[0] = '\0'; + pos2++; + offset_factor = -1; + } + } + if (pos2) + { + use_local_time = 0; + hour = 0; + min = 0; + if (isdigit ((unsigned char)pos2[0]) + && isdigit ((unsigned char)pos2[1])) + { + hour = ((pos2[0] - '0') * 10) + (pos2[1] - '0'); + pos2 += 2; + if (pos2[0] == ':') + pos2++; + if (isdigit ((unsigned char)pos2[0]) + && isdigit ((unsigned char)pos2[1])) + { + min = ((pos2[0] - '0') * 10) + (pos2[1] - '0'); + } + } + timezone_offset = offset_factor * ((hour * 3600) + (min * 60)); + } + } + } + + if (strchr (string, '-')) + { + if (strchr (string, ':')) + { + /* ISO 8601 format like: "2024-01-04T21:01:02.123Z" */ + /* initialize structure, because strptime does not do it */ + memset (&tm_date, 0, sizeof (struct tm)); + pos = strptime (string, "%Y-%m-%dT%H:%M:%S", &tm_date); + if (pos && (tm_date.tm_year > 0)) + { + if (use_local_time) + { + tv->tv_sec = mktime (&tm_date); + } + else + { + /* convert to UTC and add timezone_offset */ + time_now = mktime (&tm_date); + gmtime_r (&time_now, &tm_date_gm); + localtime_r (&time_now, &tm_date_local); + time_gm = mktime (&tm_date_gm); + time_local = mktime (&tm_date_local); + tv->tv_sec = mktime (&tm_date_local) + + (time_local - time_gm) + + timezone_offset; + } + rc = 1; + } + } + else + { + /* ISO 8601 format like: "2024-01-04" */ + /* initialize structure, because strptime does not do it */ + memset (&tm_date, 0, sizeof (struct tm)); + pos = strptime (string, "%Y-%m-%d", &tm_date); + if (pos && (tm_date.tm_year > 0)) + { + tv->tv_sec = mktime (&tm_date); + rc = 1; + } + } + } + else if (strchr (string, ':')) + { + /* hour format like: "21:01:02" */ + time_now = time (NULL); + local_time = localtime (&time_now); + strftime (str_date, sizeof (str_date), + "%Y-%m-%dT", local_time); + strcat (str_date, string); + /* initialize structure, because strptime does not do it */ + memset (&tm_date, 0, sizeof (struct tm)); + pos = strptime (str_date, "%Y-%m-%dT%H:%M:%S", &tm_date); + if (pos) + { + if (use_local_time) + { + tv->tv_sec = mktime (&tm_date); + } + else + { + /* convert to UTC and add timezone_offset */ + time_now = mktime (&tm_date); + gmtime_r (&time_now, &tm_date_gm); + localtime_r (&time_now, &tm_date_local); + time_gm = mktime (&tm_date_gm); + time_local = mktime (&tm_date_local); + tv->tv_sec = mktime (&tm_date_local) + + (time_local - time_gm) + + timezone_offset; + } + rc = 1; + } + } + else + { + /* timestamp format: "1704402062" */ + error = NULL; + value = strtol (string, &error, 10); + if (error && !error[0] && (value >= 0)) + { + tv->tv_sec = (time_t)value; + rc = 1; + } + } + + free (string); + + if (!rc) + { + tv->tv_sec = 0; + tv->tv_usec = 0; + } + + return rc; +} + +/* * Returns difference between two times. * * The following variables are set, if pointer is not NULL: diff --git a/src/core/wee-util.h b/src/core/wee-util.h index e67550c3f..75cdf6800 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -33,6 +33,7 @@ extern char *util_get_microseconds_string (long long diff); extern const char *util_get_time_string (const time_t *date); extern int util_strftimeval (char *string, int max, const char *format, struct timeval *tv); +extern int util_parse_time (const char *datetime, struct timeval *tv); extern void util_get_time_diff (time_t time1, time_t time2, time_t *total_seconds, int *days, int *hours, int *minutes, diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 1a7be2737..12edccb3f 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -695,6 +695,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv) new_plugin->util_timeval_add = &util_timeval_add; new_plugin->util_get_time_string = &util_get_time_string; new_plugin->util_strftimeval = &util_strftimeval; + new_plugin->util_parse_time = &util_parse_time; new_plugin->util_version_number = &util_version_number; new_plugin->list_new = &weelist_new; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 6647494e0..ad45ebe5c 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -68,7 +68,7 @@ struct timeval; * please change the date with current one; for a second change at same * date, increment the 01, otherwise please keep 01. */ -#define WEECHAT_PLUGIN_API_VERSION "20231226-01" +#define WEECHAT_PLUGIN_API_VERSION "20240105-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -439,6 +439,7 @@ struct t_weechat_plugin const char *(*util_get_time_string) (const time_t *date); int (*util_strftimeval) (char *string, int max, const char *format, struct timeval *tv); + int (*util_parse_time) (const char *datetime, struct timeval *tv); int (*util_version_number) (const char *version); /* sorted lists */ @@ -1499,6 +1500,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); (weechat_plugin->util_get_time_string)(__date) #define weechat_util_strftimeval(__string, __max, __format, __tv) \ (weechat_plugin->util_strftimeval)(__string, __max, __format, __tv) +#define weechat_util_parse_time(__datetime, __tv) \ + (weechat_plugin->util_parse_time)(__datetime, __tv) #define weechat_util_version_number(__version) \ (weechat_plugin->util_version_number)(__version) |