Skip to content

Commit

Permalink
add src/rounding.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Mar 11, 2024
1 parent 9fa65ae commit 9def8bd
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/rounding.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@



function round(::Type{DateTime}, dt::AbstractCFDateTime,r = RoundNearestTiesUp)
origin = _origin_period(dt)
t = dt.instant + (origin + DATETIME_OFFSET)

t_ms =
if 3+_exponent(t) > 0
t.duration * _factor(t) * 10^(3+_exponent(t))
else
t.duration * _factor(t) / 10^(-3-_exponent(t))
end

t_ms_rounded = round(Int64,t_ms,r)
return DateTime(UTInstant{Millisecond}(Dates.Millisecond(t_ms_rounded)))
end


function Base.floor(dt::DateTimeStandard,p::Period)
origintuple = _origintuple(dt)
origin = _origin_period(dt)
t = dt.instant + origin

tt = t - mod(t,p)

ttt = tt - origin

return DateTimeStandard{typeof(ttt), Val(origintuple)}(ttt)
end

function Base.floor(dt::DateTimeStandard,p::Dates.TimePeriod)
floor(dt,convert(Period,p))
end

0 comments on commit 9def8bd

Please sign in to comment.