boost::date_time: ISO compliant IO for periods/durations?

Hello boosters, I would like to know whether there already exist ISO 8086 conforming extractors/inserters for the duration/period classes? Specifically I am looking for io/string functions which can handle "Periods of Time, no Specific Start or End", which begin with a 'P' following a list of periods, e.g. "P18Y9M4DT11H9M8S" (18 years, 9 months, 4 days, 11 hours, 9 minutes and 8 seconds) (I am using the term period in the sense ISO 8086 seems to do, i.e. including durations and periods - or did I overlook something?) Thanks for any help, Daniel

On Tue, 11 May 2004 14:43:54 +0200, Daniel Krügler wrote
Hello boosters,
I would like to know whether there already exist ISO 8086
I assume you mean ISO 8601...
conforming extractors/inserters for the duration/period classes? Specifically I am looking for io/string functions which can handle "Periods of Time, no Specific Start or End", which begin with a 'P' following a list of periods, e.g.
"P18Y9M4DT11H9M8S" (18 years, 9 months, 4 days, 11 hours, 9 minutes and 8 seconds)
I'm afraid the library doesn't currently offer an extraction of this sort of duration. The primary reason is that I (and others) have had difficulty dealing with the imprecise nature of a time duration that includes years and months. The problem is, what is the length of a duration such as '3 months'? The answer is, it depends on which day you want to measure it from and what rules you want to apply. So for representing durations you currently only have days, hours, minutes, and seconds. So the question goes back to you -- what are you going to do with these durations? If you can answer that, it shouldn't be difficult to write a parsing function... Jeff

Jeff Garland schrieb:
On Tue, 11 May 2004 14:43:54 +0200, Daniel Krügler wrote
Hello boosters,
I would like to know whether there already exist ISO 8086
I assume you mean ISO 8601...
HeHe, of course. 8086 was the other magic number any DOS historian is aware of....
conforming extractors/inserters for the duration/period classes? Specifically I am looking for io/string functions which can handle "Periods of Time, no Specific Start or End", which begin with a 'P' following a list of periods, e.g.
"P18Y9M4DT11H9M8S" (18 years, 9 months, 4 days, 11 hours, 9 minutes and 8 seconds)
I'm afraid the library doesn't currently offer an extraction of this sort of duration. The primary reason is that I (and others) have had difficulty dealing with the imprecise nature of a time duration that includes years and months. The problem is, what is the length of a duration such as '3 months'? The answer is, it depends on which day you want to measure it from and what rules you want to apply. So for representing durations you currently only have days, hours, minutes, and seconds.
So the question goes back to you -- what are you going to do with these durations? If you can answer that, it shouldn't be difficult to write a parsing function...
Jeff
OK, I understand the nature of the problem here. And probably it wouldn't help, if ISO 8601 would define a month in this context, would it? Regrettably I have no access to the full 8601 spec, but does anyone have? Lets assume, that ISO 8601 would provide a resolution, would it make sense to make such an iso_... function available (Because it would depend on a special calendar definition)? Thanks for your reply, Daniel

On Tue, 11 May 2004 17:45:43 +0200, Daniel Krügler wrote
OK, I understand the nature of the problem here. And probably it wouldn't help, if ISO 8601 would define a month in this context, would it? Regrettably I have no access to the full 8601 spec, but does anyone have?
I have seen it and it doesn't discuss the semantic issues associated with such a representation -- only how it should be formatted as text.
Lets assume, that ISO 8601 would provide a resolution, would it make sense to make such an iso_... function available (Because it would depend on a special calendar definition)?
I have no problem adding the parsing, but there needs to be something to parse to. I have some experimental code that will likely become available in the future that represents months and years as a 'logical duration' type that is different from the 'physical duration' types. You can then imagine a structure like: struct logical_duration { logical_duration(years y, months m, days d, ...etc...); private: months month_count; //years just convert to months by multiplying by 12 time_duration time; //physical duration of all the other parts }; Then a duration like this could be parsed into this structure and then would provide for the usual interactions with the other classes in the library. For example: logical_duration ld = from_iso_string("P18Y9M4DT11H9M8S"); ptime t += add_with_end_of_month_snap(t, ld); And since none of this is in the library currently, I'd like to hear any details of your use case that you can share... Jeff

----- Original Message ----- From: "Jeff Garland" <jeff@crystalclearsoftware.com>
conforming extractors/inserters for the duration/period classes? Specifically I am looking for io/string functions which can handle "Periods of Time, no Specific Start or End", which begin with a 'P' following a list of periods, e.g.
"P18Y9M4DT11H9M8S" (18 years, 9 months, 4 days, 11 hours, 9 minutes and 8 seconds)
I'm afraid the library doesn't currently offer an extraction of this sort of duration. The primary reason is that I (and others) have had difficulty dealing with the imprecise nature of a time duration that includes years and months. The problem is, what is the length of a duration such as '3 months'? The answer is, it depends on which day you want to measure it from and what rules you want to apply. So for representing durations you currently only have days, hours, minutes, and seconds.
Such functionality would come very handy at times, though. Say you want something to occur every first day of the month. You'd want a start date which is the first day of a month, and increase that date with one month to reach the next month. This is currently very difficult, because the next date has to be calculated manually. It would be nice if a duration of a month or a year was available, although comparing the length of a month and the length of 30 days would be quite impossible. best regards, Richard Peters

On Wed, 12 May 2004 11:07:25 +0200, Richard Peters wrote
----- Original Message ----- From: "Jeff Garland" <jeff@crystalclearsoftware.com> ...snip...
rules you want to apply. So for representing durations you currently only have days, hours, minutes, and seconds.
Such functionality would come very handy at times, though. Say you want something to occur every first day of the month. You'd want a start date which is the first day of a month, and increase that date with one month to reach the next month. This is currently very difficult, because the next date has to be calculated manually. It would be nice if a duration of a month or a year was available, although comparing the length of a month and the length of 30 days would be quite impossible.
Don't disagree, but I think we need to keep 'hard' in perspective. Non-obvious yes, but not hard. The following will trivially do what you want using namespace boost::gregorian; date add_years_months(date d, int years, int months) { int total_months = years*12 + months; month_iterator mi(d, total_months); ++mi; return *mi; } int main() { date d = add_years_months(date(2003,Jan,1), 2, 3); std::cout << d << std::endl; //April 1 2005 } Jeff
participants (3)
-
Daniel Krügler
-
Jeff Garland
-
Richard Peters