aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_parse_timestamp.c
diff options
context:
space:
mode:
Diffstat (limited to 'libpatch_parse_timestamp.c')
-rw-r--r--libpatch_parse_timestamp.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/libpatch_parse_timestamp.c b/libpatch_parse_timestamp.c
new file mode 100644
index 0000000..aceb058
--- /dev/null
+++ b/libpatch_parse_timestamp.c
@@ -0,0 +1,47 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+const char *
+libpatch_parse_timestamp(const char *text, struct tm *time_out, const char **frac_out,
+ int *has_zone_out, enum libpatch_style style)
+{
+ const char *fmt, *r;
+ struct tm zone;
+
+ if (style == LIBPATCH_STYLE_COPIED) {
+ fmt = "%a %b %e %T %Y";
+ } else if (style == LIBPATCH_STYLE_UNIFIED) {
+ fmt = "%Y-%m-%d %H:%M:%S";
+ } else {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ text = strptime(text, fmt, time_out);
+ if (!text)
+ return NULL;
+
+ if (text[0] == '.') {
+ *frac_out = ++text;
+ while (isdigit(*text))
+ text++;
+ } else {
+ *frac_out = NULL;
+ }
+
+ time_out->tm_isdst = -1;
+ time_out->tm_zone = NULL;
+
+ r = strptime(text, " %z", &zone);
+ if (r) {
+ text = r;
+ *has_zone_out = 1;
+ time_out->tm_gmtoff = zone.tm_gmtoff;
+ } else {
+ *has_zone_out = 0;
+ time_out->tm_gmtoff = 0;
+ }
+
+ return text;
+}