• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37649

12 Oct 2023 05:15AM UTC coverage: 87.56% (+0.9%) from 86.678%
#37649

push

local

web-flow
Reinstate load-time Pkg.precompile (#51672)

5 of 5 new or added lines in 1 file covered. (100.0%)

72441 of 82733 relevant lines covered (87.56%)

12848798.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.15
/stdlib/Dates/src/conversions.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
# Conversion/Promotion
4

5
"""
6
    Date(dt::DateTime) -> Date
7

8
Convert a `DateTime` to a `Date`. The hour, minute, second, and millisecond parts of
9
the `DateTime` are truncated, so only the year, month and day parts are used in
10
construction.
11
"""
12
Date(dt::TimeType) = convert(Date, dt)
1,651✔
13

14
"""
15
    DateTime(dt::Date) -> DateTime
16

17
Convert a `Date` to a `DateTime`. The hour, minute, second, and millisecond parts of
18
the new `DateTime` are assumed to be zero.
19
"""
20
DateTime(dt::TimeType) = convert(DateTime, dt)
3,039✔
21

22
"""
23
    Time(dt::DateTime) -> Time
24

25
Convert a `DateTime` to a `Time`. The hour, minute, second, and millisecond parts of
26
the `DateTime` are used to create the new `Time`. Microsecond and nanoseconds are zero by default.
27
"""
28
Time(dt::DateTime) = convert(Time, dt)
×
29

30
Base.convert(::Type{DateTime}, dt::Date) = DateTime(UTM(value(dt) * 86400000))
3,053✔
31
Base.convert(::Type{Date}, dt::DateTime) = Date(UTD(days(dt)))
1,638✔
32
Base.convert(::Type{Time}, dt::DateTime) = Time(Nanosecond((value(dt) % 86400000) * 1000000))
×
33

34
Base.convert(::Type{DateTime},x::Millisecond)  = DateTime(Dates.UTInstant(x))  # Converts Rata Die milliseconds to a DateTime
×
35
Base.convert(::Type{Millisecond},dt::DateTime) = Millisecond(value(dt))        # Converts DateTime to Rata Die milliseconds
×
36
Base.convert(::Type{Date},x::Day)  = Date(Dates.UTInstant(x))  # Converts Rata Die days to a Date
×
37
Base.convert(::Type{Day},dt::Date) = Day(value(dt))            # Converts Date to Rata Die days
×
38

39
### External Conversions
40
const UNIXEPOCH = value(DateTime(1970)) #Rata Die milliseconds for 1970-01-01T00:00:00
41

42
"""
43
    unix2datetime(x) -> DateTime
44

45
Take the number of seconds since unix epoch `1970-01-01T00:00:00` and convert to the
46
corresponding `DateTime`.
47
"""
48
function unix2datetime(x)
3✔
49
    # Rounding should match `now` below
50
    rata = UNIXEPOCH + trunc(Int64, Int64(1000) * x)
3✔
51
    return DateTime(UTM(rata))
3✔
52
end
53

54
"""
55
    datetime2unix(dt::DateTime) -> Float64
56

57
Take the given `DateTime` and return the number of seconds
58
since the unix epoch `1970-01-01T00:00:00` as a [`Float64`](@ref).
59
"""
60
datetime2unix(dt::DateTime) = (value(dt) - UNIXEPOCH) / 1000.0
×
61

62
"""
63
    now() -> DateTime
64

65
Return a `DateTime` corresponding to the user's system time including the system timezone
66
locale.
67
"""
68
function now()
493✔
69
    tv = Libc.TimeVal()
493✔
70
    tm = Libc.TmStruct(tv.sec)
493✔
71
    return DateTime(tm.year + 1900, tm.month + 1, tm.mday, tm.hour, tm.min, tm.sec, div(tv.usec, 1000))
493✔
72
end
73

74
"""
75
    today() -> Date
76

77
Return the date portion of `now()`.
78
"""
79
today() = Date(now())
×
80

81
"""
82
    now(::Type{UTC}) -> DateTime
83

84
Return a `DateTime` corresponding to the user's system time as UTC/GMT.
85
For other time zones, see the TimeZones.jl package.
86

87
# Example
88
```julia
89
julia> now(UTC)
90
2023-01-04T10:52:24.864
91
```
92
"""
93
now(::Type{UTC}) = unix2datetime(time())
3✔
94

95
"""
96
    rata2datetime(days) -> DateTime
97

98
Take the number of Rata Die days since epoch `0000-12-31T00:00:00` and return the
99
corresponding `DateTime`.
100
"""
101
rata2datetime(days) = DateTime(yearmonthday(days)...)
×
102

103
"""
104
    datetime2rata(dt::TimeType) -> Int64
105

106
Return the number of Rata Die days since epoch from the given `Date` or `DateTime`.
107
"""
108
datetime2rata(dt::TimeType) = days(dt)
×
109

110
# Julian conversions
111
const JULIANEPOCH = value(DateTime(-4713, 11, 24, 12))
112

113
"""
114
    julian2datetime(julian_days) -> DateTime
115

116
Take the number of Julian calendar days since epoch `-4713-11-24T12:00:00` and return the
117
corresponding `DateTime`.
118
"""
119
function julian2datetime(f)
×
120
    rata = JULIANEPOCH + round(Int64, Int64(86400000) * f)
×
121
    return DateTime(UTM(rata))
×
122
end
123

124
"""
125
    datetime2julian(dt::DateTime) -> Float64
126

127
Take the given `DateTime` and return the number of Julian calendar days since the julian
128
epoch `-4713-11-24T12:00:00` as a [`Float64`](@ref).
129
"""
130
datetime2julian(dt::DateTime) = (value(dt) - JULIANEPOCH) / 86400000.0
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc