andrewmcveigh / cljs-time

A clj-time inspired date library for clojurescript.
342 stars 57 forks source link

Parsing years with 2 digits or leading zeros #146

Open bgalartza opened 2 years ago

bgalartza commented 2 years ago

When trying to parse dates that contain years with 2 digits (years <= 99), or leading zeros (ej "0099") ,the library automatically adds the century.

cljs.user> (cljs-time.format/parse (cljs-time.format/formatter "yyyy-MM-DD") "99-12-10")
#object[Object 19991210T000000]
cljs.user> (cljs-time.format/parse (cljs-time.format/formatter "yyyy-MM-DD") "0001-12-10")                                                                                                    
#object[Object 20011210T000000] 
cljs.user> (cljs-time.format/parse "0000-10-15T23:50:58.165Z")                                                                                                                                
#object[Object 20001015T235058

First I thought it might be a bug, but after checking the involved code, it looks like it's the expected behavior. Also, found that regular JS Date objects have a similar behavior.

But I wonder if there is any workaround, configuration parameter, or method to avoid the year inference to actually parse years < 99. As IMHO it's a valid use case.

As reference this is the behaviour of the java-time Clojure library in this aspect:

dev> (java-time/local-date "yyyy-MM-dd" "0001-12-10")                                                                                                                                         
#object[java.time.LocalDate 0x3520f56d "0001-12-10"]

dev> (java-time/local-date "yy-MM-dd" "01-12-10")                                                                                                                                             
#object[java.time.LocalDate 0x6317782c "2001-12-10"]

This issue in the Google Closure library might also be interesting.

Thank you