You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Datelike currently offers a set of with_*() methods to set the day/month/year, but the resulting date must be valid (for example calling with_month(13) doesn't translate to january of the next year). You can also add a Duration to a Datelike, but there's no good Duration value for year/month/day because of leap years, various month lengths, and DST.
I'd like to have methods add_days/month/years(i32) -> Self to easily add/subtract the corresponding number of time units. Might want to have add_hour/min/sec/etc() for consistency's sake, although adding a Duration works well in this case (unless we want to handle DST ?).
My usecase is deceptively simple: I write a report and want to know when the next day/week/month/year starts, after a given timestamp. For example I have 2018-11-14T12:34:56z as input and want 2018-12-14T00:00:00z as output (beginning of next month).
This is ugly but doable for "next year":
fnnext_year<T>(d:T) -> TwhereT:Datelike{
d.with_year(d.year()+1).unwrap().with_month(1).unwrap().with_day(1).unwrap().with_hour(0).unwrap().with_minute(0).unwrap().with_second(0).unwrap()// Thankfully I know that my input had a second granularity so I don't care about nanoseconds.}
But the implementation for next_month/week/day() is not going to be fun.
Incidentally, it with also be nice to have a with_year/month/day/etc() that panics instead of returning a result, for cases like this where the input is hardcoded and known to be correct. Or a with_ymd() and with_hmsn() ;)
The text was updated successfully, but these errors were encountered:
Datelike
currently offers a set ofwith_*()
methods to set the day/month/year, but the resulting date must be valid (for example callingwith_month(13)
doesn't translate to january of the next year). You can also add aDuration
to aDatelike
, but there's no goodDuration
value for year/month/day because of leap years, various month lengths, and DST.I'd like to have methods
add_days/month/years(i32) -> Self
to easily add/subtract the corresponding number of time units. Might want to haveadd_hour/min/sec/etc()
for consistency's sake, although adding aDuration
works well in this case (unless we want to handle DST ?).My usecase is deceptively simple: I write a report and want to know when the next day/week/month/year starts, after a given timestamp. For example I have
2018-11-14T12:34:56z
as input and want2018-12-14T00:00:00z
as output (beginning of next month).This is ugly but doable for "next year":
But the implementation for
next_month/week/day()
is not going to be fun.Incidentally, it with also be nice to have a
with_year/month/day/etc()
that panics instead of returning a result, for cases like this where the input is hardcoded and known to be correct. Or awith_ymd()
andwith_hmsn()
;)The text was updated successfully, but these errors were encountered: