minetest_calendar/init.lua

136 lines
4.0 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 = {
{ name = S("New Year's Eve"),
days = 0,
months = 0
},
{ name = S("Lucky Day"),
days = 7,
months = 7
},
{ name = S("Bad Luck Day"),
days = 13,
months = 2
},
{ name = S("Boxing Day"),
days = 23,
months = 11
},
{ name = S("Scary Day"),
days = 29,
months = 8
},
{ name = S("Fool's Day"),
days = 0,
months = 3
},
{ name = S("Sylvester"),
days = 29,
months = 11
},
}
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]
if holiday.days == days and holiday.months == months then
table.insert(found_holidays, holiday)
end
end
return found_holidays
end
-- Returny weekday number
-- * 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
-- 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
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
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")