Skip to content

Instantly share code, notes, and snippets.

@narate
Last active December 12, 2022 12:43
Show Gist options
  • Save narate/7576032 to your computer and use it in GitHub Desktop.
Save narate/7576032 to your computer and use it in GitHub Desktop.
thaidate.lua print date time in Thai format.
--[[
Created by : Narate Ketram, 20-11-2013
Changelog:
- Refactoring code 30-11-2020
- fix extract_date function
- update 21-11-2013
* add thaidatetime show date and time
* change thaidate for show date only
example :
local thaidate = require 'thaidate'
print(thaidate.datetime(os.time()))
--]]
local _M = {}
local format = string.format
-- digit
local digit = { [0] = '๐' , '๑' , '๒' , '๓' , '๔' , '๕' , '๖' , '๗' , '๘' , '๙' }
-- day
local day = { 'อาทิตย์', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์' }
-- month
local month = {
'มกราคม',
'กุมภาพันธ์',
'มีนาคม',
'เมษายน',
'พฤษภาคม',
'มิถุนายน',
'กรกฎาคม',
'สิงหาคม',
'กันยายน',
'ตุลาคม',
'พฤศจิกายน',
'ธันวาคม'
}
function interp(s, tab)
return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
getmetatable("").__mod = interp
local function to_digit(c) return digit[tonumber(c)] end
local function extract(epoch)
local t = os.date('*t', epoch)
t.wday = day[t.wday]
t.month = month[t.month]
t.year = t.year + 543
t.time = format('%02s:%02s:%02s', t.hour, t.min, t.sec)
return t
end
_M.date = function(epoch)
local t = extract(epoch)
local date = ('${wday} ${day} ${month} ${year}' % t):gsub('(%d)', to_digit)
return date
end
_M.datetime = function(epoch)
local t = extract(epoch)
local datetime = ('${wday} ${day} ${month} ${year} เวลา ${time} น.' % t):gsub('(%d)', to_digit)
return datetime
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment