From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f6b27885de40df95,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-03-20 08:23:30 PST Newsgroups: comp.lang.ada Path: supernews.google.com!sn-xit-02!supernews.com!bignews.mediaways.net!nntp.kreonet.re.kr!news.maxwell.syr.edu!news.tele.dk!134.222.94.5!npeer.kpnqwest.net!uunet!ash.uu.net!xyzzy!nntp From: Jeffrey Carter Subject: Date & time formatting operations for review X-Nntp-Posting-Host: e246420.msc.az.boeing.com Content-Type: text/plain; charset=us-ascii Message-ID: <3AB77D0C.4492D7A5@boeing.com> Sender: nntp@news.boeing.com (Boeing NNTP News Access) Content-Transfer-Encoding: 7bit Organization: The Boeing Company X-Accept-Language: en Mime-Version: 1.0 Date: Tue, 20 Mar 2001 15:53:48 GMT X-Mailer: Mozilla 4.5 [en]C-CCK-MCD Boeing Kit (WinNT; U) Xref: supernews.google.com comp.lang.ada:5904 Date: 2001-03-20T15:53:48+00:00 List-Id: Here is the spec of a package with some date & time formatting operations, as promised in an earlier post. As far as I know, any possible date/time format desired can be easily constructed from these operations, in any language that uses the Latin-1 character set. If anyone is aware of any format that can't be achieved with these operations, please let me know. Currently this package is unconditionally copyrighted and may not be used without permission, but once we're satisfied with them, and have tested them more thoroughly, they'll be folded into PragmARC.Date_Handler and made available under the GMLGPL. Jeff Code follows (some lines may have wrapped): -- Experimental date-formatting operations -- This would be part of PragmARC.Date_Handler, which provides a Split operation -- to obtain Hour, Minute, and Seconds -- By combining the image functions provided here, the client should be able to create any -- date or time format desired. -- For example, given -- Year = 1953 -- Month = 12 -- Day = 28 -- Hour = 7 -- Minute = 25 -- Seconds = 31.75284 -- to obtain "31.75S07H25M@28/001953-December", use -- Seconds_Image (Seconds, True, 2) & -- 'S' & -- Hour_Image_24 (Hour) & -- 'H' & -- Minute_Image (Minute) & -- "M@" & -- Day_Image (Day) & -- '/' & -- Year_Image_Long (Year, True, 6) & -- '-' & -- Month_Image_Long (Month) -- A client will usually create a date-formatting function for its desired format from these functions. -- Copyright (C) 2001 by PragmAda Software Engineering. All rights reserved. with Ada.Calendar; with Ada.Strings.Unbounded; use Ada; use Ada.Strings.Unbounded; package Date_Formatting is -- Copied from Date_Handler: subtype Hour_Number is Integer range 0 .. 23; subtype Minute_Number is Integer range 0 .. 59; subtype Minute_Duration is Calendar.Day_Duration range 0.0 .. 60.0; -- Image functions with a Zero_Fill parameter may have extra leading characters in the result. -- If Zero_Fill, these will be '0'; otherwise they will be ' '. -- Provide the image of any year CE: function Year_Image_Short (Year : Positive; Zero_Fill : Boolean := True) return String; -- Returns the decimal image of Year rem 100. -- If Zero_Fill, result will always be 2 characters long. Otherwise, result will be 1 or 2 characters long. function Year_Image_Long (Year : Positive; Zero_Fill : Boolean := True; Width : Positive := 4) return String; -- Returns the decimal image of Year. Result will be large enough to hold this image, or Width characters long, -- whichever is larger. type Case_Selection is (Lower, Mixed, Upper, As_Is); -- Determines the formatting of month names type Name_List is array (Calendar.Month_Number) of Unbounded_String; -- Used to provide month names Default_Short_Name : constant Name_List := (01 => To_Unbounded_String ("Jan"), 02 => To_Unbounded_String ("Feb"), 03 => To_Unbounded_String ("Mar"), 04 => To_Unbounded_String ("Apr"), 05 => To_Unbounded_String ("May"), 06 => To_Unbounded_String ("Jun"), 07 => To_Unbounded_String ("Jul"), 08 => To_Unbounded_String ("Aug"), 09 => To_Unbounded_String ("Sep"), 10 => To_Unbounded_String ("Oct"), 11 => To_Unbounded_String ("Nov"), 12 => To_Unbounded_String ("Dec") ); -- Default "short" month names Default_Long_Name : constant Name_List := (01 => To_Unbounded_String ("January"), 02 => To_Unbounded_String ("February"), 03 => To_Unbounded_String ("March"), 04 => To_Unbounded_String ("April"), 05 => To_Unbounded_String ("May"), 06 => To_Unbounded_String ("June"), 07 => To_Unbounded_String ("July"), 08 => To_Unbounded_String ("August"), 09 => To_Unbounded_String ("September"), 10 => To_Unbounded_String ("October"), 11 => To_Unbounded_String ("November"), 12 => To_Unbounded_String ("December") ); -- Default "long" month names function Month_Image_Numeric (Month : Calendar.Month_Number; Zero_Fill : Boolean := True) return String; -- Returns the decimal image of Month. -- If Zero_Fill, result will always be 2 characters long. Otherwise, result will be 1 or 2 characters long. function Month_Image_Alpha (Month : Calendar.Month_Number; Casing : Case_Selection := Mixed; Name : Name_List) return String; -- Returns To_String (Name (Month) ), formatted as directed by Casing. -- Renamings for default month names: function Month_Image_Short (Month : Calendar.Month_Number; Casing : Case_Selection := Mixed; Name : Name_List := Default_Long_Name) return String renames Month_Image_Alpha; function Month_Image_Long (Month : Calendar.Month_Number; Casing : Case_Selection := Mixed; Name : Name_List := Default_Long_Name) return String renames Month_Image_Alpha; function Day_Image (Day : Calendar.Day_Number; Zero_Fill : Boolean := True) return String; -- Returns the decimal image of Day. -- If Zero_Fill, result will always be 2 characters long. Otherwise, result will be 1 or 2 characters long. type AM_PM_ID is (AM, PM); type AM_PM_List is array (AM_PM_ID) of Unbounded_String; Default_AM_PM_Name : constant AM_PM_List := (AM => To_Unbounded_String (" am"), PM => To_Unbounded_String (" pm") ); function Hour_Image_12 (Hour : Hour_Number; AM_PM : AM_PM_List := Default_AM_PM_Name; Zero_Fill : Boolean := True) return String; -- If Hour = 0, Image is "12". If Hour in 1 .. 12, Image is image of Hour. Otherwise, Image is image of Hour - 12. -- If Hour < 12, returns Image & To_String (AM_PM (AM) ). Otherwise, returns Image & To_String (AM_PM (PM) ). -- If Zero_Fill, Image will always be 2 characters long. Otherwise, Image will be 1 or 2 characters long. function Hour_Image_24 (Hour : Hour_Number; Zero_Fill : Boolean := True) return String; -- Returns the decimal image of Hour. -- If Zero_Fill, result will always be 2 characters long. Otherwise, result will be 1 or 2 characters long. function Minute_Image (Minute : Minute_Number; Zero_Fill : Boolean := True) return String; -- Returns the decimal image of Minute. -- If Zero_Fill, result will always be 2 characters long. Otherwise, result will be 1 or 2 characters long. function Seconds_Image (Seconds : Minute_Duration; Zero_Fill : Boolean := True; Aft : Natural := 0) return String; -- Returns the decimal image of Seconds, with Aft digits after the decimal point. -- If Aft = 0, result will not contain a decimal point. -- If Aft = 0 and Zero_Fill, result will always be 2 characters long. -- If Aft = 0 and not Zero_Fill, result will be 1 or 2 characters long. -- If Aft > 0 and Zero_Fill, the portion of result left of the decimal point will always be 2 characters long. -- Otherwise, the portion of result left of the decimal point may be 1 or 2 characters long. -- If Seconds >= Minute_Duration'Last, returns Seconds_Image (Minute_Duration'First, Zero_Fill, Aft); if this -- occurs, you should increment Minute. This could result in changing every part of the time and date. end Date_Formatting;