-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial work to add a Time type to Base.Dates #12274
Changes from 1 commit
b8b33f9
73eafe9
58338da
8881760
673d2b8
e383f20
6cd4ea5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,25 +16,26 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond | |
# AbstractString parsing (mainly for IO code) | ||
@eval $period(x::AbstractString) = $period(Base.parse(Int64,x)) | ||
# Period accessors | ||
typs = period in (:Microsecond, :Nanosecond) ? ["Time"] : (period in (:Hour, :Minute, :Second, :Millisecond) ? ["DateTime","Time"] : ["TimeType"]) | ||
description = typs == ["TimeType"] ? "`Date` or `DateTime`" : (typs == ["Time"] ? "`Time`" : "`Time` or `DateTime`") | ||
reference = period == :Week ? " For details see [`$accessor_str(::TimeType)`](:func:`$accessor_str`)." : "" | ||
typs = period in (:Microsecond, :Nanosecond) ? ["Time"] : | ||
period in (:Hour, :Minute, :Second, :Millisecond) ? ["Time", "DateTime"] : ["Date","DateTime"] | ||
reference = period == :Week ? " For details see [`$accessor_str(::Union{Date,DateTime})`](:func:`$accessor_str`)." : "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. old-style cross-reference format There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the new style? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for typ_str in typs | ||
@eval begin | ||
@doc """ | ||
$($period_str)(dt::$($typ_str)) -> $($period_str) | ||
|
||
The $($accessor_str) part of a $($description) as a `$($period_str)`.$($reference) | ||
The $($accessor_str) part of a $($typ_str) as a `$($period_str)`.$($reference) | ||
""" $period(dt::$(Symbol(typ_str))) = $period($(Symbol(accessor_str))(dt)) | ||
|
||
@doc """ | ||
$($period_str)(v) | ||
|
||
Construct a `$($period_str)` object with the given `v` value. Input must be | ||
losslessly convertible to an `Int64`. | ||
""" $period(v) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstrings seem a little strange here as the docstring specifically for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I've got them better sorted out now. |
||
end | ||
@eval begin | ||
@doc """ | ||
$($period_str)(v) | ||
|
||
Construct a `$($period_str)` object with the given `v` value. Input must be | ||
losslessly convertible to an `Int64`. | ||
""" $period(v) | ||
end | ||
end | ||
# Now we're safe to define Period-Number conversions | ||
# Anything an Int64 can convert to, a Period can convert to | ||
|
@@ -449,6 +450,8 @@ Base.promote_rule(::Type{Year}, ::Type{Month}) = Month | |
Base.isless{T<:OtherPeriod,S<:OtherPeriod}(x::T,y::S) = isless(promote(x,y)...) | ||
|
||
# truncating conversions to milliseconds and days: | ||
toms(c::Nanosecond) = div(value(c), 1000000) | ||
toms(c::Microsecond) = div(value(c), 1000) | ||
toms(c::Millisecond) = value(c) | ||
toms(c::Second) = 1000*value(c) | ||
toms(c::Minute) = 60000*value(c) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these magnitudes then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand your comment. These are generic fallbacks that will end up dispatching to the definitions above. This is just for the switched order of operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks wrong that
y - x = x - y
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, got it. Yes, these are magnitudes. Order doesn't matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth a comment then, in case anyone else gets confused looking at this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we already do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wat? why allow that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember if it was an explicit decision to allow or not. We can open another issue to remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in favor of removing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opened #20205