From 434d7ad64fceab2ea204ce1fbf2515c6de84d3e3 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Mon, 19 Jul 2021 09:50:09 +0200 Subject: [PATCH] feat(year) make 2 digit year configurable (#26) adds a global setting and 2 functions; - setcenturyflip(val) - val = getcenturyflip() Co-authored-by: dimfish --- docs/index.html | 139 ++++++++++++++++++++++++++++++++++++++++++++- spec/date_spec.lua | 21 +++++++ src/date.lua | 9 ++- 3 files changed, 166 insertions(+), 3 deletions(-) diff --git a/docs/index.html b/docs/index.html index 8368152..571d68c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -531,12 +531,69 @@

+
+
+
+
+

6.1.3. getcenturyflip

+
+
+
Returns the current + global setting for centuryflip. + +
+
+
Syntax
+ +
+
+local centuryflip = date.getcenturyflip()
+
+
+ +
Arguments
+ +
+
+
+
none
+
+
+
+ +
Returns
+ +
The currrent global centuryflip value.
+ +
Remarks
+ +
centuryflip is a global setting and hence should only be set + by an application, never by a library.
+ +
Example
+ +
+
+local centuryflip = date.getcenturyflip()
+
+
+
+
+
+

6.1.3. isleapyear

+ "date.isleapyear">6.1.4. isleapyear

Check if a number @@ -597,6 +654,82 @@

+ +
+
+
+
+

6.1.5. setcenturyflip

+
+
+
Sets the global + value for centuryflip. Century flip determines how 2-digit years are + interpreted when parsing string values. Any value smaller than centuryflip + will be considered 20xx, and values greater or equal will become 19xx. + +
The default value is 0, so all 2 digit years are considered 19xx.
+
+
+
Syntax
+ +
+
+date.setcenturyflip(century_flip)
+
+
+ +
Arguments
+ +
+
+
+
century_flip
+ +
a number from 0 to 100
+
+
+
+ +
Returns
+ +
Nothing.
+ +
Remarks
+ +
centuryflip is a global setting and hence should only be set + by an application, never by a library.
+ +
Example
+ +
+
+date.setcenturyflip(0)
+assert(date("01-01-00")==date(1900,01,01))
+assert(date("01-01-50")==date(1950,01,01))
+assert(date("01-01-99")==date(1999,01,01))
+date.setcenturyflip(100)
+assert(date("01-01-00")==date(2000,01,01))
+assert(date("01-01-50")==date(2050,01,01))
+assert(date("01-01-99")==date(2099,01,01))
+date.setcenturyflip(50)
+assert(date("01-01-00")==date(2000,01,01))
+assert(date("01-01-49")==date(2049,01,01))
+assert(date("01-01-50")==date(1950,01,01))
+assert(date("01-01-99")==date(1999,01,01))
+
+
+
+
+
+
@@ -786,7 +919,9 @@

Date Format.  Short dates can use either a "/" or - "-" date separator, but must follow the month/day/year format

+ "-" date separator, but must follow the month/day/year format. 2 + digit years are interpreted according to the global +
centuryflip setting.

 assert(date("02-03-04")==date(1904,02,03))
 assert(date("12/25/98")==date(1998,12,25))
diff --git a/spec/date_spec.lua b/spec/date_spec.lua
index 983dcc9..138b524 100644
--- a/spec/date_spec.lua
+++ b/spec/date_spec.lua
@@ -69,6 +69,27 @@ describe("Testing the 'date' module", function()
     local e = date()              assert(e)
   end)
 
+  it("Tests century-flip", function()
+    local old = date.getcenturyflip()
+    finally(function()
+      date.setcenturyflip(old)
+    end)
+
+    assert(old==0)
+    assert(date("01-01-00")==date(1900,01,01))
+    assert(date("01-01-50")==date(1950,01,01))
+    assert(date("01-01-99")==date(1999,01,01))
+    date.setcenturyflip(100)
+    assert(date("01-01-00")==date(2000,01,01))
+    assert(date("01-01-50")==date(2050,01,01))
+    assert(date("01-01-99")==date(2099,01,01))
+    date.setcenturyflip(50)
+    assert(date("01-01-00")==date(2000,01,01))
+    assert(date("01-01-49")==date(2049,01,01))
+    assert(date("01-01-50")==date(1950,01,01))
+    assert(date("01-01-99")==date(1999,01,01))
+  end)
+
   it("Tests leap year", function()
     assert.is_true(date.isleapyear(2012))
     assert.is_true(date.isleapyear(2000))
diff --git a/src/date.lua b/src/date.lua
index 4d50e5d..6a991b5 100644
--- a/src/date.lua
+++ b/src/date.lua
@@ -21,6 +21,8 @@
   local DAYNUM_MIN = -365242500 -- Mon Jan 01 1000000 BCE 00:00:00
   local DAYNUM_DEF =  0 -- Mon Jan 01 0001 00:00:00
   local _;
+--[[ GLOBAL SETTINGS ]]--
+  local centuryflip = 0 -- year >= centuryflip == 1900, < centuryflip == 2000
 --[[ LOCAL ARE FASTER ]]--
   local type     = type
   local pairs    = pairs
@@ -325,7 +327,7 @@
         elseif sw("^(%d+)[/\\%s,-]?%s*") then --print("$Digits")
           x, c = tonumber(sw[1]), len(sw[1])
           if (x >= 70) or (m and d and (not y)) or (c > 3) then
-            sety( x + ((x >= 100 or c>3)and 0 or 1900) )
+            sety( x + ((x >= 100 or c>3) and 0 or x 100 then date_error_arg() end
+    centuryflip = y
+  end
+  function date.getcenturyflip() return centuryflip end
 
 -- Internal functions
   function date.fmt(str) if str then fmtstr = str end; return fmtstr end