Search code examples
string-formattingdate-formattingisorecurring-events

recurring action: starttime and subsequent frequency formatting convention


I am building a service where users will be able to register a payment-plan that initiates at any given date and recurs at any given frequency. For the sake of generalisation, I consider this to be a recurrent action with a starting time.

Since the date and frequency are determined by third parties, I want to define a format for a string that describes both starting-time and frequency for storing in a database in a way that ensures maximum compatibilty with parties and interpreting functions, yet is simple and concise in its expression.

Since the granularity only needs to be on a day-to-day-basis, I am thinking of the following format:

YYYY/MM/DD%F

with a concrete example being:

2018/01/01%1M, signifying an initial payment at the first of January 2018 with a recurring payment every 1 month.

Are there any convetions for doing this, ISO standards, best practices, etc.? And do you foresee any caveats in my proposed solution? For instance, should the format include information about timezones? Or should I include time-of-day to ensure compatibility?


Solution

  • Yes, ISO 8601 defines standard formats for both dates and periods. Not for the combination of both into a payment plan, though, at least I don’t think.

    An ISO date goes like 2018-09-30 or 20180930. The form with hyphens is by far the most often used, so both for readability and for compatibility I would recommend it.

    An ISO period looks a bit more unusual. P1M means a period of one month. You can have P1Y (one year), P3M (3 months), P2W (2 weeks) and everything that you could dream of. The linked Wikipedia article mentions “For example, "P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds".”

    ISO 8601 also has formats for date-times, dates with time-of-day with and without offset from UTC. Whether you need any of that I cannot tell from the information you have given. The option is there if you find out the need is.

    Link: ISO 8601 on Wikipedia.