Skip to content
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

at(dt::DateTime, t::Time) and at(d::Date, t::Time) #24814

Open
scls19fr opened this issue Nov 28, 2017 · 1 comment
Open

at(dt::DateTime, t::Time) and at(d::Date, t::Time) #24814

scls19fr opened this issue Nov 28, 2017 · 1 comment
Labels
domain:dates Dates, times, and the Dates stdlib module stdlib Julia's standard library

Comments

@scls19fr
Copy link
Contributor

scls19fr commented Nov 28, 2017

Hello,

Julia now have a Time type thanks to @aviks issue #12140 and @quinnj PR #12274

but a function that returns DateTime at a given Time (from either a Date or a DateTime)
could be a nice feature to have.

Usage example :
with DateTime

julia> dt = DateTime(2010, 1, 3, 20, 30)
2010-01-03T20:30:00

julia> at(dt, Dates.Time(13, 30))
2010-01-03T13:30:00

with Date

julia> d = Date(2010, 1, 1)
2010-01-03
julia> at(dt, Dates.Time(13, 30))
2010-01-03T13:30:00

This is a bit out of the scope of this issue but creating a DateTime from Date and Time is not as easy as it should be

julia> dt = DateTime(2010, 1, 3, 20, 30)
2010-01-03T20:30:00

julia> d = Date(dt)
2010-01-03

julia> t = Dates.Time(dt)
20:30:00

julia> DateTime(d, t)
ERROR: MethodError: Cannot `convert` an object of type Base.Dates.Time to an object of type Int64
This may have arisen from a call to the constructor Int64(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] DateTime(::Date, ::Base.Dates.Time) at ./dates/types.jl:311

Kind regards

@fredrikekre fredrikekre added domain:dates Dates, times, and the Dates stdlib module stdlib Julia's standard library labels Nov 28, 2017
@scls19fr
Copy link
Contributor Author

scls19fr commented Nov 28, 2017

Here is a possible implementation

julia> DateTime(d::Date, t::Dates.Time) = d + t
DateTime (generic function with 1 method)

julia> at(d::Dates.TimeType, t::Dates.Time) = DateTime(Date(d), t)
at (generic function with 1 method)

julia> dt = Dates.DateTime(2010, 1, 3, 20, 30)
2010-01-03T20:30:00

julia> d = Date(dt)
2010-01-03

julia> t = Dates.Time(dt)
20:30:00

julia> t2 = Dates.Time(22, 30)
22:30:00

julia> println(at(d, t2))
2010-01-03T22:30:00

julia> println(at(dt, t2))
2010-01-03T22:30:00

I hope that (one day) function chaining issue will be fixed #5571
so it could be possible to do something like

d.at(t)

or

d |> at(t)

or (but not my prefered syntaxic sugar)

d |> at(_, t)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:dates Dates, times, and the Dates stdlib module stdlib Julia's standard library
Projects
None yet
Development

No branches or pull requests

2 participants