forked from JuliaPy/PythonCall.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeDelta64.jl
More file actions
84 lines (68 loc) · 2.19 KB
/
TimeDelta64.jl
File metadata and controls
84 lines (68 loc) · 2.19 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# type
"""
TimeDelta64(value, [unit])
Construct a `TimeDelta64` with the given `value` and [`unit`](@ref Unit).
The unit is stored as a run-time value. If the units in your code are known, using
[`InlineTimeDelta64{unit}`](@ref InlineTimeDelta64) may be preferable. The memory layout
is the same as for a `numpy.timedelta64`.
The value can be:
- An `Integer`, in which case the `unit` is required.
- `"NaT"` or `"NaN"` to make a not-a-time value.
"""
struct TimeDelta64 <: AbstractTimeDelta64
value::Int64
unit::Tuple{Unit,Cint}
function TimeDelta64(value::Integer, unit::UnitArg)
new(Int(value), unitpair(unit))
end
end
# accessors
unitpair(d::TimeDelta64) = d.unit
# constructors
# (outer value/unit constructor is unnecessary; inner constructor handles UnitArg)
function TimeDelta64(d::AbstractTimeDelta64, unit::UnitArg = defaultunit(d))
unit = unitpair(unit)
if unit == unitpair(d)
TimeDelta64(value(d), unit)
elseif isnan(d)
TimeDelta64(NAT, unit)
else
v, r = rescale(value(d), unitpair(d), unit)
iszero(r) || throw(InexactError(:convert, TimeDelta64, d))
TimeDelta64(v, unit)
end
end
function TimeDelta64(s::AbstractString, unit::UnitArg = defaultunit(s))
unit = unitpair(unit)
if s in NAT_STRINGS
TimeDelta64(NAT, unit)
else
error(
"Cannot construct TimeDelta64 from string '$s'. Only NaT variants are supported.",
)
end
end
function TimeDelta64(p::DatesPeriod, unit::UnitArg = defaultunit(p))
v, r = rescale(value(p), Unit(p), unit)
iszero(r) || throw(InexactError(:convert, TimeDelta64, p))
TimeDelta64(v, unit)
end
# convert
Base.convert(::Type{TimeDelta64}, p::TimeDelta64) = p
Base.convert(::Type{TimeDelta64}, p::Union{AbstractTimeDelta64,DatesPeriod}) =
TimeDelta64(p)
# show
function Base.show(io::IO, d::TimeDelta64)
if get(io, :typeinfo, Any) == typeof(d)
showvalue(io, d)
else
show(io, typeof(d))
print(io, "(")
showvalue(io, d)
print(io, ", ")
show(io, unitparam(unitpair(d)))
print(io, ")")
end
nothing
end
Base.show(io::IO, ::MIME"text/plain", d::TimeDelta64) = show(io, d)