views:

89

answers:

2

I was just about to write this myself, but I know this has to exist and I'm just managing to avoid all the Google keywords that would lead me right to it.

I would be looking for something like DDDMMMYYY where D, M, Y are the number of days, months, and years. So 00103000 would indicate a span of three months and one day, or 000000001 would indicate a span of one year. Ideally this format would also have a standard way to apply it that could take into consideration all of the pitfalls of timespan calculation like varying number of days in a month, leap years, etc.

I am not looking for a way to calculate a timespan between two known timestamps, as was asked here (http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time), i'm looking for something like a specific string format I can store that would indicate a span of time to be used for determining a second unknown date from a known one.

Such as, using my fictitious format above: if I said "calculate what date would be 00103000 from September 15, 2009" would return "December 16, 2009", which is three months and one day after September 15th.

+2  A: 

The simplest and most common representation of this is a numeric: the number of seconds in the time span (which may or may not be fractional, depending on your needs). Many time libraries already represent time as seconds since the epoch, so this makes addition and subtraction of time trivial. The only major issue of concern is overflows (e.g. year 2038 bug).

Bob Aman
Yes, this would be great for indicating a firm number of seconds, but I would be looking for something that could handle the human constructs of months and years, and take into account variable month lengths and leap years.
kscott
Just to clarify. You can represent month-long or even decade-long durations in seconds. The nice thing about representing durations in seconds is that you can be as precise or imprecise as you want to be.
Bob Aman
+4  A: 

I would recommend looking at the ISO 8061 format for durations. It is not only easy to parse and apply to a given date, it is a well-known standard with a lot of resources available.

Determining the duration from two points in time is a bit trickier, but only because without input from the application, it is unclear whether 1-March to 1-June is 3 months or 92 days. Nonetheless, the format can express either equally well as well as, for example, 0.25 years.

DocMax
Yes! The ISO 8061 Duration is exactly what I was looking for, and there are plenty of parsing methods out there that handle month and leap year issues. Thanks.
kscott
Wow, that's good to know. I knew about representing exact times in ISO 8601, but didn't know it covered intervals as well.
Bob Aman
That said... in memory, you still may want to convert the duration to seconds.
Bob Aman
I agree with keeping seconds whenever it makes sense. In my most recent case, the duration was for reporting "events in the last month," so I could not just keep the duration in seconds since the number of seconds in "the last month" varied depending on when the user executed the report.
DocMax
and this too is exactly my predicament. The users want to see "last year" "last month", how it has to be calculated be damned.
kscott