times.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand        --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  is distributed  in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------


immutable class TIMES < $ORDERED{TIMES}, $HASH, $BINARY, $TEXT,

immutable class TIMES < $ORDERED{TIMES}, $HASH, $BINARY, $TEXT, $ANCHORED_FMT is -- This class handles clock times to the nearest millisecond, although -- the clock accuracy is dependent upon the underlying operating system clock. -- -- NOTE 1. All times are assumed to be in the same time zone for time -- manipulation purposes. Where time zone conversions are needed then -- the class TIME_STAMP should be used. -- -- 2. The feature now returns a value of UTC time. Care should -- therefore be taken when doing time arithmetic using its value! -- Version 1.6 May 2001. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 3 Jun 96 kh Original from std Modula-2 lib. -- 8 Apr 97 kh Modified for INT to CARD -- 15 Jun 97 kh Redesign for OS portability -- 1 Oct 98 kh Factored out conversion and added binary -- 30 Oct 98 kh Refined, added pre/post conditions -- 27 Jan 99 kh Added previous and following predicates -- 31 May 01 kh Introduced $ORDERED sub-typing include COMPARABLE ; include TIME_STR ; include BINARY ; private attr val : CARD ; -- in milliseconds. private const Millisecs_per_Sec : CARD := 1000 ; private const Seconds_per_Min : CARD := 60 ; private const Millisecs_per_Min : CARD := Millisecs_per_Sec * Seconds_per_Min ; private const Minutes_per_Hour : CARD := 60 ; private const Millisecs_per_Hr : CARD := Millisecs_per_Min * Minutes_per_Hour ; private const Hours_per_Day : CARD := 24 ; private const Millisecs_per_Day : CARD := Millisecs_per_Hr * Hours_per_Day ; null : SAME is -- This routine returns the null time -- the same as 'midnight' since -- the time of day starts with zero millisecs and finishes at ('midnight' - 1) -- -- the next millisec is, of course, zero again! return val(Millisecs_per_Day) end ; nil : SAME is -- This routine returns the nil time value - which is not a valid time! return val(CARD::nil) end ; build( cursor : BIN_CURSOR ) : SAME pre ~void(cursor) and ~cursor.is_done post true is -- This routine builds a time value from the binary string indicated. -- The time zone in which the value is represented should preferably be UTC -- time. return val(CARD::build(cursor)) end ; create( time_stamp : TIME_STAMP ) : SAME is -- This routine creates a new time object provided that the -- pre-condition is satisfied. return val(time_stamp.num_milliseconds) end ; now : SAME is -- This routine returns the current clock time of day as indicated by -- the computer system clock in UTC time. sys : OS_TIME := OS_TIME::time_stamp ; return val(sys.millisecs) end ; binstr : BINSTR pre true post val.binstr = result is -- This routine returns a binary string form representation of self. return val.binstr end ; is_eq( other : SAME ) : BOOL is -- This predicate returns true if and only if other and self represent -- the same time of day. return other.val = val end ; is_lt( other : SAME ) : BOOL is -- This predicate returns true if and only if self is earlier than -- other. return val < other.val end ; is_nil : BOOL is -- This predicate returns true if and only if self is the nil value. return val = CARD::nil end ; count : CARD pre true post result = val is -- This routine returns the count of milliseconds represented by self. return val end ; -- The following four routines return the component of the time -- indicated by their name as in the four element time expression - -- -- <hour> <minute> <second> <millisecs> hour : CARD pre true post (result = (val / Millisecs_per_Hr)) is return val / Millisecs_per_Hr end ; minute : CARD pre true post (result = ((val % Millisecs_per_Hr) / Millisecs_per_Min)) is return (val % Millisecs_per_Hr) / Millisecs_per_Min end ; second : CARD pre true post (result = ((val % Millisecs_per_Min) / Millisecs_per_Sec)) is return (val % Millisecs_per_Min) / Millisecs_per_Sec end ; millisecs : CARD pre true post (result = (val % Millisecs_per_Sec)) is return val % Millisecs_per_Sec end ; following( delay : ELAPSED ) : BOOL is -- This predicate returns true if and only if the result of adding -- delay to self would result in a time on the following day (irrespective of -- any days component of delay. return (delay.millisecs + val) >= Millisecs_per_Day end ; previous( diff : ELAPSED ) : BOOL is -- This predicate returns true if and only if the result of subtracting -- diff from self would result in a time on the previous day (irrespective of -- any days component of diff. return diff.millisecs > val end ; plus( delay : ELAPSED ) : SAME pre true post (result = val((delay.millisecs + val) % Millisecs_per_Day)) is -- This returns the result of adding the given elapsed time to self. -- If the clock goes past midnight then the time given is on the following -- day - no matter how many days have elapsed! res : CARD := delay.millisecs + val ; if res >= Millisecs_per_Day then res := res - Millisecs_per_Day end ; return val(res) end ; minus( other : SAME ) : ELAPSED pre true post ((val < other.val) and (result.millisecs = (other.val - val))) or ((val >= other.val) and (result.millisecs = (val - other.val))) is -- This returns the result of subtracting other from self. The result -- is the time difference which is always positive! if val < other.val then return ELAPSED::create(0,(other.val - val)) else return ELAPSED::create(0,(val - other.val)) end end ; minus( other : ELAPSED ) : SAME pre true post ((val < other.millisecs) and (result = val((val + Millisecs_per_Day) - other.millisecs))) or ((val >= other.millisecs) and (result = val(val - other.millisecs))) is -- This returns the result of subtracting the given elapsed time from -- self. If the result would be on the previous day then the clock time -- on that day is returned! if val < other.millisecs then return val((val + Millisecs_per_Day) - other.millisecs) else return val(val - other.millisecs) end end ; hash : CARD pre true -- irrespective of value post true -- irrespective of result is -- This routine is provided to enable the establishment of date based -- queues/lists. return val.hash end ; end ; -- TIMES