Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #825 (closed defect: fixed)

Opened 12 years ago

Last modified 12 years ago

Calendar really needs add*(Time, int) methods

Reported by: Deewiant Assigned to: schveiguy
Priority: critical Milestone: 0.99.5
Component: Core Functionality Version: trunk
Keywords: Cc:

Description

Currently something as simple as adding one year to a Time is very complicated. One can't just do:

time += Gregorian.generic.toTime(1, 0, 0, 0, 0, 0, Gregorian.AD_ERA).span;

That won't work because the precise length of "one year" depends on which year is in question.

Also, that will fail because months is 0 which is an invalid value. One has to pass 1 as the month parameter, even though it won't be included in the time span. This also means that to add 1 month one has to pass 2 as the month. Intuitive much?

The basic call is something like:

time += Gregorian.generic.toTime(
	Gregorian.generic.getYears(time) + 1,
	1, 1, // months and days
	0, 0, 0, Gregorian.generic.getYears(time) < 0 ? Gregorian.BC_ERA : Gregorian.AD_ERA).span;

Now you've got the correct year length, but the years almost doubled. So you need to use a similar procedure as above to subtract the previous year count.

In summary, everything is way too convoluted unless you want to convert to a Date in between.

Hence, Calendar needs methods addYears(Time, int) (or addYears(Time, uint, era), whatever) and similarly for addMonths, addWeeks, whatever else.

Change History

12/20/07 21:55:08 changed by Deewiant

As schveiguy pointed out, my example code is erroneous in the way that getYears is always positive so one has to pass only getEra(time) as the last argument to toTime. Regardless, at least two function calls plus at least one temporary are required to add one year to a Time, so the fact of the matter doesn't change.

12/27/07 15:57:11 changed by schveiguy

  • status changed from new to assigned.
  • milestone set to 0.99.5.

01/11/08 03:06:46 changed by kris

there used to be an addMonths() in there somewhere ...

01/16/08 01:19:03 changed by schveiguy

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [3099]) Fixed bug in getDayOfWeek in Gregorian Added addMonths and addYears to Calendar/Gregorian. Fixes #825