Version 1.1.1
[minetest_calendar.git] / init.lua
blob534d0771e60b61de3cdb2c63eadc97041ae933f5
1 calendar = {}
3 -- Compability translator code to support MT 0.4, which doesn't support
4 -- translations for mods.
5 -- This adds two dummy or wrapper functions:
6 -- * minetest.translate ← calendar._translate
7 -- * minetest.get_translator ← calendar._get_translator
9 if not minetest.translate then
10 -- No translation system available, use dummy functions
11 function calendar._translate(textdomain, str, ...)
12 local arg = {n=select('#', ...), ...}
13 return str:gsub("@(.)", function(matched)
14 local c = string.byte(matched)
15 if string.byte("1") <= c and c <= string.byte("9") then
16 return arg[c - string.byte("0")]
17 else
18 return matched
19 end
20 end)
21 end
23 function calendar._get_translator(textdomain)
24 return function(str, ...) return calendar._translate(textdomain or "", str, ...) end
25 end
26 else
27 -- Translation system available, just user wrapper functions
28 calendar._translate = minetest.translate
29 calendar._get_translator = minetest.get_translator
30 end
32 local S = calendar._get_translator("calendar")
34 dofile(minetest.get_modpath("calendar").."/gameconfig.lua")
36 local function update_helper_vars()
37 -- Number of months in a year
38 calendar.MONTHS = #calendar.month_names
39 -- Number of days in a week
40 calendar.WEEK_DAYS = #calendar.weekday_names
41 -- Number of days in a year
42 calendar.YEAR_DAYS = calendar.MONTHS * calendar.MONTH_DAYS
43 end
44 update_helper_vars()
46 local holidays = {}
48 calendar.config = function(config)
49 if config.MONTH_DAYS then
50 calendar.MONTH_DAYS = config.MONTH_DAYS
51 end
52 if config.month_names then
53 calendar.month_names = config.month_names
54 end
55 if config.month_names_short then
56 calendar.month_names_short = config.month_names_short
57 end
58 if config.weekday_names then
59 calendar.weekday_names = config.weekday_names
60 end
61 if config.weekday_names_short then
62 calendar.weekday_names_short = config.weekday_names_short
63 end
64 if config.FIRST_WEEK_DAY then
65 calendar.FIRST_WEEK_DAY = config.FIRST_WEEK_DAY
66 end
67 update_helper_vars()
68 end
70 calendar.register_holiday = function(def)
71 table.insert(holidays, def)
72 end
74 calendar.get_holidays = function(total_days)
75 if not total_days then
76 total_days = minetest.get_day_count()
77 end
78 local days, months, years = calendar.get_date(total_days)
79 local found_holidays = {}
80 for h=1, #holidays do
81 local holiday = holidays[h]
82 if holiday.type == "monthday" then
83 if holiday.days == days and holiday.months == months and (holiday.years == nil or holiday.years == years) then
84 table.insert(found_holidays, holiday)
85 end
86 elseif holiday.type == "custom" then
87 if holiday.is_holiday(total_days) == true then
88 table.insert(found_holidays, holiday)
89 end
90 else
91 minetest.log("error", "[calender] Invalid holiday type: "..tostring(holiday.type))
92 end
93 end
94 return found_holidays
95 end
97 calendar.get_weekday = function(total_days)
98 if not total_days then
99 total_days = minetest.get_day_count()
101 return (total_days + calendar.FIRST_WEEK_DAY) % calendar.WEEK_DAYS
104 calendar.get_date = function(total_days, ordinal)
105 if not total_days then
106 total_days = minetest.get_day_count()
109 local y = math.floor(total_days / calendar.YEAR_DAYS) -- elapsed years in epoch
110 local m = math.floor(total_days % calendar.YEAR_DAYS / calendar.MONTH_DAYS) -- elapsed months in year
111 local d = math.floor(total_days % calendar.YEAR_DAYS % calendar.MONTH_DAYS) -- elapsed days in month
112 local j = math.floor(total_days % calendar.YEAR_DAYS) -- elapsed days in year
114 if ordinal == nil then
115 ordinal = false
117 if ordinal then
118 y = y + 1
119 m = m + 1
120 d = d + 1
121 j = j + 1
123 return d, m, y, j
126 calendar.get_total_days_from_date = function(days, months, years, is_ordinal)
127 if is_ordinal then
128 days = days - 1
129 months = months - 1
130 years = years - 1
132 return years * calendar.YEAR_DAYS + months * calendar.MONTH_DAYS + days
135 calendar.get_date_string = function(format, total_days)
136 if not total_days then
137 total_days = minetest.get_day_count()
140 if format == nil then
141 format = S("%D days, %M months, %Y years")
144 local y, m, d, j = calendar.get_date(total_days)
145 local w = calendar.get_weekday(total_days)
147 -- cardinal values
148 format = string.gsub( format, "%%Y", y ) -- elapsed years in epoch
149 format = string.gsub( format, "%%M", m ) -- elapsed months in year
150 format = string.gsub( format, "%%D", d ) -- elapsed days in month
151 format = string.gsub( format, "%%J", j ) -- elapsed days in year
153 -- ordinal values
154 format = string.gsub( format, "%%y", y + 1 ) -- current year of epoch
155 format = string.gsub( format, "%%m", m + 1 ) -- current month of year
156 format = string.gsub( format, "%%d", d + 1 ) -- current day of month
157 format = string.gsub( format, "%%j", j + 1 ) -- current day of year
159 format = string.gsub( format, "%%b", S(calendar.month_names[ m + 1 ]) ) -- current month long name
160 format = string.gsub( format, "%%h", S(calendar.month_names_short[ m + 1 ]) ) -- current month short name
162 format = string.gsub( format, "%%W", S(calendar.weekday_names[ w + 1 ]) ) -- current weekday long name
163 format = string.gsub( format, "%%w", S(calendar.weekday_names_short[ w + 1 ]) ) -- current weekday short name
165 format = string.gsub( format, "%%z", "%%" )
167 return format
170 dofile(minetest.get_modpath("calendar").."/gui.lua")