1-----------------------------------------------------------------------
2--  util-dates-formats -- Date Format ala strftime
3--  Copyright (C) 2011 Stephane Carrez
4--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
5--
6--  Licensed under the Apache License, Version 2.0 (the "License");
7--  you may not use this file except in compliance with the License.
8--  You may obtain a copy of the License at
9--
10--      http://www.apache.org/licenses/LICENSE-2.0
11--
12--  Unless required by applicable law or agreed to in writing, software
13--  distributed under the License is distributed on an "AS IS" BASIS,
14--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15--  See the License for the specific language governing permissions and
16--  limitations under the License.
17-----------------------------------------------------------------------
18
19with Ada.Strings.Unbounded;
20with Util.Properties;
21
22--  The <b>Util.Dates.Formats</b> provides a date formatting operation similar to the
23--  Unix <b>strftime</b> or the <b>GNAT.Calendar.Time_IO</b>.  The localization of month
24--  and day labels is however handled through <b>Util.Properties.Bundle</b> (similar to
25--  the Java world).  Unlike <b>strftime</b>, this allows to have a multi-threaded application
26--  that reports dates in several languages.  The <b>GNAT.Calendar.Time_IO</b> only supports
27--  English and this is the reason why it is not used here.
28--
29--  The date pattern recognizes the following formats:
30--
31--  %a     The abbreviated weekday name according to the current locale.
32--  %A     The full weekday name according to the current locale.
33--  %b     The abbreviated month name according to the current locale.
34--  %h     Equivalent to %b.  (SU)
35--  %B     The full month name according to the current locale.
36--  %c     The preferred date and time representation for the current locale.
37--  %C     The century number (year/100) as a 2-digit integer. (SU)
38--  %d     The day of the month as a decimal number (range 01 to 31).
39--  %D     Equivalent to %m/%d/%y
40--  %e     Like %d, the day of the month as a decimal number,
41--  but a leading zero is replaced by a space. (SU)
42--  %F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
43--  %G     The ISO 8601 week-based year
44--  %g     Like %G, but without century, that is, with a 2-digit year (00-99). (TZ)
45--  %H     The hour as a decimal number using a 24-hour clock (range 00 to 23).
46--  %I     The hour as a decimal number using a 12-hour clock (range 01 to 12).
47--  %j     The day of the year as a decimal number (range 001 to 366).
48--  %k     The hour (24-hour clock) as a decimal number (range 0 to 23);
49--  %l     The  hour (12-hour clock) as a decimal number (range 1 to 12);
50--  %m     The month as a decimal number (range 01 to 12).
51--  %M     The minute as a decimal number (range 00 to 59).
52--  %n     A newline character. (SU)
53--  %p     Either "AM" or "PM"
54--  %P     Like %p but in lowercase: "am" or "pm"
55--  %r     The time in a.m. or p.m. notation.
56--         In the POSIX locale this is equivalent to %I:%M:%S %p.  (SU)
57--  %R     The time in 24-hour notation (%H:%M).
58--  %s     The number of seconds since the Epoch, that is,
59--         since 1970-01-01 00:00:00 UTC. (TZ)
60--  %S     The  second as a decimal number (range 00 to 60).
61--  %t     A tab character. (SU)
62--  %T     The time in 24-hour notation (%H:%M:%S). (SU)
63--  %u     The day of the week as a decimal, range 1 to 7, Monday being 1.  See also %w.  (SU)
64--  %U     The week number of the current year as a decimal number, range 00 to 53
65--  %V     The  ISO 8601 week number
66--  %w     The day of the week as a decimal, range 0 to 6, Sunday being 0.  See also %u.
67--  %W     The week number of the current year as a decimal number, range 00 to 53
68--  %x     The preferred date representation for the current locale without the time.
69--  %X     The preferred time representation for the current locale without the date.
70--  %y     The year as a decimal number without a century (range 00 to 99).
71--  %Y     The year as a decimal number including the century.
72--  %z     The   time-zone   as   hour   offset   from   GMT.
73--  %Z     The timezone or name or abbreviation.
74--
75--  The following strftime flags are ignored:
76--
77--  %E     Modifier: use alternative format, see below. (SU)
78--  %O     Modifier: use alternative format, see below. (SU)
79--
80--  SU:  Single Unix Specification
81--  C99: C99 standard, POSIX.1-2001
82--
83--  See strftime (3) manual page
84package Util.Dates.Formats is
85
86   --  Month labels.
87   MONTH_NAME_PREFIX         : constant String := "util.month";
88
89   --  Day labels.
90   DAY_NAME_PREFIX           : constant String := "util.day";
91
92   --  Short month/day suffix.
93   SHORT_SUFFIX              : constant String := ".short";
94
95   --  Long month/day suffix.
96   LONG_SUFFIX               : constant String := ".long";
97
98   --  The date time pattern name to be used for the %x representation.
99   --  This property name is searched in the bundle to find the localized date time pattern.
100   DATE_TIME_LOCALE_NAME     : constant String := "util.datetime.pattern";
101
102   --  The default date pattern for %c (English).
103   DATE_TIME_DEFAULT_PATTERN : constant String := "%a %b %_d %T %Y";
104
105   --  The date pattern to be used for the %x representation.
106   --  This property name is searched in the bundle to find the localized date pattern.
107   DATE_LOCALE_NAME          : constant String := "util.date.pattern";
108
109   --  The default date pattern for %x (English).
110   DATE_DEFAULT_PATTERN      : constant String := "%m/%d/%y";
111
112   --  The time pattern to be used for the %X representation.
113   --  This property name is searched in the bundle to find the localized time pattern.
114   TIME_LOCALE_NAME          : constant String := "util.time.pattern";
115
116   --  The default time pattern for %X (English).
117   TIME_DEFAULT_PATTERN      : constant String := "%T %Y";
118
119   AM_NAME                   : constant String := "util.date.am";
120   PM_NAME                   : constant String := "util.date.pm";
121
122   AM_DEFAULT                : constant String := "AM";
123   PM_DEFAULT                : constant String := "PM";
124
125   --  Format the date passed in <b>Date</b> using the date pattern specified in <b>Pattern</b>.
126   --  The date pattern is similar to the Unix <b>strftime</b> operation.
127   --
128   --  For month and day of week strings, use the resource bundle passed in <b>Bundle</b>.
129   --  Append the formatted date in the <b>Into</b> string.
130   procedure Format (Into      : in out Ada.Strings.Unbounded.Unbounded_String;
131                     Pattern   : in String;
132                     Date      : in Date_Record;
133                     Bundle    : in Util.Properties.Manager'Class);
134
135   --  Format the date passed in <b>Date</b> using the date pattern specified in <b>Pattern</b>.
136   --  For month and day of week strings, use the resource bundle passed in <b>Bundle</b>.
137   --  Append the formatted date in the <b>Into</b> string.
138   procedure Format (Into      : in out Ada.Strings.Unbounded.Unbounded_String;
139                     Pattern   : in String;
140                     Date      : in Ada.Calendar.Time;
141                     Bundle    : in Util.Properties.Manager'Class);
142
143   function Format (Pattern   : in String;
144                    Date      : in Ada.Calendar.Time;
145                    Bundle    : in Util.Properties.Manager'Class) return String;
146
147   --  Append the localized month string in the <b>Into</b> string.
148   --  The month string is found in the resource bundle under the name:
149   --    util.month<month number>.short
150   --    util.month<month number>.long
151   --  If the month string is not found, the month is displayed as a number.
152   procedure Append_Month (Into   : in out Ada.Strings.Unbounded.Unbounded_String;
153                           Month  : in Ada.Calendar.Month_Number;
154                           Bundle : in Util.Properties.Manager'Class;
155                           Short  : in Boolean := True);
156
157   --  Append the localized month string in the <b>Into</b> string.
158   --  The month string is found in the resource bundle under the name:
159   --    util.month<month number>.short
160   --    util.month<month number>.long
161   --  If the month string is not found, the month is displayed as a number.
162   procedure Append_Day (Into   : in out Ada.Strings.Unbounded.Unbounded_String;
163                         Day    : in Ada.Calendar.Formatting.Day_Name;
164                         Bundle : in Util.Properties.Manager'Class;
165                         Short  : in Boolean := True);
166
167   --  Append a number with padding if necessary
168   procedure Append_Number (Into    : in out Ada.Strings.Unbounded.Unbounded_String;
169                            Value   : in Natural;
170                            Padding : in Character;
171                            Length  : in Natural := 2);
172
173   --  Append the timezone offset
174   procedure Append_Time_Offset (Into      : in out Ada.Strings.Unbounded.Unbounded_String;
175                                 Offset    : in Ada.Calendar.Time_Zones.Time_Offset);
176
177end Util.Dates.Formats;
178