From fc849dd301d45646ad8a8765f319865178071d07 Mon Sep 17 00:00:00 2001 From: lambdalisue Date: Tue, 10 Nov 2015 01:34:41 +0900 Subject: [PATCH 1/7] DateTime: Add 'fastformat()' method which use strftime internally --- autoload/vital/__latest__/DateTime.vim | 16 ++++++++++++++ doc/vital-date_time.txt | 10 +++++++++ test/DateTime.vimspec | 30 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/autoload/vital/__latest__/DateTime.vim b/autoload/vital/__latest__/DateTime.vim index a5958d6e3..e9dc434f9 100644 --- a/autoload/vital/__latest__/DateTime.vim +++ b/autoload/vital/__latest__/DateTime.vim @@ -46,6 +46,7 @@ function! s:_vital_loaded(V) abort \ '_days': 0, \ '_seconds': 0, \ }) + let s:tz_default_offset = s:timezone().offset() endfunction function! s:_vital_depends() abort @@ -453,6 +454,21 @@ function! s:DateTime.format(format, ...) abort return result endfunction " @vimlint(EVL102, 0, l:locale) +function! s:DateTime.fastformat(format, ...) abort + let tz = self.timezone() + let ts = self.unix_time() + tz.offset() - s:tz_default_offset + let locale = get(a:000, 0, '') + let format = a:format =~ '%z' + \ ? substitute(a:format, '%z', tz.offset_string(), 'g') + \ : a:format + if empty(locale) + return strftime(format, ts) + else + let expr = printf('strftime(%s, %d)', string(format), ts) + return s:_with_locale(expr, locale) + endif +endfunction +" @vimlint(EVL102, 0, l:locale) function! s:DateTime.strftime(format, ...) abort let expr = printf('strftime(%s, %d)', string(a:format), self.unix_time()) return s:_with_locale(expr, a:0 ? a:1 : '') diff --git a/doc/vital-date_time.txt b/doc/vital-date_time.txt index 3171cdf6b..0d602eee1 100644 --- a/doc/vital-date_time.txt +++ b/doc/vital-date_time.txt @@ -191,6 +191,16 @@ DateTime.format({format} [, {locale}]) Returns the formatted string of this DateTime. See |Vital.DateTime-format| about {format}. + *Vital.DateTime-DateTime.fastformat()* +DateTime.fastformat({format} [, {locale}]) + A faster version of |Vital.DateTime-DateTime.format()| which use + |strftime| internally. It substitute '%z' in {format} into the + value returned from |Vital.DateTime-TimeZone.offset_string()|. + Other accepted {format} depends on your system so it is not + portable. + See https://gist.github.com/lambdalisue/e73bd722ca8840e82bd8 + for benchmarking. + *Vital.DateTime-DateTime.strftime()* DateTime.strftime({format} [, {locate}]) Same as "strftime({format}, dt.unix_time())". diff --git a/test/DateTime.vimspec b/test/DateTime.vimspec index a3f08eafd..170b6f4b3 100644 --- a/test/DateTime.vimspec +++ b/test/DateTime.vimspec @@ -670,6 +670,36 @@ Describe DateTime End + Describe .fastformat() + Before + " Use non default timezone + let tz = DT.timezone().offset_string() ==# '+0300' ? '+0500' : '+0300' + let dt = DT.from_date(2012, 1, 2, 3, 4, 5, tz) + End + + Context symbol "%z" with a timezone + It is same as "format('%z')" + Assert Equals(dt.fastformat('%z'), dt.format('%z')) + End + + It is differrent from "strftime('%z')" + Assert NotEquals(dt.fastformat('%z'), dt.strftime('%z')) + End + End + + Context with a timezone + It is same as "format('%Y-%m-%d %H:%M:%S')" + let ft = '%Y-%m-%d %H:%M:%S' + Assert Equals(dt.fastformat(ft), dt.format(ft)) + End + + It is differrent from "strftime('%z')" + let ft = '%Y-%m-%d %H:%M:%S' + Assert NotEquals(dt.fastformat(ft), dt.strftime(ft)) + End + End + End + Describe .strftime() It is same as "strftime({format}, dt.unix_time())" Assert Equals(dt.strftime('%c'), strftime('%c', dt.unix_time())) From 5e288031e8b34195310b4484b4b401553f285327 Mon Sep 17 00:00:00 2001 From: Alisue Date: Tue, 10 Nov 2015 01:48:11 +0900 Subject: [PATCH 2/7] DateTime: 'substitute' -> 'substitutes' in doc --- doc/vital-date_time.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/vital-date_time.txt b/doc/vital-date_time.txt index 0d602eee1..36b21dcd6 100644 --- a/doc/vital-date_time.txt +++ b/doc/vital-date_time.txt @@ -194,7 +194,7 @@ DateTime.format({format} [, {locale}]) *Vital.DateTime-DateTime.fastformat()* DateTime.fastformat({format} [, {locale}]) A faster version of |Vital.DateTime-DateTime.format()| which use - |strftime| internally. It substitute '%z' in {format} into the + |strftime| internally. It substitutes '%z' in {format} into the value returned from |Vital.DateTime-TimeZone.offset_string()|. Other accepted {format} depends on your system so it is not portable. From 8ba6fe8375c180ae1003d49333d59520343b6845 Mon Sep 17 00:00:00 2001 From: lambdalisue Date: Fri, 13 Nov 2015 01:12:13 +0900 Subject: [PATCH 3/7] DateTime: Replace stftime with fastformat --- autoload/vital/__latest__/DateTime.vim | 7 +------ doc/vital-date_time.txt | 16 ++++++---------- test/DateTime.vimspec | 21 +++------------------ 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/autoload/vital/__latest__/DateTime.vim b/autoload/vital/__latest__/DateTime.vim index e9dc434f9..98b2cbf48 100644 --- a/autoload/vital/__latest__/DateTime.vim +++ b/autoload/vital/__latest__/DateTime.vim @@ -454,7 +454,7 @@ function! s:DateTime.format(format, ...) abort return result endfunction " @vimlint(EVL102, 0, l:locale) -function! s:DateTime.fastformat(format, ...) abort +function! s:DateTime.strftime(format, ...) abort let tz = self.timezone() let ts = self.unix_time() + tz.offset() - s:tz_default_offset let locale = get(a:000, 0, '') @@ -468,11 +468,6 @@ function! s:DateTime.fastformat(format, ...) abort return s:_with_locale(expr, locale) endif endfunction -" @vimlint(EVL102, 0, l:locale) -function! s:DateTime.strftime(format, ...) abort - let expr = printf('strftime(%s, %d)', string(a:format), self.unix_time()) - return s:_with_locale(expr, a:0 ? a:1 : '') -endfunction function! s:DateTime.to_string() abort return self.format('%c') endfunction diff --git a/doc/vital-date_time.txt b/doc/vital-date_time.txt index 0d602eee1..6448bed0f 100644 --- a/doc/vital-date_time.txt +++ b/doc/vital-date_time.txt @@ -191,21 +191,17 @@ DateTime.format({format} [, {locale}]) Returns the formatted string of this DateTime. See |Vital.DateTime-format| about {format}. - *Vital.DateTime-DateTime.fastformat()* -DateTime.fastformat({format} [, {locale}]) - A faster version of |Vital.DateTime-DateTime.format()| which use - |strftime| internally. It substitute '%z' in {format} into the - value returned from |Vital.DateTime-TimeZone.offset_string()|. + *Vital.DateTime-DateTime.strftime()* +DateTime.strftime({format} [, {locale}]) + An alternative version of |Vital.DateTime-DateTime.format()| which + use |strftime| internally. + It substitute '%z' in {format} into the value returned from + |Vital.DateTime-TimeZone.offset_string()|. Other accepted {format} depends on your system so it is not portable. See https://gist.github.com/lambdalisue/e73bd722ca8840e82bd8 for benchmarking. - *Vital.DateTime-DateTime.strftime()* -DateTime.strftime({format} [, {locate}]) - Same as "strftime({format}, dt.unix_time())". - Note that timezone is always converted to current timezone. - DateTime.to_string() *Vital.DateTime-DateTime.to_string()* Same as "format('%c')". diff --git a/test/DateTime.vimspec b/test/DateTime.vimspec index 170b6f4b3..387a07fac 100644 --- a/test/DateTime.vimspec +++ b/test/DateTime.vimspec @@ -670,7 +670,7 @@ Describe DateTime End - Describe .fastformat() + Describe .strftime() Before " Use non default timezone let tz = DT.timezone().offset_string() ==# '+0300' ? '+0500' : '+0300' @@ -679,33 +679,18 @@ Describe DateTime Context symbol "%z" with a timezone It is same as "format('%z')" - Assert Equals(dt.fastformat('%z'), dt.format('%z')) - End - - It is differrent from "strftime('%z')" - Assert NotEquals(dt.fastformat('%z'), dt.strftime('%z')) + Assert Equals(dt.strftime('%z'), dt.format('%z')) End End Context with a timezone It is same as "format('%Y-%m-%d %H:%M:%S')" let ft = '%Y-%m-%d %H:%M:%S' - Assert Equals(dt.fastformat(ft), dt.format(ft)) - End - - It is differrent from "strftime('%z')" - let ft = '%Y-%m-%d %H:%M:%S' - Assert NotEquals(dt.fastformat(ft), dt.strftime(ft)) + Assert Equals(dt.strftime(ft), dt.format(ft)) End End End - Describe .strftime() - It is same as "strftime({format}, dt.unix_time())" - Assert Equals(dt.strftime('%c'), strftime('%c', dt.unix_time())) - End - End - Describe .to_string() It is same as "format('%c')" Assert Equals(dt.to_string(), dt.format('%c')) From 215ed1d7b1258b1ef2d53b6782f1760f192603a1 Mon Sep 17 00:00:00 2001 From: Alisue Date: Fri, 13 Nov 2015 01:21:05 +0900 Subject: [PATCH 4/7] DateTime: Use =~? instead of =~ --- autoload/vital/__latest__/DateTime.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/vital/__latest__/DateTime.vim b/autoload/vital/__latest__/DateTime.vim index 98b2cbf48..2ddff702c 100644 --- a/autoload/vital/__latest__/DateTime.vim +++ b/autoload/vital/__latest__/DateTime.vim @@ -458,7 +458,7 @@ function! s:DateTime.strftime(format, ...) abort let tz = self.timezone() let ts = self.unix_time() + tz.offset() - s:tz_default_offset let locale = get(a:000, 0, '') - let format = a:format =~ '%z' + let format = a:format =~? '%z' \ ? substitute(a:format, '%z', tz.offset_string(), 'g') \ : a:format if empty(locale) From 8f6c6082e8d83a80cc4da9b2c0d7b3214c0d6080 Mon Sep 17 00:00:00 2001 From: lambdalisue Date: Fri, 13 Nov 2015 01:27:51 +0900 Subject: [PATCH 5/7] DateTime: Remove unnecessary 'Context' from vimspec --- test/DateTime.vimspec | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/test/DateTime.vimspec b/test/DateTime.vimspec index 387a07fac..f86d14a41 100644 --- a/test/DateTime.vimspec +++ b/test/DateTime.vimspec @@ -677,17 +677,13 @@ Describe DateTime let dt = DT.from_date(2012, 1, 2, 3, 4, 5, tz) End - Context symbol "%z" with a timezone - It is same as "format('%z')" - Assert Equals(dt.strftime('%z'), dt.format('%z')) - End + It is same as "format('%z')" + Assert Equals(dt.strftime('%z'), dt.format('%z')) End - Context with a timezone - It is same as "format('%Y-%m-%d %H:%M:%S')" - let ft = '%Y-%m-%d %H:%M:%S' - Assert Equals(dt.strftime(ft), dt.format(ft)) - End + It is same as "format('%Y-%m-%d %H:%M:%S')" + let ft = '%Y-%m-%d %H:%M:%S' + Assert Equals(dt.strftime(ft), dt.format(ft)) End End From 9b3fe93f2103eba84819abbf788a2148d1126a1e Mon Sep 17 00:00:00 2001 From: lambdalisue Date: Fri, 13 Nov 2015 01:39:55 +0900 Subject: [PATCH 6/7] DateTime: Update doc of DateTime.strftime() --- doc/vital-date_time.txt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/doc/vital-date_time.txt b/doc/vital-date_time.txt index 6448bed0f..7b97b4a09 100644 --- a/doc/vital-date_time.txt +++ b/doc/vital-date_time.txt @@ -194,11 +194,15 @@ DateTime.format({format} [, {locale}]) *Vital.DateTime-DateTime.strftime()* DateTime.strftime({format} [, {locale}]) An alternative version of |Vital.DateTime-DateTime.format()| which - use |strftime| internally. + use |strftime| internally. While |Vital.DateTime-DateTime.format()| + parse {format}, it is really slow compared to using |strftime|. + However |Vital.DateTime-DateTime.format()| is required while accepted + {format} and a return value of |strftime| depends on the system. + That's why this function exists for user who need performance but the + accuracy. It substitute '%z' in {format} into the value returned from - |Vital.DateTime-TimeZone.offset_string()|. - Other accepted {format} depends on your system so it is not - portable. + |Vital.DateTime-TimeZone.offset_string()| while |strftime| always + return the system default timezone offset string. See https://gist.github.com/lambdalisue/e73bd722ca8840e82bd8 for benchmarking. From b3b4a782fd00d3c847d70c462ba1969018a3c37f Mon Sep 17 00:00:00 2001 From: lambdalisue Date: Fri, 13 Nov 2015 01:57:22 +0900 Subject: [PATCH 7/7] DateTime: Fix English and tags in doc --- doc/vital-date_time.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/vital-date_time.txt b/doc/vital-date_time.txt index 7b97b4a09..bb2a4142a 100644 --- a/doc/vital-date_time.txt +++ b/doc/vital-date_time.txt @@ -194,14 +194,14 @@ DateTime.format({format} [, {locale}]) *Vital.DateTime-DateTime.strftime()* DateTime.strftime({format} [, {locale}]) An alternative version of |Vital.DateTime-DateTime.format()| which - use |strftime| internally. While |Vital.DateTime-DateTime.format()| - parse {format}, it is really slow compared to using |strftime|. + use |strftime()| internally. While |Vital.DateTime-DateTime.format()| + parse {format}, it is really slow compared to using |strftime()|. However |Vital.DateTime-DateTime.format()| is required while accepted - {format} and a return value of |strftime| depends on the system. + {format} and a return value of |strftime()| depends on the system. That's why this function exists for user who need performance but the accuracy. - It substitute '%z' in {format} into the value returned from - |Vital.DateTime-TimeZone.offset_string()| while |strftime| always + It substitutes '%z' in {format} into the value returned from + |Vital.DateTime-TimeZone.offset_string()| while |strftime()| always return the system default timezone offset string. See https://gist.github.com/lambdalisue/e73bd722ca8840e82bd8 for benchmarking.