Repository: JuliaLang/julia
File: stdlib/Dates/test/conversions.jl (Julia)

Code before:
    @test string(Dates.unix2datetime(1095379199.75)) == string("2004-09-16T23:59:59.750")
    @test string(Dates.unix2datetime(1095379200.00)) == string("2004-09-17T00:00:00")
    @test string(Dates.unix2datetime(1095379200.25)) == string("2004-09-17T00:00:00.250")
    @test string(Dates.unix2datetime(1095379200.50)) == string("2004-09-17T00:00:00.500")
    @test string(Dates.unix2datetime(1095379200.75)) == string("2004-09-17T00:00:00.750")
    @test string(Dates.unix2datetime(1095379201.00)) == string("2004-09-17T00:00:01")
    @test string(Dates.unix2datetime(1095379201.25)) == string("2004-09-17T00:00:01.250")
    @test string(Dates.unix2datetime(915148798.75)) == string("1998-12-31T23:59:58.750")
    @test string(Dates.unix2datetime(915148799.00)) == string("1998-12-31T23:59:59")
    @test string(Dates.unix2datetime(915148799.25)) == string("1998-12-31T23:59:59.250")
    @test string(Dates.unix2datetime(915148799.50)) == string("1998-12-31T23:59:59.500")
    @test string(Dates.unix2datetime(915148799.75)) == string("1998-12-31T23:59:59.750")
    @test string(Dates.unix2datetime(915148800.00)) == string("1999-01-01T00:00:00")
    @test string(Dates.unix2datetime(915148800.25)) == string("1999-01-01T00:00:00.250")
    @test string(Dates.unix2datetime(915148800.50)) == string("1999-01-01T00:00:00.500")
    @test string(Dates.unix2datetime(915148800.75)) == string("1999-01-01T00:00:00.750")
    @test string(Dates.unix2datetime(915148801.00)) == string("1999-01-01T00:00:01")
    @test string(Dates.unix2datetime(915148801.25)) == string("1999-01-01T00:00:01.250")
end

@testset "conversions with localtime=true" begin
    @test unix2datetime(datetime2unix(DateTime(2000, 1, 1), localtime=true), localtime=true) == DateTime(2000, 1, 1)
    @test julian2datetime(datetime2julian(DateTime(2000, 1, 1), localtime=true), localtime=true) == DateTime(2000, 1, 1)

    unix2localdatetime(x::Real) = DateTime(Libc.TmStruct(floor(x))) + Millisecond(round(Int, (x - floor(x)) * 1000))
    localunixdiff() = value(DateTime(Libc.TmStruct(0))) - Dates.UNIXEPOCH
    @test unix2datetime(1095379198.75, localtime=true) == unix2localdatetime(1095379198.75)
    @test datetime2unix(unix2datetime(1095379198.75, localtime=true), localtime=true) == 1095379198.75
    @test julian2datetime(1721119.5 - float(localunixdiff()/86400000), localtime=true) == DateTime(0, 3, 1)
    @test datetime2julian(julian2datetime(1721119.5, localtime=true), localtime=true) == 1721119.5
end

@testset "conversion to/from Rata Die" begin
    @test Date(Dates.rata2datetime(734869)) == Dates.Date(2013, 1, 1)
    @test Dates.datetime2rata(Dates.rata2datetime(734869)) == 734869
end

@testset "conversion to/from Julian calendar" begin
    # Tests from here: http://mysite.verizon.net/aesir_research/date/back.htm#JDN
    @test Dates.julian2datetime(1721119.5) == Dates.DateTime(0, 3, 1)
    @test Dates.julian2datetime(1721424.5) == Dates.DateTime(0, 12, 31)
    @test Dates.julian2datetime(1721425.5) == Dates.DateTime(1, 1, 1)
    @test Dates.julian2datetime(2299149.5) == Dates.DateTime(1582, 10, 4)
    @test Dates.julian2datetime(2415020.5) == Dates.DateTime(1900, 1, 1)
    @test Dates.julian2datetime(2415385.5) == Dates.DateTime(1901, 1, 1)
    @test Dates.julian2datetime(2440587.5) == Dates.DateTime(1970, 1, 1)
    @test Dates.julian2datetime(2444239.5) == Dates.DateTime(1980, 1, 1)
    @test Dates.julian2datetime(2452695.625) == Dates.DateTime(2003, 2, 25, 3)
    @test Dates.datetime2julian(Dates.DateTime(2013, 12, 3, 21)) == 2456630.375
end
@testset "now, today, and UTC" begin

Review comment (suggestion):
```suggestion
    localunixdiff() = Dates.value(DateTime(Libc.TmStruct(0))) - Dates.UNIXEPOCH
```

Code after:
    @test string(Dates.unix2datetime(1095379199.75)) == string("2004-09-16T23:59:59.750")
    @test string(Dates.unix2datetime(1095379200.00)) == string("2004-09-17T00:00:00")
    @test string(Dates.unix2datetime(1095379200.25)) == string("2004-09-17T00:00:00.250")
    @test string(Dates.unix2datetime(1095379200.50)) == string("2004-09-17T00:00:00.500")
    @test string(Dates.unix2datetime(1095379200.75)) == string("2004-09-17T00:00:00.750")
    @test string(Dates.unix2datetime(1095379201.00)) == string("2004-09-17T00:00:01")
    @test string(Dates.unix2datetime(1095379201.25)) == string("2004-09-17T00:00:01.250")
    @test string(Dates.unix2datetime(915148798.75)) == string("1998-12-31T23:59:58.750")
    @test string(Dates.unix2datetime(915148799.00)) == string("1998-12-31T23:59:59")
    @test string(Dates.unix2datetime(915148799.25)) == string("1998-12-31T23:59:59.250")
    @test string(Dates.unix2datetime(915148799.50)) == string("1998-12-31T23:59:59.500")
    @test string(Dates.unix2datetime(915148799.75)) == string("1998-12-31T23:59:59.750")
    @test string(Dates.unix2datetime(915148800.00)) == string("1999-01-01T00:00:00")
    @test string(Dates.unix2datetime(915148800.25)) == string("1999-01-01T00:00:00.250")
    @test string(Dates.unix2datetime(915148800.50)) == string("1999-01-01T00:00:00.500")
    @test string(Dates.unix2datetime(915148800.75)) == string("1999-01-01T00:00:00.750")
    @test string(Dates.unix2datetime(915148801.00)) == string("1999-01-01T00:00:01")
    @test string(Dates.unix2datetime(915148801.25)) == string("1999-01-01T00:00:01.250")
end

@testset "conversions with localtime=true" begin
    unix2localdatetime(x::Real) = DateTime(Libc.TmStruct(floor(x))) + Millisecond(round(Int, (x - floor(x)) * 1000))
    @test unix2datetime(1095379198.75, localtime=true) == unix2localdatetime(1095379198.75)
end

@testset "conversion to/from Rata Die" begin
    @test Date(Dates.rata2datetime(734869)) == Dates.Date(2013, 1, 1)
    @test Dates.datetime2rata(Dates.rata2datetime(734869)) == 734869
end

@testset "conversion to/from Julian calendar" begin
    # Tests from here: http://mysite.verizon.net/aesir_research/date/back.htm#JDN
    @test Dates.julian2datetime(1721119.5) == Dates.DateTime(0, 3, 1)
    @test Dates.julian2datetime(1721424.5) == Dates.DateTime(0, 12, 31)
    @test Dates.julian2datetime(1721425.5) == Dates.DateTime(1, 1, 1)
    @test Dates.julian2datetime(2299149.5) == Dates.DateTime(1582, 10, 4)
    @test Dates.julian2datetime(2415020.5) == Dates.DateTime(1900, 1, 1)
    @test Dates.julian2datetime(2415385.5) == Dates.DateTime(1901, 1, 1)
    @test Dates.julian2datetime(2440587.5) == Dates.DateTime(1970, 1, 1)
    @test Dates.julian2datetime(2444239.5) == Dates.DateTime(1980, 1, 1)
    @test Dates.julian2datetime(2452695.625) == Dates.DateTime(2003, 2, 25, 3)
    @test Dates.datetime2julian(Dates.DateTime(2013, 12, 3, 21)) == 2456630.375
end
@testset "now, today, and UTC" begin
    @test typeof(Dates.now()) <: Dates.DateTime
    @test typeof(Dates.today()) <: Dates.Date
    @test typeof(Dates.now(Dates.UTC)) <: Dates.DateTime

    if Sys.isapple()
        withenv("TZ" => "UTC") do
            a = Dates.now()