summaryrefslogtreecommitdiffstats
path: root/aconf/model/time.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-03-10 22:45:18 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-03-24 01:18:13 +0200
commit7d9c43916b0600ac4879dfe9793eab807a83ab2b (patch)
treeec54ed64c9a557b6ea4ad88d31138a02d3e0cd04 /aconf/model/time.lua
parentcb6c243dc356ef1d46d7ddb96e6ea6ae007c6cca (diff)
downloadaconf-7d9c43916b0600ac4879dfe9793eab807a83ab2b.tar.bz2
aconf-7d9c43916b0600ac4879dfe9793eab807a83ab2b.tar.xz
rename ACF2 to Alpine Configurator (aconf)
Diffstat (limited to 'aconf/model/time.lua')
-rw-r--r--aconf/model/time.lua63
1 files changed, 63 insertions, 0 deletions
diff --git a/aconf/model/time.lua b/aconf/model/time.lua
new file mode 100644
index 0000000..ca6008d
--- /dev/null
+++ b/aconf/model/time.lua
@@ -0,0 +1,63 @@
+--[[
+Copyright (c) 2012-2014 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+local M = {}
+
+local raise = require('aconf.error').raise
+
+local object = require('aconf.object')
+local class = object.class
+local super = object.super
+
+
+local posix = require('posix')
+
+
+M.Timestamp = class(require('aconf.model.field').String)
+M.Date = class(M.Timestamp)
+M.Time = class(M.Timestamp)
+
+M.Date.format = '%Y-%m-%d'
+M.Time.format = '%H:%M:%S'
+M.Timestamp.format = M.Date.format..' '..M.Time.format
+
+function M.Timestamp:_offset(value)
+ if not value then return end
+ local time = posix.strptime(value, self.format)
+ return time and os.time(time)
+end
+
+function M.Timestamp:_load(context)
+ local value = super(self, M.Timestamp):_load(context)
+ return value and self.epoch_offset and os.date(self.format, value) or value
+end
+
+function M.Timestamp:_save(context, value)
+ if self.epoch_offset then value = self:_offset(value) end
+ super(self, M.Timestamp):_save(context, value)
+end
+
+function M.Timestamp:normalize(context, value)
+ local time = self:_offset(value)
+ return time and os.date(self.format, time) or value
+end
+
+function M.Timestamp:validate(context, value)
+ super(self, M.Timestamp):validate(context, value)
+ if not self:_offset(value) then raise(context.path, 'Invalid value') end
+end
+
+function M.Date:init(params)
+ super(self, M.Date):init(params)
+ self.widget = 'date'
+end
+
+function M.Time:init(params)
+ super(self, M.Time):init(params)
+ if params.seconds == false then self.format = '%H:%M' end
+end
+
+
+return M