From 8ce2a7db5c4ea48a342b665385a54514c79d30bb Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 18 Aug 2014 15:13:51 -0400 Subject: [PATCH] luatz/timetable: Add __sub metamethod to allow subtraction of timetables. Reintroduces coercion from cfc4ba97c3596db1105abc17692a54185cfd3a30 --- luatz/timetable.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 ) -- 2.39.5