208 lines
5.0 KiB
Plaintext
208 lines
5.0 KiB
Plaintext
|
import string
|
||
|
|
||
|
class Time
|
||
|
|
||
|
var utc
|
||
|
var last
|
||
|
var times
|
||
|
|
||
|
def init ()
|
||
|
self.utc = false
|
||
|
self.last = nil
|
||
|
self.times = {}
|
||
|
end
|
||
|
|
||
|
static def iso(epoch)
|
||
|
return tasmota.time_str(epoch)
|
||
|
end
|
||
|
|
||
|
static def zone(offset)
|
||
|
return tasmota.cmd("timezone "+str(offset))['Timezone']
|
||
|
end
|
||
|
|
||
|
# dynamic methods
|
||
|
|
||
|
|
||
|
def prop(epoch,prop)
|
||
|
if type(epoch) != 'int'
|
||
|
prop = epoch
|
||
|
epoch = self.now('e')
|
||
|
end
|
||
|
prop = prop ? prop : ""
|
||
|
var t = tasmota.time_dump(epoch)
|
||
|
if t.contains(prop)
|
||
|
return t.item(prop)
|
||
|
else
|
||
|
return t
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def isLocal(epoch)
|
||
|
return ! self.utc
|
||
|
end
|
||
|
|
||
|
def save (name,epoch)
|
||
|
if ! name
|
||
|
return false
|
||
|
end
|
||
|
if epoch == 'last'
|
||
|
epoch = self.last
|
||
|
end
|
||
|
if type(epoch) != 'integer'
|
||
|
epoch = self.now('e')
|
||
|
end
|
||
|
self.times.setitem(name,epoch)
|
||
|
return self.get(name)
|
||
|
end
|
||
|
|
||
|
def remove (name)
|
||
|
self.times.remove(name)
|
||
|
end
|
||
|
|
||
|
def get (name)
|
||
|
return name ? self.times.find(name) : self.times
|
||
|
end
|
||
|
|
||
|
def elapsed (name, unit)
|
||
|
if self.get(name)
|
||
|
var elapsed = self.now('e') - self.get(name)
|
||
|
if unit == 'hms' return self.hms(elapsed) end
|
||
|
return elapsed
|
||
|
end
|
||
|
|
||
|
end
|
||
|
|
||
|
def now(format)
|
||
|
var e = tasmota.rtc()
|
||
|
if format == 'r'
|
||
|
return e
|
||
|
end
|
||
|
e = e[self.utc ? 'utc' : 'local']
|
||
|
self.last = e
|
||
|
if format == nil
|
||
|
return self.iso(e)
|
||
|
end
|
||
|
if format == 'full'
|
||
|
return tasmota.strftime(e)
|
||
|
end
|
||
|
if format == 'e'
|
||
|
return e
|
||
|
end
|
||
|
|
||
|
end
|
||
|
|
||
|
def hms(t) # in seconds
|
||
|
var h = t / 3600
|
||
|
var s = (t % 3600)
|
||
|
var m = s / 60
|
||
|
s = s % 60
|
||
|
return string.format('%d:%d:%d',h,m,s)
|
||
|
end
|
||
|
|
||
|
def tomorrow(unit)
|
||
|
return self.day(unit,self.now('e') + 86400)
|
||
|
end
|
||
|
|
||
|
def yesterday(unit)
|
||
|
return self.day(unit,self.now('e') - 86400)
|
||
|
end
|
||
|
|
||
|
def today(unit)
|
||
|
return self.day(unit)
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
def day(unit,e)
|
||
|
if type(unit) == int
|
||
|
e = unit
|
||
|
unit = nil
|
||
|
end
|
||
|
e = e ? e : self.now('e')
|
||
|
if unit == 'json'
|
||
|
var d = tasmota.time_dump(e)
|
||
|
d.remove('hour')
|
||
|
d.remove('min')
|
||
|
d.remove('sec')
|
||
|
return d
|
||
|
end
|
||
|
if unit == 'full' return tasmota.strftime("%d %B %Y",e) end
|
||
|
if unit == 'dmy' return tasmota.strftime("%d/%m/%Y",e) end
|
||
|
if unit == 'ymd' return tasmota.strftime("%Y-%m-%d",e) end
|
||
|
return tasmota.strftime("%m/%d/%Y",e)
|
||
|
end
|
||
|
|
||
|
|
||
|
def tod(unit)
|
||
|
var t = {}
|
||
|
t = tasmota.time_dump(self.now('e'))
|
||
|
if unit == 'json'
|
||
|
t.remove('weekday')
|
||
|
t.remove('month')
|
||
|
t.remove('year')
|
||
|
t.remove('day')
|
||
|
return t
|
||
|
end
|
||
|
if unit == 'sec'
|
||
|
return t['hour']*3600+t['min']*60+t['sec']
|
||
|
end
|
||
|
if unit == 'hour'
|
||
|
return t['hour']+t['min']*1.0/60+t['sec']*1.0/3600
|
||
|
end
|
||
|
if unit == 'min'
|
||
|
return t['hour']*60+t['min']+t['sec']*1.0/60
|
||
|
end
|
||
|
if unit == '24'
|
||
|
return string.format('%02d:%02d:%02d',t['hour'],t['min'],t['sec'])
|
||
|
end
|
||
|
var half = t['hour'] > 11 ? "P.M" : "A.M"
|
||
|
var hour = t['hour'] > 12 ? t['hour'] - 12 : t['hour']
|
||
|
return string.format('%02d:%02d:%02d %s',hour,t['min'],t['sec'],half)
|
||
|
end
|
||
|
|
||
|
def test ()
|
||
|
|
||
|
print('epoch: ',self.now('e'))
|
||
|
print('iso: ',self.now())
|
||
|
print('raw: ',self.now('r'))
|
||
|
|
||
|
print('props: ',self.prop())
|
||
|
print('prop year: ',self.prop('year'))
|
||
|
print('prop year 0: ',self.prop(0,'year'))
|
||
|
|
||
|
|
||
|
print('day default(mdy): ',self.day())
|
||
|
print('day json: ',self.day('json'))
|
||
|
print('day dmy: ',self.day('dmy'))
|
||
|
print('day ymd: ',self.day('ymd'))
|
||
|
print('day full: ',self.day('full'))
|
||
|
|
||
|
print('tomorrow ymd: ',self.tomorrow('ymd'))
|
||
|
print('today ymd: ',self.today('ymd'))
|
||
|
print('yesterda ymd: ',self.yesterday('ymd'))
|
||
|
|
||
|
print('tod default: ',self.tod())
|
||
|
print('tod json: ',self.tod('json'))
|
||
|
print('tod sec: ',self.tod('sec'))
|
||
|
print('tod minutes: ',self.tod('min'))
|
||
|
print('tod hours: ',self.tod('hour'))
|
||
|
print('tod 24: ',self.tod('24'))
|
||
|
self.save('test1')
|
||
|
print('test1: ',self.get('test1'))
|
||
|
print('last: ',self.last)
|
||
|
self.save('test2')
|
||
|
print('test2: ',self.get('test2'))
|
||
|
print('last: ',self.last)
|
||
|
print('all saved:', self.get())
|
||
|
print('hms',self.hms(1),self.hms(61),self.hms(3661))
|
||
|
|
||
|
end
|
||
|
|
||
|
end
|
||
|
|
||
|
|
||
|
var time = module('time')
|
||
|
time = Time()
|
||
|
if debug[3] print('returning time instance') end
|
||
|
return time
|