digitalmars.D.learn - Adding days to std.datetime.Date
- Piotr Szturmaj (8/8) Apr 07 2011 Is it possible to add a particular number of days to a Date?
- Steven Schveighoffer (9/16) Apr 07 2011 Yes, use core.time.Duration.
- Piotr Szturmaj (4/23) Apr 07 2011 Well, I did find it few mins ago. But std.datetime's doc still states
- =?UTF-8?B?IlRow6lv?= Bueno" (3/12) Dec 02 2014 Is there any particular reason why std.datetime's "add" does not
- H. S. Teoh via Digitalmars-d-learn (14/31) Dec 02 2014 Sure it does:
- =?UTF-8?B?IlRow6lv?= Bueno" (7/41) Dec 02 2014 Indeed, thank you :)
- =?UTF-8?B?IlRow6lv?= Bueno" (5/9) Dec 02 2014 But still, why this method
- H. S. Teoh via Digitalmars-d-learn (5/15) Dec 02 2014 Hmm. Looks like an oversight. File a bug?
- Steven Schveighoffer (8/20) Dec 02 2014 Not an oversight.
- Steven Schveighoffer (7/13) Dec 02 2014 Aaaand to expand on this, since roll *does* support days, it's because
- =?UTF-8?B?IlRow6lv?= Bueno" (5/24) Dec 02 2014 Oh well, this explanation make so much sense !
- Jonathan M Davis via Digitalmars-d-learn (13/28) Dec 06 2014 A prime example of where to use it would be if you have a GUI with spinn...
- Jonathan M Davis via Digitalmars-d-learn (5/25) Dec 06 2014 Exactly. add wouldn't even exist if it weren't for the fact that Duratio...
Is it possible to add a particular number of days to a Date? I have number of days since 1 Jan 2000 and I want to convert it to Date: int days = read!int; // number of days since 1 Jan 2000 Date x = Date(2000, 1, 1); x.add!"days"(days); Unfortunately add() does not support adding days. Will it be possible in the future or is there another approach? Thanks
Apr 07 2011
On Thu, 07 Apr 2011 15:07:02 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:Is it possible to add a particular number of days to a Date? I have number of days since 1 Jan 2000 and I want to convert it to Date: int days = read!int; // number of days since 1 Jan 2000 Date x = Date(2000, 1, 1); x.add!"days"(days); Unfortunately add() does not support adding days. Will it be possible in the future or is there another approach?Yes, use core.time.Duration. Duration was moved to core so it could be used in core functions, like Thread.sleep. so x += dur!"days"(days); See: http://www.digitalmars.com/d/2.0/phobos/core_time.html#dur -Steve
Apr 07 2011
Steven Schveighoffer wrote:On Thu, 07 Apr 2011 15:07:02 -0400, Piotr Szturmaj <bncrbme jadamspam.pl> wrote:Well, I did find it few mins ago. But std.datetime's doc still states that it provide types to represent durations of time.Is it possible to add a particular number of days to a Date? I have number of days since 1 Jan 2000 and I want to convert it to Date: int days = read!int; // number of days since 1 Jan 2000 Date x = Date(2000, 1, 1); x.add!"days"(days); Unfortunately add() does not support adding days. Will it be possible in the future or is there another approach?Yes, use core.time.Duration. Duration was moved to core so it could be used in core functions, like Thread.sleep. so x += dur!"days"(days); See: http://www.digitalmars.com/d/2.0/phobos/core_time.html#dur-SteveThanks!
Apr 07 2011
On Thursday, 7 April 2011 at 19:10:40 UTC, Piotr Szturmaj wrote:Is it possible to add a particular number of days to a Date? I have number of days since 1 Jan 2000 and I want to convert it to Date: int days = read!int; // number of days since 1 Jan 2000 Date x = Date(2000, 1, 1); x.add!"days"(days); Unfortunately add() does not support adding days. Will it be possible in the future or is there another approach? ThanksIs there any particular reason why std.datetime's "add" does not support days ?
Dec 02 2014
On Tue, Dec 02, 2014 at 05:21:27PM +0000, via Digitalmars-d-learn wrote:On Thursday, 7 April 2011 at 19:10:40 UTC, Piotr Szturmaj wrote:Sure it does: import std.datetime; import std.stdio; void main() { auto date = Date(2000, 1, 1); date += 10.days; writeln(date); } Output: 2000-Jan-11 T -- Heads I win, tails you lose.Is it possible to add a particular number of days to a Date? I have number of days since 1 Jan 2000 and I want to convert it to Date: int days = read!int; // number of days since 1 Jan 2000 Date x = Date(2000, 1, 1); x.add!"days"(days); Unfortunately add() does not support adding days. Will it be possible in the future or is there another approach? ThanksIs there any particular reason why std.datetime's "add" does not support days ?
Dec 02 2014
On Tuesday, 2 December 2014 at 18:00:14 UTC, H. S. Teoh via Digitalmars-d-learn wrote:On Tue, Dec 02, 2014 at 05:21:27PM +0000, via Digitalmars-d-learn wrote:Indeed, thank you :) But still, why this method "month" or "years" while this oneOn Thursday, 7 April 2011 at 19:10:40 UTC, Piotr Szturmaj wrote:Sure it does: import std.datetime; import std.stdio; void main() { auto date = Date(2000, 1, 1); date += 10.days; writeln(date); } Output: 2000-Jan-11 TIs it possible to add a particular number of days to a Date? I have number of days since 1 Jan 2000 and I want to convert it to Date: int days = read!int; // number of days since 1 Jan 2000 Date x = Date(2000, 1, 1); x.add!"days"(days); Unfortunately add() does not support adding days. Will it be possible in the future or is there another approach? ThanksIs there any particular reason why std.datetime's "add" does not support days ?
Dec 02 2014
But still, why this method supports "month" or "years" while this oneBut still, why this method "month" or "years" while this one days ?*
Dec 02 2014
On Tue, Dec 02, 2014 at 06:49:54PM +0000, via Digitalmars-d-learn wrote:Hmm. Looks like an oversight. File a bug? T -- "Computer Science is no more about computers than astronomy is about telescopes." -- E.W. DijkstraBut still, why this method or "years" while this oneBut still, why this method "month" or "years" while this one ?*
Dec 02 2014
On 12/2/14 2:00 PM, H. S. Teoh via Digitalmars-d-learn wrote:On Tue, Dec 02, 2014 at 06:49:54PM +0000, via Digitalmars-d-learn wrote:Not an oversight. Date.add and Date.roll are for adding units that are variable. For example, how many days are in a month? Answer: depends on the month. How many days in a year? Answer: depends on the year. But days are NOT variable, there are exactly 24 hours in a day. So to add a day, you just add a day with +=. -SteveHmm. Looks like an oversight. File a bug?But still, why this method or "years" while this oneBut still, why this method "month" or "years" while this one ?*
Dec 02 2014
On 12/2/14 2:14 PM, Steven Schveighoffer wrote:Not an oversight. Date.add and Date.roll are for adding units that are variable. For example, how many days are in a month? Answer: depends on the month. How many days in a year? Answer: depends on the year. But days are NOT variable, there are exactly 24 hours in a day. So to add a day, you just add a day with +=.Aaaand to expand on this, since roll *does* support days, it's because the number being added isn't the important unit, it's the unit above. Hence rolling days means you will stay in the same month. I'm not exactly sure of the reason behind roll, but I'm sure Jonathan has one :) -Steve
Dec 02 2014
On Tuesday, 2 December 2014 at 19:21:35 UTC, Steven Schveighoffer wrote:On 12/2/14 2:14 PM, Steven Schveighoffer wrote:Oh well, this explanation make so much sense ! Unfortunately, I was not able to find it on the documentation :/ Thank you.Not an oversight. Date.add and Date.roll are for adding units that are variable. For example, how many days are in a month? Answer: depends on the month. How many days in a year? Answer: depends on the year. But days are NOT variable, there are exactly 24 hours in a day. So to add a day, you just add a day with +=.Aaaand to expand on this, since roll *does* support days, it's because the number being added isn't the important unit, it's the unit above. Hence rolling days means you will stay in the same month. I'm not exactly sure of the reason behind roll, but I'm sure Jonathan has one :) -Steve
Dec 02 2014
On Tuesday, December 02, 2014 14:21:35 Steven Schveighoffer via Digitalmars-d-learn wrote:On 12/2/14 2:14 PM, Steven Schveighoffer wrote:A prime example of where to use it would be if you have a GUI with spinners for the values, and you didn't want incrementing the day to increment the month. It would be kind of annoying to implement that without roll. And since you need to specify the units for rolling (and the addition operator was already used for normal adding), adding a Duration to the time point in order to roll didn't make sense, and a separate function was needed for it even for the smaller units. I don't remember if roll originated with Boost, or if I came up with it though (since portions of std.datetime's API are based on Boost - e.g. julianDay is only there because Boost had it, and astronomers use it such that it seemed useful enough to have; I wouldn't have come up with it on my own though). - Jonathan M DavisNot an oversight. Date.add and Date.roll are for adding units that are variable. For example, how many days are in a month? Answer: depends on the month. How many days in a year? Answer: depends on the year. But days are NOT variable, there are exactly 24 hours in a day. So to add a day, you just add a day with +=.Aaaand to expand on this, since roll *does* support days, it's because the number being added isn't the important unit, it's the unit above. Hence rolling days means you will stay in the same month. I'm not exactly sure of the reason behind roll, but I'm sure Jonathan has one :)
Dec 06 2014
On Tuesday, December 02, 2014 14:14:58 Steven Schveighoffer via Digitalmars-d-learn wrote:On 12/2/14 2:00 PM, H. S. Teoh via Digitalmars-d-learn wrote:Exactly. add wouldn't even exist if it weren't for the fact that Duration doesn't work with units greater than weeks because of they depend on what date you're talking about. - Jonathan M DavisOn Tue, Dec 02, 2014 at 06:49:54PM +0000, via Digitalmars-d-learn wrote:Not an oversight. Date.add and Date.roll are for adding units that are variable. For example, how many days are in a month? Answer: depends on the month. How many days in a year? Answer: depends on the year. But days are NOT variable, there are exactly 24 hours in a day. So to add a day, you just add a day with +=.Hmm. Looks like an oversight. File a bug?But still, why this method or "years" while this oneBut still, why this method "month" or "years" while this one ?*
Dec 06 2014