-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdatetime.jl
51 lines (42 loc) · 1.58 KB
/
datetime.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Constructors for the julia Date, Time and DateTime types.
if VERSION < v"0.4-"
using Dates
else
using Base.Dates
end
import Base.==
const MYSQL_DATE_FORMAT = Dates.DateFormat("yyyy-mm-dd")
const MYSQL_DATETIME_FORMAT = Dates.DateFormat("yyyy-mm-dd HH:MM:SS")
function Base.convert(::Type{Date}, datestr::AbstractString)
Date(datestr, MYSQL_DATE_FORMAT)
end
function Base.convert(::Type{DateTime}, dtimestr::AbstractString)
if !contains(dtimestr, " ")
dtimestr = "1970-01-01 " * dtimestr
end
DateTime(dtimestr, MYSQL_DATETIME_FORMAT)
end
function Base.convert(::Type{DateTime}, mtime::MYSQL_TIME)
if mtime.year == 0 || mtime.month == 0 || mtime.day == 0
DateTime(1970, 1, 1,
mtime.hour, mtime.minute, mtime.second)
else
DateTime(mtime.year, mtime.month, mtime.day,
mtime.hour, mtime.minute, mtime.second)
end
end
function Base.convert(::Type{Date}, mtime::MYSQL_TIME)
Date(mtime.year, mtime.month, mtime.day)
end
function Base.convert(::Type{MYSQL_TIME}, date::Date)
MYSQL_TIME(Dates.year(date), Dates.month(date), Dates.day(date), 0, 0, 0, 0, 0, 0)
end
function Base.convert(::Type{MYSQL_TIME}, dtime::DateTime)
if Dates.year(dtime) == 1970 && Dates.month(dtime) == 1 && Dates.day(dtime) == 1
MYSQL_TIME(0, 0, 0,
Dates.hour(dtime), Dates.minute(dtime), Dates.second(dtime), 0, 0, 0)
else
MYSQL_TIME(Dates.year(dtime), Dates.month(dtime), Dates.day(dtime),
Dates.hour(dtime), Dates.minute(dtime), Dates.second(dtime), 0, 0, 0)
end
end