![]() ![]() ![]() ![]() |
![]() |
|
![]() |
||
![]() |
情報を小出しにしたいわけじゃないんだけど、なんとなく恥ずかしくて公開できない。俺メモで使っていて、他でも使えるかもしれない、そんな便利そうな関数を書く。
ちなみに今回の「日付を変更するための入力を求める関数 omemo#dateinput() 」はhowmのときに書いた関数の焼き直し。
動作イメージは、以下。
let format = '%Y-%m-%d' let time = omemo#datetime2int('2008-07-12', format) echo omemo#dateinput(format, time) " formatはstrftime()で使われるもの " omemo#datetime2int()は文字列をUnix timeに変換する関数 " omemo#dateinput()が今日の関数。 " 2008-07-01 +num (shift), yymmdd (set), . (today): " ↑みたいなで入力を求められる。 " +5と入力すると、5日後の文字列'2008-07-17'が返ってくる。 " 03と入力すると、日を3日にした文字列'2008-07-03'が返ってくる。 " 0803と入力すると、月を8月、日を3日にした文字列'2008-08-03'が返ってくる。 " 010803と入力すると、年を2001年、月を8月、日を3日にした文字列'2001-08-03'が返ってくる。 " .と入力すると、今日を表す文字列'2008-07-12'が返ってくる。
うん。というわけで、以下、関数。
function! omemo#dateinput(...) let format = exists('a:1') ? a:1 : '%Y-%m-%d' let time = exists('a:2') ? a:2 : localtime() let input = input(strftime(format, time) . \ ' +num (shift), yymmdd (set), . (today): ') if input == '' return '' endif let result = '' if input =~ '^\d\{1,6}$' let f = format let pats = ['^\(\d\)$', '^\(\d\d\)$', '^\(\d\)\(\d\d\)$', \ '^\(\d\d\)\(\d\d\)$', '^\(\d\)\(\d\d\)\(\d\d\)$', \ '^\(\d\d\)\(\d\d\)\(\d\d\)$'] let pat = get(filter(pats, 'input =~ v:val'), 0, '') if input =~ '^\d\{1,2}$' " set dd let d = matchlist(input, pat)[1] if index(range(1, 31), d + 0) == -1 | return | endif let f = format let f = substitute(f, '\C\V%d', printf('%02d', d + 0), 'g') elseif input =~ '^\d\{3,4}$' " set mmdd let [m, d] = matchlist(input, pat)[1:2] if index(range(1, 12), m + 0) == -1 | return | endif if index(range(1, 31), d + 0) == -1 | return | endif let f = format let f = substitute(f, '\C\V%m', printf('%02d', m + 0), 'g') let f = substitute(f, '\C\V%d', printf('%02d', d + 0), 'g') elseif input =~ '^\d\{5,6}$' " set yymmdd let [y, m, d] = matchlist(input, pat)[1:3] if index(range(1, 12), m + 0) == -1 | return | endif if index(range(1, 31), d + 0) == -1 | return | endif let y = y + 0 if 38 < y && y < 70 return elseif y >= 70 let y = y + 1900 elseif y <= 38 let y = y + 2000 endif let f = format let f = substitute(f, '\C\V%Y', printf('%04d', y + 0), 'g') let f = substitute(f, '\C\V%m', printf('%02d', m + 0), 'g') let f = substitute(f, '\C\V%d', printf('%02d', d + 0), 'g') endif let d = time elseif input =~ '^[+-]\d\{1,3}$' " shift +num let d = time let d = eval(input . ' * 86400') + d let f = format elseif input =~ '^\.$' " today . let d = localtime() let f = format endif return strftime(f, d) endfunction
面白みのない動作原理。最終的にはstrftime()を呼ぶ、そのためのフォーマットを適当に整形しますよ。ってそんな感じ。
Suyun2013/05/03 03:35I can't hear anthying over the sound of how awesome this article is.
ewieptcotdm2013/05/05 01:02WkDWnC <a href="http://viriblntrrze.com/">viriblntrrze</a>
paenpxylpy2013/05/05 13:50EwTj4X , [url=http://kiksgqpujflp.com/]kiksgqpujflp[/url], [link=http://lrsmelwgicbx.com/]lrsmelwgicbx[/link], http://unuagssfvsan.com/
tlccgroeefj2013/05/08 11:51ILoFes , [url=http://qushwgagodyr.com/]qushwgagodyr[/url], [link=http://nopkvhzykdme.com/]nopkvhzykdme[/link], http://ojtoaywtsfcb.com/
aziyuvx2019/10/15 04:27http://mewkid.net/buy-xalanta/ - Amoxicillin Online <a href="http://mewkid.net/buy-xalanta/">18</a> bah.ptrf.vim.g.hatena.ne.jp.dgu.rd http://mewkid.net/buy-xalanta/
ugejiaorelebo2019/10/15 04:57http://mewkid.net/buy-xalanta/ - Buy Amoxicillin <a href="http://mewkid.net/buy-xalanta/">Amoxicillin 500 Mg</a> ssc.dnbx.vim.g.hatena.ne.jp.wft.fx http://mewkid.net/buy-xalanta/