Why does DateTime.TryParse fail to parse negative (-ve) years for BC/BCE dates in ISO 8601 date format

41 views Asked by At

I've built a plot of influential people across history on a timeline using C# in Unity3D. I run a Validate() method over the .json and then construct gameobjects. The code successfully plots people with birth and death dates in AD. However, it fails and throws an error when I insert people into the .json file who lived in the BC/BCE era which uses negative years.

Can anyone please suggest why it's likely failing and what steps you'd take to try and resolve this?

Any help would be greatly appreciated! Thank you

Parsing code:

public override bool Validate()
{
    if (!base.Validate())
        return false;
    if (!DateTime.TryParse(date_of_birth, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
    {
        Debug.LogError($"{name} - Date of birth '{date_of_birth}' could not be parsed as a date, ignoring.");
        return false;
    }
    if (!string.IsNullOrEmpty(date_of_death) && !DateTime.TryParse(date_of_death, CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
    {
        Debug.LogError($"{name} - Date of death '{date_of_death}' could not be parsed as a date, ignoring.");
        return false;
    }
    return true;
}

Example JSON:

{
            "name": "Aristotle",
            "date_of_birth": "-0383-01-01T00:00:00Z",
            "date_of_death": "-0321-01-01T00:00:00Z",
},
{
            "name": "Jabir ibn Hayyan",
            "date_of_birth": "0721-01-01T00:00:00Z",
            "date_of_death": "0813-01-01T00:00:00Z",
},

Error:

Aristotle - Date of birth '-0383-01-01T00:00:00Z' could not be parsed as a date, ignoring.
UnityEngine.Debug:LogError (object)
...

Tried using 4 digit years: "date_of_birth": "-0383-01-01T00:00:00Z", 5 digit years: "date_of_birth": "-00383-01-01T00:00:00Z", based on https://en.wikipedia.org/wiki/ISO_8601#General_principles

Was expecting it to just convert it to a simple negative int.

0

There are 0 answers