tstampstr.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>  <--------------


partial class TIME_STAMP_STR < $TEXT, $ANCHORED_FMT

partial class TIME_STAMP_STR < $TEXT, $ANCHORED_FMT is -- This partial class provides conversions applicable to both time -- of day and elapsed time classes. -- Version 1.0 Oct 98. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 30 Oct 98 kh Original from TIME_STAMP class private scan( loc_cursor : STR_CURSOR ) : SAME pre ~void(loc_cursor) and ~loc_cursor.is_done post true -- see the public routines for post-conditions is -- This private routine gets the value of a time-stamp by attempting to -- look for date or time first, returning both elements zero if some format -- error occurred, both elements CARD::nil if a range error occurred, -- otherwise a valid value. start_index : CARD := loc_cursor.index ; real_start : CARD := start_index ; loc_date : DATES ; loc_time : TIMES ; loc_date := DATES::build(loc_cursor) ; if loc_cursor.index = start_index then -- error of some kind loc_time := TIMES::build(loc_cursor) ; -- try time first if start_index = loc_cursor.index then -- Not even that!! if loc_time.count = 0 then -- bad format loc_cursor.set_index(real_start) ; return days(0).millisecs(0) else loc_cursor.set_index(real_start) ; return days(CARD::nil).millisecs(CARD::nil) end else -- Time first OK! start_index : CARD := loc_cursor.index ; loc_date := DATES::build(loc_cursor) ; if loc_cursor.index = start_index then -- error again! if loc_date.count = 0 then loc_cursor.set_index(real_start) ; return days(0).millisecs(0) else loc_cursor.set_index(real_start) ; return days(CARD::nil).millisecs(CARD::nil) end else return create(loc_date,loc_time) end end else -- date first & OK! start_index : CARD := loc_cursor.index ; loc_time := TIMES::build(loc_cursor) ; if start_index = loc_cursor.index then -- an error of some kind if loc_time.count = 0 then -- bad format loc_cursor.set_index(real_start) ; return days(0).millisecs(0) else loc_cursor.set_index(real_start) ; return days(CARD::nil).millisecs(CARD::nil) end else -- time OK too! return create(loc_date,loc_time) end end end ; is_date_time( str : STR ) : CONVERSION_RESULTS pre (str.size > 0) post (result = CONVERSION_RESULTS::All_Right) or (result = CONVERSION_RESULTS::Out_of_Range) or (result = CONVERSION_RESULTS::Bad_Format) is -- This routine makes use of the date and time converters to check if -- the string argument contains a date-time representation at its head -- returning the appropriate conversion result. Note that, because of the -- sequential nature of the two components, a local string cursor has to be -- used to scan, rather than the is_xxx predicates of the date and time -- converters. loc_cursor : STR_CURSOR := str.cursor ; res : SAME := scan(loc_cursor) ; if res.millisecs = CARD::maxval then return CONVERSION_RESULTS::Out_of_Range elsif (res.days = 0) and (res.millisecs = 0) then return CONVERSION_RESULTS::Bad_Format else return CONVERSION_RESULTS::All_Right end end ; build( loc_cursor : STR_CURSOR ) : SAME pre ~void(loc_cursor) and (loc_cursor.remaining > 1) post ((result.days >= 0) and (result.millisecs >= 0) and (initial(loc_cursor.index) = loc_cursor.index)) or (initial(loc_cursor.index) < loc_cursor.index) is -- This routine creates a date and time value from the indicated string, -- returning an unchanged cursor if there was some error. This is built upon -- the date and time conversion facilities and makes the initial assumption -- that the date is first. If a date is not found first then an attempt is -- made to obtain a time first. If either component is unreadable then two -- zero or two CARD::nil components are returned dependent upon whether -- there was a bad format or a range error. return scan(loc_cursor) end ; create( str : STR ) : SAME pre (is_date_time(str) = CONVERSION_RESULTS::All_Right) post true is -- This routine creates the whole number corresponding to the textual -- representation contained in str in the given repertoire and encoding. return build(str.cursor) end ; create_local( str : STR ) : SAME pre (is_date_time(str) = CONVERSION_RESULTS::All_Right) post true is -- This routine creates the time stamp corresponding to the textual -- representation contained in str in the given repertoire and encoding. -- The string is assumed to be in the representation of a local time zone -- time stamp. res : SAME := build(str.cursor) ; return res.to_UTC(str.index_lib) end ; create_UTC( str : STR ) : SAME pre (is_date_time(str) = CONVERSION_RESULTS::All_Right) post true is -- This routine creates the time stamp corresponding to the textual -- representation contained in str in the given repertoire and encoding. -- The string is assumed to be in the representation of a UTC time stamp. res : SAME := build(str.cursor) ; return res end ; str( lib : LIBCHARS ) : STR pre ~void(lib) post (result.size > 0) is -- This routine returns a string representation of self using the given -- repertoire and encoding. Self MUST be a UTC timestamp. If a time_zone -- indication is required then the value of self will be converted to that -- time zone before formatting! return lib.culture.date_time.date_time.fmt(zone_stamp(lib),lib) end ; str : STR pre true post (result.size > 0) is -- This routine returns a string representation of self using the given -- repertoire and encoding. Self MUST be a UTC timestamp. If a time_zone -- indication is required then the value of self will be converted to that -- time zone before formatting! return str(LIBCHARS::default) end ; fmt( format : ANCHORED_DESCR, lib : LIBCHARS ) : STR pre ~void(format) and ~void(lib) post result.size > 0 is -- This routine returns a formatted representation of self in the given -- repertoire and encoding as dictated by the given format description. res : STR := str(lib) ; if res.size < format.leading then -- needs a filler res := STR::create(format.filler.card,lib).repeat( format.leading - res.size) + res end ; return res end ; fmt( format : ANCHORED_DESCR ) : STR pre ~void(format) post result.size > 0 is -- This routine returns a formatted representation of self in the default -- repertoire and encoding as dictated by the given format description. return fmt(format,LIBCHARS::default) end ; end ; -- TIME_STR