From: daurnimator Date: Mon, 18 Aug 2014 19:13:51 +0000 (-0400) Subject: luatz/timetable: Add __sub metamethod to allow subtraction of timetables. Reintroduce... X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/8ce2a7db5c4ea48a342b665385a54514c79d30bb?ds=sidebyside luatz/timetable: Add __sub metamethod to allow subtraction of timetables. Reintroduces coercion from cfc4ba97c3596db1105abc17692a54185cfd3a30 --- diff --git a/luatz/timetable.lua b/luatz/timetable.lua index 6a39a13..10af170 100644 --- a/luatz/timetable.lua +++ b/luatz/timetable.lua @@ -148,7 +148,16 @@ function timetable_methods:strftime ( format_string ) return strftime ( format_string , self ) end -local timetable_mt = { +local timetable_mt + +local function coerce_arg ( t ) + if getmetatable ( t ) == timetable_mt then + return t:timestamp ( ) + end + return t +end + +timetable_mt = { __index = timetable_methods ; __tostring = timetable_methods.rfc_3339 ; __eq = function ( a , b ) @@ -157,6 +166,9 @@ local timetable_mt = { __lt = function ( a , b ) return a:timestamp ( ) < b:timestamp ( ) end ; + __sub = function ( a , b ) + return coerce_arg ( a ) - coerce_arg ( b ) + end ; } local function cast_timetable ( tm )