139 lines
4.4 KiB
Lua
139 lines
4.4 KiB
Lua
calendar = {}
|
|
local S
|
|
if minetest.get_translator then
|
|
S = minetest.get_translator("calendar")
|
|
else
|
|
S = function(s) return s end
|
|
end
|
|
local F = minetest.formspec_escape
|
|
|
|
calendar.MONTH_DAYS = 30
|
|
|
|
-- Long month names
|
|
calendar.month_names = { S('January'), S('February'), S('March'), S('April'), S('May'), S('June'), S('July'), S('August'), S('September'), S('October'), S('November'), S('December') }
|
|
--calendar.month_names = { S('Spring'), S('Summer'), S('Harvest'), S('Winter') }
|
|
-- Short month names
|
|
calendar.month_names_short = { S('Jan'), S('Feb'), S('Mar'), S('Apr'), S('May'), S('Jun'), S('Jul'), S('Aug'), S('Sep'), S('Oct'), S('Nov'), S('Dec') }
|
|
--calendar.month_names_short = { S('Spr'), S('Sum'), S('Har'), S('Win') }
|
|
calendar.MONTHS = #calendar.month_names
|
|
|
|
calendar.YEAR_DAYS = calendar.MONTHS * calendar.MONTH_DAYS
|
|
|
|
-- Week day names
|
|
calendar.weekday_names = { S("Monday"), S("Tuesday"), S("Wednesday"), S("Thursday"), S("Friday"), S("Saturday"), S("Sunday") }
|
|
calendar.weekday_names_short = { S("Mo"), S("Tu"), S("We"), S("Th"), S("Fr"), S("Sa"), S("Su") }
|
|
calendar.WEEK_DAYS = #calendar.weekday_names
|
|
calendar.FIRST_WEEK_DAY = 0
|
|
|
|
local holidays = {}
|
|
|
|
calendar.register_holiday = function(def)
|
|
table.insert(holidays, def)
|
|
end
|
|
|
|
-- Example holidays
|
|
calendar.register_holiday({
|
|
name = S("New Year's Eve"),
|
|
type = "monthday",
|
|
days = 0,
|
|
months = 0
|
|
})
|
|
|
|
calendar.register_holiday({
|
|
name = S("Mother's Day"),
|
|
type = "custom",
|
|
-- First Sunday in May
|
|
is_holiday = function(total_days)
|
|
local d, m, y = calendar.get_date(total_days)
|
|
local wday = calendar.get_weekday(total_days)
|
|
return wday == 6 and m == 4 and d <= 6
|
|
end,
|
|
})
|
|
|
|
calendar.get_holidays = function(total_days)
|
|
if not total_days then
|
|
total_days = minetest.get_day_count()
|
|
end
|
|
local days, months, years = calendar.get_date(total_days)
|
|
local found_holidays = {}
|
|
for h=1, #holidays do
|
|
local holiday = holidays[h]
|
|
if holiday.type == "monthday" then
|
|
if holiday.days == days and holiday.months == months and (holiday.years == nil or holiday.years == years) then
|
|
table.insert(found_holidays, holiday)
|
|
end
|
|
elseif holiday.type == "custom" then
|
|
if holiday.is_holiday(total_days) == true then
|
|
table.insert(found_holidays, holiday)
|
|
end
|
|
else
|
|
minetest.log("error", "[calender] Invalid holiday type: "..tostring(holiday.type))
|
|
end
|
|
end
|
|
return found_holidays
|
|
end
|
|
|
|
calendar.get_weekday = function(total_days)
|
|
if not total_days then
|
|
total_days = minetest.get_day_count()
|
|
end
|
|
return (total_days + calendar.FIRST_WEEK_DAY) % calendar.WEEK_DAYS
|
|
end
|
|
|
|
calendar.get_date = function(total_days, ordinal)
|
|
if not total_days then
|
|
total_days = minetest.get_day_count()
|
|
end
|
|
|
|
local y = math.floor(total_days / calendar.YEAR_DAYS) -- elapsed years in epoch
|
|
local m = math.floor(total_days % calendar.YEAR_DAYS / calendar.MONTH_DAYS) -- elapsed months in year
|
|
local d = math.floor(total_days % calendar.YEAR_DAYS % calendar.MONTH_DAYS) -- elapsed days in month
|
|
local j = math.floor(total_days % calendar.YEAR_DAYS) -- elapsed days in year
|
|
|
|
if ordinal == nil then
|
|
ordinal = false
|
|
end
|
|
if ordinal then
|
|
y = y + 1
|
|
m = m + 1
|
|
d = d + 1
|
|
j = j + 1
|
|
end
|
|
return d, m, y, j
|
|
end
|
|
|
|
calendar.get_date_string = function(format, total_days)
|
|
if not total_days then
|
|
total_days = minetest.get_day_count()
|
|
end
|
|
|
|
if format == nil then
|
|
format = S("%D days, %M months, %Y years")
|
|
end
|
|
|
|
local y, m, d, j = calendar.get_date(total_days)
|
|
|
|
-- cardinal values
|
|
format = string.gsub( format, "%%Y", y ) -- elapsed years in epoch
|
|
format = string.gsub( format, "%%M", m ) -- elapsed months in year
|
|
format = string.gsub( format, "%%D", d ) -- elapsed days in month
|
|
format = string.gsub( format, "%%J", j ) -- elapsed days in year
|
|
|
|
-- ordinal values
|
|
format = string.gsub( format, "%%y", y + 1 ) -- current year of epoch
|
|
format = string.gsub( format, "%%m", m + 1 ) -- current month of year
|
|
format = string.gsub( format, "%%d", d + 1 ) -- current day of month
|
|
format = string.gsub( format, "%%j", j + 1 ) -- current day of year
|
|
|
|
format = string.gsub( format, "%%b", calendar.month_names[ m + 1 ] ) -- current month long name
|
|
format = string.gsub( format, "%%h", calendar.month_names_short[ m + 1 ] ) -- current month short name
|
|
|
|
format = string.gsub( format, "%%z", "%%" )
|
|
|
|
return format
|
|
end
|
|
|
|
dofile(minetest.get_modpath("calendar").."/gui.lua")
|
|
dofile(minetest.get_modpath("calendar").."/command.lua")
|
|
dofile(minetest.get_modpath("calendar").."/node.lua")
|