11import Dates: Date, DateTime
2+ using DateTimes64: DateTime64, pydatetime_string, datetime_from_pystring
23
34""" NumPy array protocol type string (typestr) format
45
@@ -21,37 +22,7 @@ Base.codepoint(x::ASCIIChar) = UInt8(x)
2122Base. show (io:: IO , x:: ASCIIChar ) = print (io, Char (x))
2223Base. zero (:: Union{ASCIIChar,Type{ASCIIChar}} ) = ASCIIChar (Base. zero (UInt8))
2324
24-
25- using Dates: Period, TimeType, Date, DateTime, Dates
26- import Base.==
27- struct DateTime64{P} <: TimeType
28- i:: Int64
29- end
30- Base. convert (:: Type{Date} ,t:: DateTime64{P} ) where P = Date (1970 )+ P (t. i)
31- Base. convert (:: Type{DateTime} ,t:: DateTime64{P} ) where P = DateTime (1970 )+ P (t. i)
32- Base. show (io:: IO ,t:: DateTime64{P} ) where P = print (io," DateTime64[" ,P," ]: " ,string (DateTime (t)))
33- Base. isless (x:: DateTime64{P} , y:: DateTime64{P} ) where P = isless (x. i, y. i)
34- == (x:: DateTime64{P} , y:: DateTime64{P} ) where P = x. i == y. i
35- strpairs = [Dates. Year => " Y" , Dates. Month => " M" , Dates. Week => " W" , Dates. Day=> " D" ,
36- Dates. Hour => " h" , Dates. Minute => " m" , Dates. Second=> " s" , Dates. Millisecond => " ms" ,
37- Dates. Microsecond => " us" , Dates. Nanosecond => " ns" ]
38- const jlperiod = Dict {String,Any} ()
39- const pdt64string = Dict {Any, String} ()
40- for p in strpairs
41- jlperiod[p[2 ]] = p[1 ]
42- pdt64string[p[1 ]] = p[2 ]
43- end
44- Base. convert (:: Type{DateTime64{P}} , t:: Date ) where P = DateTime64 {P} (Dates. value (P (t- Date (1970 ))))
45- Base. convert (:: Type{DateTime64{P}} , t:: DateTime ) where P = DateTime64 {P} (Dates. value (P (t- DateTime (1970 ))))
46- Base. convert (:: Type{DateTime64{P}} , t:: DateTime64{Q} ) where {P,Q} = DateTime64 {P} (Dates. value (P (Q (t. i))))
47- Base. zero (t:: Union{DateTime64, Type{<:DateTime64}} ) = t (0 )
4825Base. zero (t:: Union{String, Type{String}} ) = " "
49- # Base.promote_rule(::Type{<:DateTime64{<:Dates.DatePeriod}}, ::Type{Date}) = Date
50- # Base.promote_rule(::Type{<:DateTime64{<:Dates.DatePeriod}}, ::Type{DateTime}) = DateTime
51- # Base.promote_rule(::Type{<:DateTime64{<:Dates.TimePeriod}}, ::Type{Date}) = DateTime
52- # Base.promote_rule(::Type{<:DateTime64{<:Dates.TimePeriod}}, ::Type{DateTime}) = DateTime
53-
54-
5526
5627typestr (t:: Type ) = string (' <' , ' V' , sizeof (t))
5728typestr (t:: Type{>:Missing} ) = typestr (Base. nonmissingtype (t))
@@ -63,7 +34,7 @@ typestr(t::Type{<:AbstractFloat}) = string('<', 'f', sizeof(t))
6334typestr (:: Type{MaxLengthString{N,UInt32}} ) where N = string (' <' , ' U' , N)
6435typestr (:: Type{MaxLengthString{N,UInt8}} ) where N = string (' <' , ' S' , N)
6536typestr (:: Type{<:Array} ) = " |O"
66- typestr (:: Type{<:DateTime64{P}} ) where P = " <M8[ $(pdt64string[P]) ] "
37+ typestr (t :: Type{<:DateTime64} ) = pydatetime_string (t)
6738typestr (:: Type{<:AbstractString} ) = " |O"
6839
6940const typestr_regex = r" ^([<|>])([tbiufcmMOSUV])(\d *)(\[\w +\] )?$"
@@ -107,7 +78,7 @@ function typestr(s::AbstractString, filterlist=nothing)
10778 end
10879 if tc == ' M' && ts == 8
10980 # We have a datetime64 value
110- return DateTime64{jlperiod[ String (typespec)[ 2 : end - 1 ]]}
81+ return datetime_from_pystring (s)
11182 end
11283 # convert typecode to Char and typesize to Int
11384 typemap[(tc,ts)]
0 commit comments