Skip to content

Commit

Permalink
Merge pull request #342 from lambdalisue/fix/datetime_improve_response
Browse files Browse the repository at this point in the history
DateTime: Fix 'strftime()' to follow timezone of the DateTime object
  • Loading branch information
thinca committed Nov 12, 2015
2 parents ff0a4e3 + b3b4a78 commit 02eefba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
15 changes: 13 additions & 2 deletions autoload/vital/__latest__/DateTime.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -454,8 +455,18 @@ function! s:DateTime.format(format, ...) abort
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 : '')
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
function! s:DateTime.to_string() abort
return self.format('%c')
Expand Down
16 changes: 13 additions & 3 deletions doc/vital-date_time.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,19 @@ DateTime.format({format} [, {locale}])
See |Vital.DateTime-format| about {format}.

*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.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()|.
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 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.

DateTime.to_string() *Vital.DateTime-DateTime.to_string()*
Same as "format('%c')".
Expand Down
15 changes: 13 additions & 2 deletions test/DateTime.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,19 @@ Describe DateTime
End

Describe .strftime()
It is same as "strftime({format}, dt.unix_time())"
Assert Equals(dt.strftime('%c'), strftime('%c', dt.unix_time()))
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

It is same as "format('%z')"
Assert Equals(dt.strftime('%z'), dt.format('%z'))
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

Expand Down

0 comments on commit 02eefba

Please sign in to comment.