6

I use ISO 8601 to represent dates & durations and all is OK. But now I need to represent relative dates and durations like:

  • The date for first day, at midnight, of the next week
  • All the last month period.

In fact I need to have a syntax for it, I prefer a standard one. I don't need code (I yet have all the code) but a syntax to store relatives date or durations just like ISO 8601 use "P1Y2M10DT2H30M" for a duration.

Thanks...

philnext
  • 173
  • 1
  • 7
  • 1
    A little more detail would help. What technologies are you using? Are you using a rich framework such as Java or .NET? Or is this close-to-the-metal C code for some embedded application? – RationalGeek May 24 '11 at 14:50
  • In fact I use it to save a choice of period, my software log events and the user may choose to print, every month, the 'last month' events, or every first day of a new trimester, the 'last trimester' event. This syntax is used by some programs, the 'scheduler' in Python or for the end user interface in Delphi. – philnext May 24 '11 at 15:00
  • Start with a table that contains all of the dates within a reasonable range you'll be working with. – JeffO May 24 '11 at 15:14
  • @Jeff : I give to the user the possibility to choose all combinaisons of choices (last week, last month, last trimester, 2 last month, 2 last trimesters...) so there is not any 'reasonable' range for it. – philnext May 24 '11 at 19:39
  • The ICAL standard does us the notation `-PT11H30M` to indicate 11.5 hours before the event. – Manngo Mar 18 '17 at 11:44

3 Answers3

5

Not a standard, but a convention used by FogBugz and Jira (both bugtrackers) is to enter durations in simple terms like 2d for 2 days, 3w for 3 weeks, 1m for 1 month, etc.

The convention could be extended for dates and periods relative to some other date (usually the current date I presume).

  • today + 2 days: +2d
  • today a year back: -1y
  • next Sunday: +1Sun (use the day-of-week names which should of course be locale specific)
  • the 15 of next month: +m15, or +1m15
  • 20th of the previous: -1m20
  • last of next month: +1mL (L signifies last day of the month/week/quarter/semester/year, used a capital L to distinguish it from the number 1 in many fonts)

To specifiy periods instead of just dates, you could double up the letters:

  • all of last month: -1mm
  • all of next week: +1ww
  • all of last year: -1yy

To specify a period between two specific relative dates:

  • 15 of next month to 20th of month after that: +m15..+2m20
  • all of last month could then also be specified as: -1m1..-1mL

Parsing could become an interesting business...

Marjan Venema
  • 8,151
  • 3
  • 32
  • 35
  • OK it seems to be a good solution, quite close to my own syntax. Do you have any link to documentation ? – philnext May 24 '11 at 19:36
  • @philnext: sorry, no documentation, unless you count the FogBugz and Jira manuals. You should be able to find them at their respective sites? http://www.fogbugz.com and http://www.atlassian.com for Jira. – Marjan Venema May 24 '11 at 19:54
1

If I understand what you mean correctly, you wish to have a way to say DateTime.NextSunday? If that's the case, as far as I know there isn't a standard you can use for that.

The two examples you gave above are specific examples of a date you need. You would still express the date time using the ISO standard, you just have to prgrammatically define what each means.

How I would do this is in my UI, I'd have a button called "Next Week". Here in Canada, the first day of the week is Sunday, but that's not the same for everyone. Thus I'd write my code like this (c# is my language of choice):

DateTime today = DateTime.Now;
int daysToSunday =  (DayOfWeek.Sunday - DateTime.Now.DayOfWeek) + 7;
DateTime weekStart = today.Date.AddDays(daysToSunday);

Note, I didn't test this, just a quick write-up.

Moral though, is that for a concept of time like those you've stated above, you need to code a solution to arrive at those dates.

Tyanna
  • 9,528
  • 1
  • 34
  • 54
1

In the Java world Joda Time supports this kind of operation with respect to your locale meaning it adjusts automatically for the small cultural differences if you need it to.

For example you could create a DateTime which is absolute (today) and add it a relative period (today.plusDays(7) to add a week for example). It also supports many ways to convey this concept of relative duration depending on your use case.

If you do not use Java, I still suggest you look at it to see what to look for in a good calendar management library for your own environment or give you inspiration on making your own.

Careful here though if you decide to go custom, time and dates are simple concepts but only on the surface. There is a lot of adjustments that need to be done to the time in the background to keep it's absolute (a second is a second) and relative (at noon the sun should be at it's zenith) natures in check. Creating your own implementation is simple until you get errors when dealing with such things, combine the microsecond or more precision of computers and their absolute autistic trait they are quite inept when times come to deal with fuzzy concepts that we humans take for granted.

Hope this helped !

Newtopian
  • 7,201
  • 3
  • 35
  • 52
  • Thanks for it but I have all the code (in Python & Delphi) for it but I search for a standard syntax for 'the period for the last week' or 'the period of the week 2 weeks ago'. I have an internal syntax but I would found a standard one. – philnext May 24 '11 at 18:25
  • hooo I see now... sorry I misunderstood the question ! I guess thus far Marjan's answer is the best one, for fairness I'll transfer the upvote I got so it gets on top of mine. – Newtopian May 24 '11 at 21:41