1

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 (Calculate relative time in C#), 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.

Community
  • 1
  • 1
kscott
  • 1,866
  • 3
  • 25
  • 41
  • While I myself am using C#.net and MS SQL 2008 for development, I'd be interested in any implementation of such a format. – kscott Oct 22 '09 at 15:59
  • What's "one year after" a Feb 29? What's "one year before" the result of that? – AakashM Oct 22 '09 at 16:28

2 Answers2

4

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.

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
DocMax
  • 12,094
  • 7
  • 44
  • 44
  • 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 Oct 22 '09 at 16:27
  • 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 Oct 22 '09 at 17:18
  • That said... in memory, you still may want to convert the duration to seconds. – Bob Aman Oct 22 '09 at 17:21
  • 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 Oct 22 '09 at 17:48
  • 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 Oct 23 '09 at 14:56
2

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
  • 32,839
  • 9
  • 71
  • 95
  • 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 Oct 22 '09 at 16:18
  • 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 Oct 22 '09 at 17:23