Skip to content

Commit

Permalink
unde beautiful hack and reimplement unix2datetime in LibGit2
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Nov 21, 2017
1 parent 3830f66 commit 264a35e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 18 additions & 6 deletions base/libgit2/signature.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,28 @@ function Base.convert(::Type{GitSignature}, sig::Signature)
return GitSignature(sig_ptr_ptr[])
end

# Work around needing to access Dates from Base,
# even though Dates is in the stdlib.
# This Ref is filled with the needed function
# when Dates is loaed
const unix2datetime = Ref{Any}()
function yearmonthday(days)
z = days + 306; h = 100z - 25; a = fld(h, 3652425); b = a - fld(a, 4)
y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153)
d = c - div(153m - 457, 5); return m > 12 ? (y + 1, m - 12, d) : (y, m, d)
end
lpad0(x) = lpad(x, 2, '0')

function unix2date(t)
UNIXEPOCH = 62135683200000
rata = UNIXEPOCH + round(Int64, Int64(1000) * t)
year, month, day = yearmonthday(fld(rata, 86400000))
secs = t % (24 * 60 * 60)
mins = div(secs, 60)
m, d = lpad0(month), lpad0(day)
h, mi, s = lpad0.(round.(Int, (div(mins, 60), mins % 60, secs % 60)))
return "$year-$m-$d $h:$mi:$s"
end

function Base.show(io::IO, sig::Signature)
print(io, "Name: ", sig.name, ", ")
print(io, "Email: ", sig.email, ", ")
print(io, "Time: ", unix2datetime[](sig.time + 60*sig.time_offset))
print(io, "Time: ", unix2date(sig.time + 60*sig.time_offset))
@printf(io, "%+03i:%02i", divrem(sig.time_offset, 60)...)
end

Expand Down
2 changes: 0 additions & 2 deletions stdlib/Dates/src/Dates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ include("io.jl")
include("parse.jl")
include("deprecated.jl")

Base.LibGit2.unix2datetime[] = unix2datetime

export Period, DatePeriod, TimePeriod,
Year, Month, Week, Day, Hour, Minute, Second, Millisecond,
Microsecond, Nanosecond,
Expand Down

0 comments on commit 264a35e

Please sign in to comment.