summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2019-11-21 15:36:18 +0100
committerBram Moolenaar <Bram@vim.org>2019-11-21 15:36:18 +0100
commit10455d43fef041309ce0613fa792c635dd71e3a8 (patch)
tree66b437223be119f47c759603193a6b1c6ff205c3 /src
parent9ae862ebba4a8962cb1c6811a2a46656fa672599 (diff)
patch 8.1.2326: cannot parse a date/time stringv8.1.2326
Problem: Cannot parse a date/time string. Solution: Add strptime(). (Stephen Wall, closes #)
Diffstat (limited to 'src')
-rwxr-xr-xsrc/auto/configure4
-rw-r--r--src/config.h.in1
-rw-r--r--src/configure.ac4
-rw-r--r--src/evalfunc.c42
-rw-r--r--src/os_unix.h11
-rw-r--r--src/testdir/test_functions.vim26
-rw-r--r--src/version.c2
7 files changed, 80 insertions, 10 deletions
diff --git a/src/auto/configure b/src/auto/configure
index d591ab5f1c..713bf9ea80 100755
--- a/src/auto/configure
+++ b/src/auto/configure
@@ -12572,8 +12572,8 @@ for ac_func in fchdir fchown fchmod fsync getcwd getpseudotty \
memset mkdtemp nanosleep opendir putenv qsort readlink select setenv \
getpgid setpgid setsid sigaltstack sigstack sigset sigsetjmp sigaction \
sigprocmask sigvec strcasecmp strerror strftime stricmp strncasecmp \
- strnicmp strpbrk strtol tgetent towlower towupper iswupper tzset \
- usleep utime utimes mblen ftruncate unsetenv posix_openpt
+ strnicmp strpbrk strptime strtol tgetent towlower towupper iswupper \
+ tzset usleep utime utimes mblen ftruncate unsetenv posix_openpt
do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/src/config.h.in b/src/config.h.in
index f9ae0bbf3d..e17f036c7c 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -206,6 +206,7 @@
#undef HAVE_STRNCASECMP
#undef HAVE_STRNICMP
#undef HAVE_STRPBRK
+#undef HAVE_STRPTIME
#undef HAVE_STRTOL
#undef HAVE_CANBERRA
#undef HAVE_ST_BLKSIZE
diff --git a/src/configure.ac b/src/configure.ac
index 4065817f34..6a10638e01 100644
--- a/src/configure.ac
+++ b/src/configure.ac
@@ -3744,8 +3744,8 @@ AC_CHECK_FUNCS(fchdir fchown fchmod fsync getcwd getpseudotty \
memset mkdtemp nanosleep opendir putenv qsort readlink select setenv \
getpgid setpgid setsid sigaltstack sigstack sigset sigsetjmp sigaction \
sigprocmask sigvec strcasecmp strerror strftime stricmp strncasecmp \
- strnicmp strpbrk strtol tgetent towlower towupper iswupper tzset \
- usleep utime utimes mblen ftruncate unsetenv posix_openpt)
+ strnicmp strpbrk strptime strtol tgetent towlower towupper iswupper \
+ tzset usleep utime utimes mblen ftruncate unsetenv posix_openpt)
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_FSEEKO
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 945c5f227f..63c4057ba7 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -20,7 +20,7 @@
# include <float.h>
#endif
-#ifdef MACOS_X
+#if defined(MACOS_X)
# include <time.h> // for time_t
#endif
@@ -237,6 +237,9 @@ static void f_stridx(typval_T *argvars, typval_T *rettv);
static void f_strlen(typval_T *argvars, typval_T *rettv);
static void f_strcharpart(typval_T *argvars, typval_T *rettv);
static void f_strpart(typval_T *argvars, typval_T *rettv);
+#ifdef HAVE_STRPTIME
+static void f_strptime(typval_T *argvars, typval_T *rettv);
+#endif
static void f_strridx(typval_T *argvars, typval_T *rettv);
static void f_strtrans(typval_T *argvars, typval_T *rettv);
static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
@@ -738,6 +741,9 @@ static funcentry_T global_functions[] =
{"string", 1, 1, FEARG_1, f_string},
{"strlen", 1, 1, FEARG_1, f_strlen},
{"strpart", 2, 3, FEARG_1, f_strpart},
+#ifdef HAVE_STRPTIME
+ {"strptime", 2, 2, FEARG_1, f_strptime},
+#endif
{"strridx", 2, 3, FEARG_1, f_strridx},
{"strtrans", 1, 1, FEARG_1, f_strtrans},
{"strwidth", 1, 1, FEARG_1, f_strwidth},
@@ -7412,6 +7418,40 @@ f_strpart(typval_T *argvars, typval_T *rettv)
rettv->vval.v_string = vim_strnsave(p + n, len);
}
+#ifdef HAVE_STRPTIME
+/*
+ * "strptime({format}, {timestring})" function
+ */
+ static void
+f_strptime(typval_T *argvars, typval_T *rettv)
+{
+ struct tm tmval;
+ char_u *fmt;
+ char_u *str;
+ vimconv_T conv;
+ char_u *enc;
+
+ vim_memset(&tmval, NUL, sizeof(tmval));
+ fmt = tv_get_string(&argvars[0]);
+ str = tv_get_string(&argvars[1]);
+
+ conv.vc_type = CONV_NONE;
+ enc = enc_locale();
+ convert_setup(&conv, p_enc, enc);
+ if (conv.vc_type != CONV_NONE)
+ fmt = string_convert(&conv, fmt, NULL);
+ if (fmt == NULL
+ || strptime((char *)str, (char *)fmt, &tmval) == NULL
+ || (rettv->vval.v_number = mktime(&tmval)) == -1)
+ rettv->vval.v_number = 0;
+
+ if (conv.vc_type != CONV_NONE)
+ vim_free(fmt);
+ convert_setup(&conv, NULL, NULL);
+ vim_free(enc);
+}
+#endif
+
/*
* "strridx()" function
*/
diff --git a/src/os_unix.h b/src/os_unix.h
index 618c0e9708..a7a0c841f1 100644
--- a/src/os_unix.h
+++ b/src/os_unix.h
@@ -127,9 +127,16 @@
# endif
#endif
+// on some systems time.h should not be included together with sys/time.h
#if !defined(HAVE_SYS_TIME_H) || defined(TIME_WITH_SYS_TIME)
-# include <time.h> /* on some systems time.h should not be
- included together with sys/time.h */
+// Needed for strptime()
+# ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE
+# endif
+# ifndef __USE_XOPEN
+# define __USE_XOPEN
+# endif
+# include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim
index e7b81daf4b..7284787834 100644
--- a/src/testdir/test_functions.vim
+++ b/src/testdir/test_functions.vim
@@ -181,9 +181,8 @@ func Test_str2nr()
endfunc
func Test_strftime()
- if !exists('*strftime')
- return
- endif
+ CheckFunction strftime
+
" Format of strftime() depends on system. We assume
" that basic formats tested here are available and
" identical on all systems which support strftime().
@@ -222,7 +221,28 @@ func Test_strftime()
else
unlet $TZ
endif
+endfunc
+
+func Test_strptime()
+ CheckFunction strptime
+
+ if exists('$TZ')
+ let tz = $TZ
+ endif
+ let $TZ = 'UTC'
+
+ call assert_equal(1484653763, strptime('%Y-%m-%d %X', '2017-01-17 11:49:23'))
+ call assert_fails('call strptime()', 'E119:')
+ call assert_fails('call strptime("xxx")', 'E119:')
+ call assert_equal(0, strptime("%Y", ''))
+ call assert_equal(0, strptime("%Y", "xxx"))
+
+ if exists('tz')
+ let $TZ = tz
+ else
+ unlet $TZ
+ endif
endfunc
func Test_resolve_unix()
diff --git a/src/version.c b/src/version.c
index c448e57fd6..23a1fc8cc7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -742,6 +742,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2326,
+/**/
2325,
/**/
2324,