-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.jl
72 lines (61 loc) · 1.85 KB
/
utils.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Dates
@inline time_now(::Type{DateTime}) = Dates.now(Dates.UTC)
@inline time_zero(::Type{DateTime}) = DateTime(0)
"""
Rounds a Date/Time to the nearest period relative to an optional origin.
# Examples
```jldoctest
julia> using Dates
julia> round_origin(DateTime(2019, 1, 1, 12, 30, 0), Hour(1), RoundUp)
"2019-01-01T13:00:00"
julia> origin = DateTime(2020, 1, 1, 12, 30, 0);
julia> round_origin(DateTime(2019, 1, 1, 12, 30, 0), Hour(1), RoundUp, origin=origin)
"2019-01-01T12:30:00"
```
"""
@inline function round_origin(
value::V,
period::P,
mode::Base.RoundingMode
;
origin=nothing
) where {V<:Union{Dates.AbstractDateTime,Dates.AbstractTime},P<:Dates.Period}
isnothing(origin) && return round(value, period, mode)
origin + round(value - origin, period, mode)
end
"""
Rounds a numeric value to the nearest bucket relative to an optional origin.
# Examples
```jldoctest
julia> round_origin(4.5, 1.0, RoundUp)
5.0
julia> origin = 0.5;
julia> round_origin(1.0, 1.0, RoundDown, origin=origin)
0.5
```
"""
@inline function round_origin(
value::V,
bucket_width::P,
mode::Base.RoundingMode
;
origin=nothing
) where {V<:Real,P<:Real}
isnothing(origin) && return round(value / bucket_width, mode) * bucket_width
origin + round((value - origin) / bucket_width, mode) * bucket_width
end
"""
Returns the number of nanoseconds since UNIX epoch of arbitrary AbstractTime type.
"""
@inline function nanos_since_epoch_zero(x::T) where {T<:Dates.AbstractTime}
Dates.value(Nanosecond(x - T(1970, 1, 1)))
end
# Function to recursively remove line number nodes
function _remove_line_nodes!(ex)
ex isa Expr || return ex
Base.remove_linenums!(ex)
foreach(_remove_line_nodes!, ex.args)
ex
end
# Function to print an expression without line number nodes
_print_expression(expr) = println(_remove_line_nodes!(deepcopy(expr)))