You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At least two OpenHistoricalMap projects implement functions to convert between ISO 8601-1 strings and decimal years. A A PL/pgSQL function is used when generating tiles, and a JavaScript function is part of the timeslider Leaflet plugin. While I don’t doubt that these functions are correctly implemented, they’re quite involved.
Use the date’s year to create a date object for the previous and next New Year’s Days.
Convert all three date objects to a number (i.e., milliseconds since the epoch).
Get the difference between the given date and the previous New Year’s Day and the difference between the two New Year’s Days.
Get the proportion between these two differences and add it to the given year.
It ends up being four lines of code in JavaScript, without any need for special cases around leap years and such. It should be similar in PL/pgSQL with the to_number() and to_date() functions.
I think it would be nice to streamline these implementations to rely more heavily on these built-in date conversion utilities. Inevitably, someone will need similar functionality in a language besides PL/pgSQL or outside of Leaflet; they would need to be careful when porting the code to avoid the many pitfalls when parsing dates manually. I also suspect that this simpler algorithm is slightly faster in most languages, but probably not by a significant amount.
@gregallensworth pointed out a major flaw in the proposed algorithm as implemented in JavaScript. The Date() constructor conforms to the requirement in ISO 8601-1 that the year be exactly four digits, so years before 1 CE and after 9999 CE are unsupported, and years before 1000 CE need to be zero-padded (which I think we’re more lenient about).
The workaround is to set each date component individually, although it bloats the code beyond the four lines that I advertised above:
/** * Converts the given ISO 8601-1 date to a decimal year. * * @param isoDate A date string in ISO 8601-1 format. * @returns A floating point number of years since year 0. */functiondecimalYearFromISODate(isoDate){// Require a valid YYYY, YYYY-MM, or YYYY-MM-DD date, but allow the year// to be a variable number of digits or negative, unlike ISO 8601-1.if(!isoDate||!/^-?\d{1,4}(?:-\d\d){0,2}$/.test(isoDate))return;varymd=isoDate.split("-");// A negative year results in an extra element at the beginning.if(ymd[0]===""){ymd.shift();ymd[0]*=-1;}varyear=+ymd[0];vardate=dateFromUTC(year,+ymd[1]-1,+ymd[2]);if(isNaN(date))return;// Add the year and the fraction of the date between two New Year’s Days.varnextNewYear=dateFromUTC(year+1,0,1).getTime();varlastNewYear=dateFromUTC(year,0,1).getTime();returnyear+(date.getTime()-lastNewYear)/(nextNewYear-lastNewYear);}/** * Returns a `Date` object representing the given UTC date components. * * @param year A one-based year in the proleptic Gregorian calendar. * @param month A zero-based month. * @param day A one-based day. * @returns A date object. */functiondateFromUTC(year,month,day){vardate=newDate(Date.UTC(year,month,day));// Date.UTC() treats a two-digit year as an offset from 1900.date.setUTCFullYear(year);returndate;}
This workaround should be unnecessary in PL/pgSQL, since to_date() lets you specify the input date format and claims to support negative years.
I looked into this before rewriting the PL/pgSQL side, and to_date() will not work. Postgres dates start at 4713 BC and we have older data. In the Database I found 833 dates with negative years, and 156 of them are older than that limit. Most are natural features, like relation/2939425 (natural=scree) and relation/2937938 (natural=fell), both with start_date=-271820, there are 79 features at that year and 53 more at -99998. Seems the PL/pgSQL version has to stay arithmetic. I think the JavaScript side is fine, Date covers about ±273,790 years, though -271820 is only ~2,000 years from that limit.
Activity
1ec5 commentedon Jun 5, 2023
@gregallensworth pointed out a major flaw in the proposed algorithm as implemented in JavaScript. The
Date()constructor conforms to the requirement in ISO 8601-1 that the year be exactly four digits, so years before 1 CE and after 9999 CE are unsupported, and years before 1000 CE need to be zero-padded (which I think we’re more lenient about).The workaround is to set each date component individually, although it bloats the code beyond the four lines that I advertised above:
This workaround should be unnecessary in PL/pgSQL, since
to_date()lets you specify the input date format and claims to support negative years.1ec5 commentedon Mar 13, 2025
#800 (comment) has a six-line replacement for
pad_datein DateFunctions-plpgsql.Rub21 commentedon Sep 2, 2026
I looked into this before rewriting the PL/pgSQL side, and
to_date()will not work. Postgres dates start at 4713 BC and we have older data. In the Database I found 833 dates with negative years, and 156 of them are older than that limit. Most are natural features, like relation/2939425 (natural=scree) and relation/2937938 (natural=fell), both withstart_date=-271820, there are 79 features at that year and 53 more at -99998. Seems the PL/pgSQL version has to stay arithmetic. I think the JavaScript side is fine,Datecovers about ±273,790 years, though -271820 is only ~2,000 years from that limit.