概要
nimの日付・時刻操作をコツコツと。
以下メモ
今何時?
import times
proc `$`(t:TimeInfo) : string = format(t, "yyyy/MM/dd HH:mm:ss")
block:
# 現在時刻を取得
let now: TimeInfo = getLocalTime(getTime())
# フォーマット化する
let nowStr: string = format(now, "yyyy/MM/dd HH:mm:ss")
echo "現在=",nowStr
# 文字列をパースし、時刻型に変換
let now2: TimeInfo = parse(nowStr,"yyyy/MM/dd HH:mm:ss")
echo now == now2
(stdout)
現在=2016/06/05 09:42:19
true
日付情報
import times
proc `$`(t:TimeInfo) : string = format(t, "yyyy/MM/dd HH:mm:ss")
block:
let time: TimeINfo = getLocalTime(getTime())
echo "現在 =",time
echo "second =",time.second
echo "minute =",time.minute
echo "hour =",time.hour
echo "monthday=",time.monthday
echo "month =",time.month
echo "month =",ord(time.month) + 1
echo "year =",time.year
echo "weekday =",time.weekday
echo "weekday =",ord(time.weekday) + 1
echo "yearday =",time.yearday
echo "isDST =",time.isDST
echo "tzname =",time.tzname
echo "timezone=",time.timezone
(stdout)
現在 =2016/06/05 09:42:20
second =20
minute =42
hour =9
monthday=5
month =June
month =6
year =2016
weekday =Sunday
weekday =7
yearday =156
isDST =false
tzname =JST
timezone=-32400
うるう年?
import times
proc `$`(t:TimeInfo) : string = format(t, "yyyy/MM/dd HH:mm:ss")
block:
echo getTime().getGMTime.year.isLeapYear
(stdout)
true
日付の加算・減算
import times
proc `$`(t:TimeInfo) : string = format(t, "yyyy/MM/dd HH:mm:ss")
block:
let today: TimeInfo = getLocalTime(getTime())
# TimeIntervalを生成する
let day: TimeInterval = initInterval(hours=24)
echo "明日 =",today + day
echo "昨日 =",today - day
# TimeIntervalを利用しないで、Rubyっぽい?かんじで計算
echo "1秒後 =",today + 1.seconds
echo "1分後 =",today + 1.minutes
echo "1時間後=",today + 1.hours
echo "1週間後=",today + 7.days
echo "1ヶ月後=",today + 1.months
echo "1年後 =",today + 1.years
(stdout)
明日 =2016/06/06 09:42:21
昨日 =2016/06/04 09:42:21
1秒後 =2016/06/05 09:42:22
1分後 =2016/06/05 09:43:21
1時間後=2016/06/05 10:42:21
1週間後=2016/06/12 09:42:21
1ヶ月後=2016/07/05 09:42:21
1年後 =2017/06/05 09:42:21