Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Year 2000 for 00-40 #26

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 137 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,69 @@ <h4 class="title"><a name="date.epoch" id=
</div>
</div>

<div class="section" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="date.getcenturyflip" id=
"date.getcenturyflip"></a>6.1.3.&nbsp;getcenturyflip</h4>
</div>
</div>
</div><a class="indexterm" name="id352557" id="id352557"></a>Returns the current
global setting for centuryflip.

<div class="variablelist">
<dl>
<dt><span class="term"><span class=
"bold"><strong>Syntax</strong></span></span></dt>

<dd>
<pre class="programlisting">
local centuryflip = date.<span class="bold"><strong>getcenturyflip</strong></span>()
</pre>
</dd>

<dt><span class="term"><span class=
"bold"><strong>Arguments</strong></span></span></dt>

<dd>
<div class="variablelist">
<dl>
<dt><span class="term"><span class=
"emphasis"><em>none</em></span></span></dt>
</dl>
</div>
</dd>

<dt><span class="term"><span class=
"bold"><strong>Returns</strong></span></span></dt>

<dd>The currrent global centuryflip value.</dd>

<dt><span class="term"><span class=
"bold"><strong>Remarks</strong></span></span></dt>

<dd>centuryflip is a global setting and hence should only be set
by an application, never by a library.</dd>

<dt><span class="term"><span class=
"bold"><strong>Example</strong></span></span></dt>

<dd>
<pre class="programlisting">
local centuryflip = date.getcenturyflip()
</pre>
</dd>
</dl>
</div>
</div>

<div class="section" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="date.isleapyear" id=
"date.isleapyear"></a>6.1.3.&nbsp;isleapyear</h4>
"date.isleapyear"></a>6.1.4.&nbsp;isleapyear</h4>
</div>
</div>
</div><a class="indexterm" name="id352556" id="id352556"></a>Check if a number
Expand Down Expand Up @@ -597,6 +654,82 @@ <h4 class="title"><a name="date.isleapyear" id=
</dl>
</div>
</div>

<div class="section" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a name="date.setcenturyflip" id=
"date.setcenturyflip"></a>6.1.5.&nbsp;setcenturyflip</h4>
</div>
</div>
</div><a class="indexterm" name="id352555" id="id352555"></a>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.

<div>The default value is 0, so all 2 digit years are considered 19xx.</div>
<div class="variablelist">
<dl>
<dt><span class="term"><span class=
"bold"><strong>Syntax</strong></span></span></dt>

<dd>
<pre class="programlisting">
date.<span class="bold"><strong>setcenturyflip</strong></span>(<em class=
"parameter"><code>century_flip</code></em>)
</pre>
</dd>

<dt><span class="term"><span class=
"bold"><strong>Arguments</strong></span></span></dt>

<dd>
<div class="variablelist">
<dl>
<dt><span class="term"><span class=
"emphasis"><em>century_flip</em></span></span></dt>

<dd>a number from 0 to 100</a></dd>
</dl>
</div>
</dd>

<dt><span class="term"><span class=
"bold"><strong>Returns</strong></span></span></dt>

<dd>Nothing.</dd>

<dt><span class="term"><span class=
"bold"><strong>Remarks</strong></span></span></dt>

<dd>centuryflip is a global setting and hence should only be set
by an application, never by a library.</dd>

<dt><span class="term"><span class=
"bold"><strong>Example</strong></span></span></dt>

<dd>
<pre class="programlisting">
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))
</pre>
</dd>
</dl>
</div>
</div>

</div>

<div class="section" lang="en" xml:lang="en">
Expand Down Expand Up @@ -786,7 +919,9 @@ <h4 class="title"><a name="date.__call" id=
</pre>

<p><b>Date Format.&nbsp;</b> Short dates can use either a "/" or
"-" date separator, but must follow the month/day/year format</p>
"-" date separator, but must follow the month/day/year format. 2
digit years are interpreted according to the global
<a href="#date.setcenturyflip">centuryflip setting</a>.</p>
<pre class="programlisting">
assert(date("02-03-04")==date(1904,02,03))
assert(date("12/25/98")==date(1998,12,25))
Expand Down
21 changes: 21 additions & 0 deletions spec/date_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 8 additions & 1 deletion src/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<centuryflip and 2000 or 1900) )
else
if m then setd(x) else m = x end
end
Expand Down Expand Up @@ -705,6 +707,11 @@
function date.epoch() return date_epoch:copy() end

function date.isodate(y,w,d) return date_new(makedaynum_isoywd(y + 0, w and (w+0) or 1, d and (d+0) or 1), 0) end
function date.setcenturyflip(y)
if y ~= floor(y) or y < 0 or y > 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
Expand Down