Skip to content

Commit e3ccc9d

Browse files
authored
Factor out DateTime64 type (#145)
* Factor out DateTime64 into dep * fix typestr parsing
1 parent 0ff255d commit e3ccc9d

File tree

3 files changed

+9
-34
lines changed

3 files changed

+9
-34
lines changed

Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Blosc = "a74b3585-a348-5f62-a45c-50e91977d574"
99
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
1010
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1111
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
12+
DateTimes64 = "b342263e-b350-472a-b1a9-8dfd21b51589"
1213
DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
1314
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
1415
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
@@ -24,6 +25,7 @@ AWSS3 = "0.10"
2425
Blosc = "0.5, 0.6, 0.7"
2526
CodecZlib = "0.6, 0.7"
2627
DataStructures = "0.17, 0.18"
28+
DateTimes64 = "1"
2729
DiskArrays = "0.4.2"
2830
HTTP = "^1.3"
2931
JSON = "0.21"

src/ZArray.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import JSON
22
import OffsetArrays: OffsetArray
33
import DiskArrays: AbstractDiskArray
44
import DiskArrays
5+
using DateTimes64: DateTime64
6+
using Dates: Day, Millisecond
57

68
"""
79
Number of tasks to use for async reading of chunks. Warning: setting this to very high values can lead to a large memory footprint.
@@ -372,8 +374,8 @@ filterfromtype(::Type{<:Union{MaxLengthString, Union{MaxLengthString, Missing}}}
372374
#Not all Array types can be mapped directly to a valid ZArray encoding.
373375
#Here we try to determine the correct element type
374376
to_zarrtype(::AbstractArray{T}) where T = T
375-
to_zarrtype(a::AbstractArray{<:Date}) = DateTime64{Dates.Day}
376-
to_zarrtype(a::AbstractArray{<:DateTime}) = DateTime64{Dates.Millisecond}
377+
to_zarrtype(a::AbstractArray{<:Date}) = DateTime64{Day}
378+
to_zarrtype(a::AbstractArray{<:DateTime}) = DateTime64{Millisecond}
377379

378380
function ZArray(a::AbstractArray, args...; kwargs...)
379381
z = zcreate(to_zarrtype(a), args..., size(a)...; kwargs...)

src/metadata.jl

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 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)
2122
Base.show(io::IO, x::ASCIIChar) = print(io, Char(x))
2223
Base.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)
4825
Base.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

5627
typestr(t::Type) = string('<', 'V', sizeof(t))
5728
typestr(t::Type{>:Missing}) = typestr(Base.nonmissingtype(t))
@@ -63,7 +34,7 @@ typestr(t::Type{<:AbstractFloat}) = string('<', 'f', sizeof(t))
6334
typestr(::Type{MaxLengthString{N,UInt32}}) where N = string('<', 'U', N)
6435
typestr(::Type{MaxLengthString{N,UInt8}}) where N = string('<', 'S', N)
6536
typestr(::Type{<:Array}) = "|O"
66-
typestr(::Type{<:DateTime64{P}}) where P = "<M8[$(pdt64string[P])]"
37+
typestr(t::Type{<:DateTime64}) = pydatetime_string(t)
6738
typestr(::Type{<:AbstractString}) = "|O"
6839

6940
const 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

Comments
 (0)