Skip to content

Commit 5ecd81c

Browse files
committed
Added --date option for specifying the date to build for ('now' being the default).
The same 'now' time is now used everywhere.
1 parent 96166be commit 5ecd81c

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

build/meta.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ do
381381
local function outputArgumentChecks(errLevel, argsStr)
382382
local optionalPos = argsStr:find("?", 1, true) or #argsStr
383383
local n = 1
384+
local first = true
384385

385386
for pos, argNames, types in argsStr:gmatch"()([%w_,]+):([%w_,*]+)" do
386387
if types == "*" then
@@ -395,8 +396,11 @@ do
395396
local typesText = multipleTypes and types:gsub(",", " or ") or types
396397

397398
for argName in argNames:gmatch"[%w_]+" do
399+
if not first then __LUA"\n" end
400+
first = false
401+
398402
__LUA(F(
399-
"if %s then errorf(%d, \"Bad argument #%d '%s'. (Expected %s, got %%s)\", type(%s)) end\n",
403+
"if %s then errorf(%d, \"Bad argument #%d '%s'. (Expected %s, got %%s)\", type(%s)) end",
400404
ifFormat:format(argName, typesCode),
401405
errLevel + 1,
402406
n,

src/app.lua2p

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ local function setup()
305305
elseif arg == "--autobuild" then
306306
_G.autobuild = true
307307

308+
elseif arg == "--date" then
309+
i = i + 1
310+
local datetime = programArguments[i] or errorNoPos("[Arguments] Missing datetime value after '%s'.", argRaw)
311+
_G.nowTime = datetimeToTime(datetime)
312+
arg = arg .. os.date("! '%Y-%m-%d %H:%M:%S UTC'", nowTime)
313+
308314
elseif arg == "--drafts" then
309315
_G.includeDrafts = true
310316

src/functions.lua2p

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,8 +1759,8 @@ function _G.generateFromTemplateString(page, template, modTime, onPageInit)
17591759
page._readonly = true
17601760

17611761
if
1762-
(page.isDraft.v and not includeDrafts ) or -- Is draft?
1763-
(datetimeToTime(page.publishDate:g()) > os.time()) -- Is in future?
1762+
(page.isDraft.v and not includeDrafts ) or -- Is draft?
1763+
(datetimeToTime(page.publishDate:g()) > nowTime) -- Is in future?
17641764
then
17651765
page._isSkipped = true
17661766
site._outputFileSkippedPageCount = site._outputFileSkippedPageCount + 1
@@ -1986,7 +1986,7 @@ end
19861986
function _G.getDatetime(time)
19871987
!ARGS "? time:number"
19881988

1989-
local date = dateLib(time or os.time()):tolocal()
1989+
local date = dateLib(time or nowTime):tolocal()
19901990
local datetime = date:fmt"${iso}%z" :gsub("..$", ":%0") :gsub("%+00:00$", "Z")
19911991

19921992
return datetime
@@ -2619,8 +2619,7 @@ end
26192619
-- Compute the difference in seconds between local time and UTC. (Normal time.)
26202620
-- http://lua-users.org/wiki/TimeZone
26212621
function _G.getTimezone()
2622-
local now = os.time()
2623-
return os.difftime(now, os.time(os.date("!*t", now)))
2622+
return os.difftime(nowTime, os.time(os.date("!*t", nowTime)))
26242623
end
26252624

26262625
-- Return a timezone string in ISO 8601:2000 standard form (+hhmm or -hhmm).
@@ -2632,7 +2631,7 @@ end
26322631
-- Return the timezone offset in seconds, as it was on the given time. (DST obeyed.)
26332632
-- timezoneOffset = getTimezoneOffset( [ time=now ] )
26342633
function _G.getTimezoneOffset(time)
2635-
time = time or os.time()
2634+
time = time or nowTime
26362635
local dateUtc = os.date("!*t", time)
26372636
local dateLocal = os.date("*t", time)
26382637
dateLocal.isdst = false -- This is the trick.

src/globals.lua2p

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ _G.autobuild = false
6363
_G.enableGc = true
6464
_G.ignoreModificationTimes = false
6565
_G.includeDrafts = false
66+
_G.nowTime = os.time()
6667
_G.outputMetaprograms = false
6768
_G.verbosePrint = false
6869

testsite/content/blog/first.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{
2-
page.title = 'First Post!'
2+
page.title = "First Post!"
3+
page.date = "1995-01-01 13:59 GMT"
34

45
P.foo = "bar"
56
}}

testsite/content/tests.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{
22
page.title = 'Some "tests"<hr>'
3+
page.date = "2021-05-22 09:00 +02"
34

45
P.foo = "bar"
56
}}

0 commit comments

Comments
 (0)