mirror of
https://github.com/TangentFoxy/lua-date.git
synced 2025-07-28 02:52:18 +00:00
Upload version 1.0
This commit is contained in:
64
samples/mkcalendar.lua
Normal file
64
samples/mkcalendar.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
--[[---------------------
|
||||
This script makes an html calendar
|
||||
Syntax: mkcalendar.lua year1 year2 year3 .. yearn > file
|
||||
arg:
|
||||
year1 .. yearn - the year(s) of the calendar to generate
|
||||
file - the name of the file to write the generated text calendar
|
||||
--]]---------------------
|
||||
|
||||
require"date"
|
||||
|
||||
function makemonth(y,m)
|
||||
local t = {}
|
||||
local d = date(y,m,1)
|
||||
t.name = d:fmt("%B")
|
||||
t.year = y
|
||||
-- get back to the nearest sunday
|
||||
d:adddays(-(d:getweekday()-1))
|
||||
repeat
|
||||
local tt = {}
|
||||
table.insert(t,tt)
|
||||
repeat -- insert the week days
|
||||
table.insert(tt, d:getday())
|
||||
until d:adddays(1):getweekday() == 1
|
||||
until d:getmonth() ~= m
|
||||
return t
|
||||
end
|
||||
|
||||
local htm_foot = '\n</html>'
|
||||
local htm_head = [[
|
||||
<style>
|
||||
th {background:black; color: silver; vertical-align: middle;}
|
||||
td {vertical-align: middle; text-align:center;}
|
||||
td.sun {color: red;}
|
||||
td.sat {color: blue;}
|
||||
</style>
|
||||
<html>
|
||||
]]
|
||||
local htm_yearhead = '\n<table align="left">'
|
||||
local htm_monhead = '\n<tr><th colspan = "7">%s, %s</th></tr><tr><td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thu</td><td>fri</td><td>sat</td></tr>'
|
||||
local htm_monweek = '\n<tr><td class="sun">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td class="sat">%s</td></tr>'
|
||||
local htm_yearfoot = '\n</table>'
|
||||
function makecalendar(y, iox)
|
||||
iox:write(htm_yearhead)
|
||||
for i = 1, 12 do
|
||||
local tm = makemonth(y, i)
|
||||
iox:write(string.format(htm_monhead, tm.name, tm.year))
|
||||
for k, v in ipairs(tm) do
|
||||
iox:write(string.format(htm_monweek, v[1], v[2], v[3], v[4], v[5], v[6], v[7]))
|
||||
end
|
||||
end
|
||||
iox:write(htm_yearfoot)
|
||||
|
||||
end
|
||||
|
||||
io.stdout:write(htm_head)
|
||||
|
||||
for k, v in ipairs(arg) do
|
||||
local y = tonumber(v)
|
||||
if y then makecalendar(y, io.stdout) end
|
||||
end
|
||||
|
||||
io.stdout:write(htm_foot)
|
||||
|
||||
|
41
samples/mkisocal.lua
Normal file
41
samples/mkisocal.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
--[[---------------------
|
||||
This script makes an iso year-week-day calendar
|
||||
Syntax: mkisocal.lua year1 year2 year3 .. yearn > file
|
||||
arg:
|
||||
year1 .. yearn - the year(s) of the calendar to generate
|
||||
file - the name of the file to write the generated text calendar
|
||||
--]]---------------------
|
||||
|
||||
require"date"
|
||||
|
||||
local htm_foot = [[</body></html>]]
|
||||
local htm_head = [[<html><head><style>body{color:#000000;background-color:#FFFFFF;font-family:sans-serif;}th{background:#000000;color:#CCCCCC;vertical-align:middle;}td{vertical-align:top;text-align:center;font-weight:bold;}.s{color:#999999;font-size:60%;}</style></head><body>]]
|
||||
local htm_yearhead = [[<table align="center" width="100%" border="1"><tr><th>Year</th><th>Week</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>]]
|
||||
local htm_yearfoot = [[</table>]]
|
||||
function makecalendar(year, iow)
|
||||
local d = date():setisoyear(year,1,1)
|
||||
iow(htm_yearhead)
|
||||
iow("<!--".. d .. "-->\n")
|
||||
while d:getisoyear() == year do
|
||||
iow(d:fmt("<tr><td>%G</td><td>%V<br/><small class='s'>%Y-%j</small></td>"))
|
||||
repeat iow(d:fmt("<td>%u<br/><small class='s'>%b %d %Y</small></td>"))
|
||||
until d:adddays(1):getisoweekday() == 1
|
||||
iow("</tr>\n")
|
||||
end
|
||||
iow(htm_yearfoot)
|
||||
|
||||
end
|
||||
|
||||
|
||||
local out = io.write
|
||||
|
||||
out(htm_head)
|
||||
|
||||
for k, v in ipairs(arg) do
|
||||
local d = tonumber(v);
|
||||
if d then makecalendar(d, out) end
|
||||
end
|
||||
|
||||
out(htm_foot)
|
||||
|
||||
|
23
samples/test.lua
Normal file
23
samples/test.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'date'
|
||||
|
||||
d1 = date('July 4 2009')
|
||||
assert(d1:getmonth() == 7)
|
||||
d2 = d1:copy()
|
||||
d2:adddays(2)
|
||||
diff = d2 - d1
|
||||
assert(diff:spandays() == 2)
|
||||
assert(diff:spanhours() == 48)
|
||||
|
||||
assert(date 'July 4 2009' == date '2009-07-04')
|
||||
|
||||
a = date(1970, 1, 1)
|
||||
y, m, d = a:getdate()
|
||||
assert(y == 1970 and m == 1 and d == 1)
|
||||
|
||||
d = date(1966, 'sep', 6)
|
||||
assert(d:getday() == 6)
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user