comp.lang.ada
 help / color / mirror / Atom feed
* Date & time formatting operations for review
@ 2001-03-20 15:53 Jeffrey Carter
  2001-03-20 19:03 ` Ted Dennison
  2001-03-26 19:15 ` End of line delimited comments versus multiple line comments (was Date & time formatting operations for review) Colin Paul Gloster
  0 siblings, 2 replies; 8+ messages in thread
From: Jeffrey Carter @ 2001-03-20 15:53 UTC (permalink / raw)


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;



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2001-03-29  8:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-20 15:53 Date & time formatting operations for review Jeffrey Carter
2001-03-20 19:03 ` Ted Dennison
2001-03-20 22:58   ` Jeffrey Carter
2001-03-23  1:45     ` singlespeeder
2001-03-23 15:45       ` Jeffrey Carter
2001-03-26 19:15 ` End of line delimited comments versus multiple line comments (was Date & time formatting operations for review) Colin Paul Gloster
2001-03-27  4:12   ` Simon Wright
2001-03-29  8:54     ` Colin Paul Gloster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox