Search code examples
datetimetimestampiso8601

Is there a standard way to indicate an invalid value in an ISO8601-formatted timestamp?


I have some code that creates an ISO8601-formatted timestamp but if the incoming date is in an invalid format, the conversion fails.

I'd like to know if there's a standard way of representing an invalid timestamp in this format?

Wikipedia mentions years prior to 1583 as not being handled under normal operation so my first thought was to simply set the value to something like 0000-01-01T00:00:00Z. If there's a more standard way to indicate an invalid value though, I'd rather use it instead.


Solution

  • ISO 8601 does not define a string that represents an invalid timestamp. It only defines what a valid formatted string looks like.

    Also, ISO 8601 uses a proleptic Gregorian calendar, which means that all positive years are valid. It allows for negative years (by agreement), but does not define eras such as BC/BCE. In other words, one day before 0001-01-01 would be 0000-12-31 in the proleptic Gregorian calendar. One day before 0000-01-01 would be -0001-12-31 (and year 0 is a leap year).

    As far as how you represent "invalid" in your code, that's entirely language and implementation dependent. In many cases, a null would be appropriate. If you are strictly working with strings, then perhaps "" would be better. Hard to say without more context.