1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
ch_base.open_mod(core.get_current_modname())
local storage = core.get_mod_storage()
local time_shift = storage:get_int("time_shift")
local epoch = 1577836800 -- 1. 1. 2020 UTC
-- local epoch2 = 2208988800 -- 1. 1. 2040 UTC
local time_speed_during_day, time_speed_during_night
local rwtime_callback
ch_time = {}
local dst_points_03 = {
[2022] = 1648342800,
[2023] = 1679792400,
[2024] = 1711846800,
[2025] = 1743296400,
[2026] = 1774746000,
[2027] = 1806195600,
[2028] = 1837645200,
[2029] = 1869094800,
[2030] = 1901149200,
}
local dst_points_10 = {
[2022] = 1667091600, -- 2 => 1
[2023] = 1698541200,
[2024] = 1729990800,
[2025] = 1761440400,
[2026] = 1792890000,
[2027] = 1824944400,
[2028] = 1856394000,
[2029] = 1887843600,
[2030] = 1919293200,
}
local nazvy_mesicu = {
{"leden", "ledna", "led", "LED"},
{"únor", "února", "úno", "ÚNO"},
{"březen", "března", "bře", "BŘE"},
{"duben", "dubna", "dub", "DUB"},
{"květen", "května", "kvě", "KVĚ"},
{"červen", "června", "čer", "ČER"},
{"červenec", "července", "čvc", "ČVC"},
{"srpen", "srpna", "srp", "SRP"},
{"září", "září", "zář", "ZÁŘ"},
{"říjen", "října", "říj", "ŘÍJ"},
{"listopad", "listopadu", "lis", "LIS"},
{"prosinec", "prosince", "pro", "PRO"},
}
local dny_v_tydnu = {
"pondělí",
"úterý",
"středa",
"čtvrtek",
"pátek",
"sobota",
"neděle",
}
local function ifthenelse(cond, t, f)
if cond then return t else return f end
end
-- Nastavení
-- ===================================
function ch_time.get_time_shift()
return time_shift
end
function ch_time.set_time_shift(new_shift)
storage:set_int("time_shift", new_shift)
time_shift = storage:get_int("time_shift")
end
function ch_time.get_time_speed_during_day()
return time_speed_during_day
end
function ch_time.get_time_speed_during_night()
return time_speed_during_night
end
function ch_time.set_time_speed_during_day(new_value)
time_speed_during_day = new_value
end
function ch_time.set_time_speed_during_night(new_value)
time_speed_during_night = new_value
end
function ch_time.get_rwtime_callback()
return rwtime_callback
end
function ch_time.set_rwtime_callback(new_callback)
--[[ callback musí vracet strukturu v tomto formátu:
secs = int,
string = string,
string_extended = string,
]]
rwtime_callback = new_callback
end
local Cas = {}
function Cas:den_v_tydnu_cislo()
local n = self.lt.wday - 1
return ifthenelse(n == 0, 7, n)
end
function Cas:den_v_tydnu_nazev()
return dny_v_tydnu[self:den_v_tydnu_cislo()]
end
function Cas:nazev_mesice(pad)
return nazvy_mesicu[self.lt.month][pad]
end
function Cas:den_v_roce()
return self.lt.yday
end
function Cas:posun_cislo()
return ifthenelse(self.je_letni_cas, 2, 1)
end
function Cas:posun_text()
return ifthenelse(self.je_letni_cas, "+02:00", "+01:00")
end
function Cas:znamka32()
return ch_time.znamka32(self.time)
end
function Cas:YYYY_MM_DD()
return string.format("%04d-%02d-%02d", self.rok, self.mesic, self.den)
end
function Cas:YYYY_MM_DD_HH_MM_SS()
return string.format("%04d-%02d-%02d %02d:%02d:%02d", self.rok, self.mesic, self.den, self.hodina, self.minuta, self.sekunda)
end
function Cas:YYYY_MM_DD_HH_MM_SS_ZZZ()
return string.format("%04d-%02d-%02d %02d:%02d:%02d %s",
self.rok, self.mesic, self.den, self.hodina, self.minuta, self.sekunda, self:posun_text())
end
function Cas:YYYY_MM_DD_HH_MM_SSZZZ()
return string.format("%04d-%02d-%02d %02d:%02d:%02d%s",
self.rok, self.mesic, self.den, self.hodina, self.minuta, self.sekunda, self:posun_text())
end
function Cas:YYYY_MM_DDTHH_MM_SSZZZ()
return string.format("%04d-%02d-%02dT%02d:%02d:%02d%s",
self.rok, self.mesic, self.den, self.hodina, self.minuta, self.sekunda, self:posun_text())
end
function Cas:HH_MM_SS()
return string.format("%02d:%02d:%02d", self.hodina, self.minuta, self.sekunda)
end
function Cas:UTC_YYYY_MM_DD()
local u = self.utc
return string.format("%04d-%02d-%02d", u.year, u.month, u.day)
end
function Cas:UTC_YYYY_MM_DD_HH_MM_SS()
local u = self.utc
return string.format("%04d-%02d-%02d %02d:%02d:%02d", u.year, u.month, u.day, u.hour, u.min, u.sec)
end
function Cas:za_n_sekund(n)
return ch_time.na_strukturu(self.time + n)
end
-- API
-- ============================================
-- Zformátuje čas stejným způsobem jako os.time(), ale podle českého locale.
function ch_time.date(format, time)
if format == nil then return nil end
local st = ch_time.na_strukturu(time)
local dvt_nazev = st:den_v_tydnu_nazev()
local values = {
["%a"] = dvt_nazev:sub(1, 2),
["%A"] = dvt_nazev,
["%b"] = nazvy_mesicu[st.mesic][3],
["%B"] = nazvy_mesicu[st.mesic][1],
["%c"] = st:YYYY_MM_DD_HH_MM_SS(),
["%d"] = string.format("%02d", st.den),
["%H"] = string.format("%02d", st.hodina),
["%I"] = string.format("%02d", st.hodina % 12),
["%M"] = string.format("%02d", st.minuta),
["%m"] = string.format("%02d", st.mesic),
["%p"] = ifthenelse(st.hodina < 12, "dop", "odp"),
["%S"] = string.format("%02d", st.sekunda),
["%w"] = tostring(st:den_v_tydnu_cislo()),
["%x"] = st:YYYY_MM_DD(),
["%X"] = st:HH_MM_SS(),
["%Y"] = tostring(st.rok),
["%y"] = string.format("%02d", st.rok % 100),
["%%"] = "%",
}
format = string.gsub(format, "%%[aAbBcdHImMpSwxXyY%%]", values)
return format
end
-- Vrací počet sekund od začátku epochy, posunutý o nastavený time_shift.
function ch_time.time()
return os.time() + time_shift
end
--[[
Vrátí strukturu popisující zadaný časový okamžik.
time = int, -- čas vrácený z ch_time.time() nebo posunutý
]]
function ch_time.na_strukturu(time)
if time == nil then
time = ch_time.time()
end
local utc_time = os.date("!*t", time)
local je_letni_cas
if utc_time.month < 7 then
local dst_point = dst_points_03[utc_time.year]
je_letni_cas = dst_point ~= nil and time >= dst_point
else
local dst_point = dst_points_10[utc_time.year]
je_letni_cas = dst_point ~= nil and time < dst_point
end
local lt = os.date("!*t", time + 3600 * ifthenelse(je_letni_cas, 2, 1))
local result = {
time = time,
utc = utc_time, -- year, month, day, hour, min, sec, yday
lt = lt, -- local time
rok = lt.year,
mesic = lt.month,
den = lt.day,
hodina = lt.hour,
minuta = lt.min,
sekunda = lt.sec,
je_letni_cas = je_letni_cas,
}
return setmetatable(result, {__index = Cas})
end
--[[
Vrátí strukturu popisující aktuální časový okamžik.
]]
function ch_time.aktualni_cas()
return ch_time.na_strukturu(ch_time.time())
end
--[[
Vrátí časovou známku v rozsahu typu 'int32_t' jako počet sekund od 1. 1. 2020 UTC.
Rozsah je od 1951-12-13 20:45:53 UTC do 2088-01-19 03:14:07 UTC.
Není-li zadaný čas, použije aktuální čas.
]]
function ch_time.znamka32(time)
if time == nil then
time = ch_time.time()
end
local result = time - epoch
if result > 2147483647 then
return 2147483647
elseif result < -2147483647 then
return -2147483647
else
return result
end
end
--[[
Vrátí herní čas ve struktuře:
{
day_count = int -- návratová hodnota funkce minetest.get_day_count()
timeofday = float -- hodnota podle funkce minetest.get_timeofday()
hodina = int (0..23) -- hodina ve hře (celá)
minuta = int (0..59) -- minuta ve hře (celá)
sekunda = int (0..59) -- sekunda ve hře (celá)
daynight_ratio = float
natural_light = int (0..15)
time_speed = float -- návratová hodnota core.settings:get("time_speed")
}
]]
function ch_time.herni_cas()
local timeofday = core.get_timeofday()
if timeofday == nil then return nil end
local sekundy_celkem = math.floor(timeofday * 86400)
local minuty_celkem = math.floor(sekundy_celkem / 60)
local hodiny_celkem = math.floor(minuty_celkem / 60)
local result = {
day_count = core.get_day_count(),
timeofday = timeofday,
hodina = hodiny_celkem,
minuta = minuty_celkem % 60,
sekunda = sekundy_celkem % 60,
time_speed = tonumber(core.settings:get("time_speed")),
}
if type(result.time_speed) ~= "number" then
core.log("warning", "ch_time.herni_cas(): invalid type of time_speed!")
end
if 367 < minuty_celkem and minuty_celkem < 1072 then
-- den
result.day_night_ratio, result.natural_light = 1, 15
elseif minuty_celkem < 282 or minuty_celkem > 1158 then
-- noc
result.day_night_ratio, result.natural_light = 0, 2
elseif minuty_celkem < 500 then
-- úsvit
result.day_night_ratio = (minuty_celkem - 282) / 86.0
if minuty_celkem < 295 then
result.natural_light = 3
elseif minuty_celkem < 305 then
result.natural_light = 4
elseif minuty_celkem < 312 then
result.natural_light = 5
elseif minuty_celkem < 319 then
|