minetest_calendar/init.lua

150 lines
5.0 KiB
Lua
Raw Normal View History

2020-08-26 17:24:25 -07:00
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
2020-08-26 17:26:34 -07:00
calendar.YEAR_DAYS = calendar.MONTHS * calendar.MONTH_DAYS
2020-08-26 17:24:25 -07:00
-- 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
2020-08-27 00:42:22 -07:00
calendar.FIRST_WEEK_DAY = 0
2020-08-26 17:24:25 -07:00
2020-08-27 01:40:04 -07:00
local holidays = {}
-- Register a holiday.
-- def: Table with these fields:
-- * name: Human-readable name
2020-08-27 15:06:46 -07:00
-- * type: type of holiday, determines other argumetns
-- Arguments when type=="monthday":
-- * days: Cardinal month day on which the holiday occurs
-- * months: Cardinal month on which the holiday occurs
-- * years: (optional) Cardinal year on which the holiday occurs. If not set, occurs every year
-- Arguments when type=="custom":
-- * is_holiday: function that takes total days as parameter and must return true if it's a holiday and false if not
2020-08-27 01:40:04 -07:00
calendar.register_holiday = function(def)
table.insert(holidays, def)
end
2020-08-27 15:06:46 -07:00
-- Example holidays
2020-08-27 01:40:04 -07:00
calendar.register_holiday({
name = S("New Year's Eve"),
2020-08-27 15:06:46 -07:00
type = "monthday",
2020-08-27 01:40:04 -07:00
days = 0,
months = 0
})
2020-08-26 17:24:25 -07:00
2020-08-27 15:06:46 -07:00
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,
})
2020-08-26 17:24:25 -07:00
calendar.get_holidays = function(total_days)
local days, months, years = calendar.get_date(total_days)
local found_holidays = {}
for h=1, #holidays do
local holiday = holidays[h]
2020-08-27 15:06:46 -07:00
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))
2020-08-26 17:24:25 -07:00
end
end
return found_holidays
end
2020-08-27 15:06:46 -07:00
-- Returns weekday number
2020-08-27 00:42:22 -07:00
-- * total_days: elapsed days since start of world
calendar.get_weekday = function(total_days)
return (total_days + calendar.FIRST_WEEK_DAY) % calendar.WEEK_DAYS
end
2020-08-26 17:24:25 -07:00
-- Returns day, month, year, days in year
-- * days: elapsed days since start of world
-- * ordinal: if true, use ordinal numbers (day, month, year start counting at 1).
-- if false, use cardinal numbers (day, month, year start counting at 0).
-- default: false
calendar.get_date = function(days, ordinal)
if not days then
days = minetest.get_day_count()
end
2020-08-26 17:26:34 -07:00
local y = math.floor( days / calendar.YEAR_DAYS ) -- elapsed years in epoch
local m = math.floor( days % calendar.YEAR_DAYS / calendar.MONTH_DAYS ) -- elapsed months in year
local d = math.floor( days % calendar.YEAR_DAYS % calendar.MONTH_DAYS ) -- elapsed days in month
local j = math.floor( days % calendar.YEAR_DAYS ) -- elapsed days in year
2020-08-26 17:24:25 -07:00
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( str, env_date )
if not env_date then
env_date = minetest.get_day_count( )
end
if str == nil then
str = S("%D days, %M months, %Y years")
end
local y, m, d, j = calendar.get_date()
-- cardinal values
str = string.gsub( str, "%%Y", y ) -- elapsed years in epoch
str = string.gsub( str, "%%M", m ) -- elapsed months in year
str = string.gsub( str, "%%D", d ) -- elapsed days in month
str = string.gsub( str, "%%J", j ) -- elapsed days in year
-- ordinal values
str = string.gsub( str, "%%y", y + 1 ) -- current year of epoch
str = string.gsub( str, "%%m", m + 1 ) -- current month of year
str = string.gsub( str, "%%d", d + 1 ) -- current day of month
str = string.gsub( str, "%%j", j + 1 ) -- current day of year
str = string.gsub( str, "%%b", calendar.month_names[ m + 1 ] ) -- current month long name
str = string.gsub( str, "%%h", calendar.month_names_short[ m + 1 ] ) -- current month short name
str = string.gsub( str, "%%z", "%%" )
return str
end
dofile(minetest.get_modpath("calendar").."/gui.lua")
2020-08-27 14:08:58 -07:00
dofile(minetest.get_modpath("calendar").."/command.lua")
dofile(minetest.get_modpath("calendar").."/node.lua")