LoginSignup
0
1

More than 5 years have passed since last update.

Mac で Julia #8 Dates

Last updated at Posted at 2018-08-28

前回に引き続きJuliaについてです。

ドキュメントに沿って試していきます。

よく使うのを試します。

はじめに

julia> using Date
ERROR: ArgumentError: Package Date not found in current path:
- Run `Pkg.add("Date")` to install the Date package.

Stacktrace:
 [1] require(::Module, ::Symbol) at ./loading.jl:817

julia> using Dates

DateじゃなくてDatesなんですね。

julia> Dates.DateTime(2013)
2013-01-01T00:00:00

julia> DateTime(2013)
2013-01-01T00:00:00

julia> DateTime(2013,7)
2013-07-01T00:00:00

julia> DateTime(2013,7,1,12,30,59)
2013-07-01T12:30:59

julia> DateTime(2013,7,1,12,30,59,10)
2013-07-01T12:30:59.01

julia> DateTime(2013,7,1,12,30,59,10,1)
ERROR: MethodError: no method matching DateTime(::Int64, ::Int64, ::Int64, ::Int64, ::Int64, ::Int64, ::Int64, ::Int64)

ミリ秒まで指定できました。

julia> Dates.DateTime(2020,2,29)
2020-02-29T00:00:00

julia> Dates.DateTime(2019,2,29)
ERROR: ArgumentError: Day: 29 out of range (1:28)

うるう年も大丈夫です。

DateFormat

julia> df = Dates.DateFormat("y-m-d")
dateformat"y-m-d"

julia> Date("2015-12-31",df)
2015-12-31

julia> Date("2015",df)
2015-01-01

julia> Date("2015-12-31 12:30",df)
ERROR: ArgumentError: Found extra characters at the end of date time string

Dates型に変換するフォーマット指定です。足りないのは大丈夫でしたが余分なのはダメなようです。

julia> df = Dates.DateFormat("y/m/d H:M:S.s")
dateformat"y/m/d H:M:S.s"

julia> Dates.DateTime("2015/12/31 12:00:10.1",df)
2015-12-31T12:00:10.1

ミリ秒まで指定するにはこんな感じですかね。

now()

julia> Dates.now()
2018-08-28T12:30:59.999

現時刻を表示できます。タイムゾーンはでないのかな?

julia> Dates.now(Dates.UTC)
2018-08-28T03:30:59.999

UTCのときはこうですね。Dates.JSTはエラーになりました。
基本LocalDateTimeなんですかね?

Dates.Time

julia> Dates.Time(12,30,10,50,50,50)
12:30:10.05005005

ナノ秒まで指定できました。

unix2datetime 、datetime2unix

julia> Dates.unix2datetime(0)
1970-01-01T00:00:00

julia> Dates.unix2datetime(1234567890)
2009-02-13T23:31:30

UNIX時間を変換します。

julia> Dates.datetime2unix(DateTime("2009-02-13T23:31:30"))
1.23456789e9

逆はdatetime2unixですね。

Durations/Comparisons

julia> d = (Dates.Date("2018-08-28") - Dates.Date("2020-01-01"))
-491 days

julia> typeof(d)
Day

引き算です。

julia> d1 = Dates.Date("2018-08-28")
2018-08-28

julia> d2 = Dates.Date("2020-01-01")
2020-01-01

julia> d1 == d2
false

julia> d1 != d2
true

julia> d1 > d2
false

julia> d1 < d2
true

普通ですねー

今日はこの辺で。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1