1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT RUN-TIME COMPONENTS                         --
4--                                                                          --
5--                         G N A T . C A L E N D A R                        --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1999-2012, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This package extends Ada.Calendar to handle Hour, Minute, Second,
33--  Second_Duration and Day_Of_Week and Day_In_Year from Calendar.Time.
34--  Second_Duration precision depends on the target clock precision.
35--
36--  GNAT.Calendar provides the same kind of abstraction found in Ada.Calendar.
37--  It provides Split and Time_Of to build and split a Time data. And it
38--  provides accessor functions to get only one of Hour, Minute, Second,
39--  Second_Duration. Other functions are to access more advanced values like
40--  Day_Of_Week, Day_In_Year and Week_In_Year.
41
42with Ada.Calendar;
43with Interfaces.C;
44
45package GNAT.Calendar is
46
47   type Day_Name is
48     (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
49   pragma Ordered (Day_Name);
50
51   subtype Hour_Number         is Natural range 0 .. 23;
52   subtype Minute_Number       is Natural range 0 .. 59;
53   subtype Second_Number       is Natural range 0 .. 59;
54   subtype Second_Duration     is Ada.Calendar.Day_Duration range 0.0 .. 1.0;
55   subtype Day_In_Year_Number  is Positive range 1 .. 366;
56   subtype Week_In_Year_Number is Positive range 1 .. 53;
57
58   No_Time : constant Ada.Calendar.Time;
59   --  A constant set to the first date that can be represented by the type
60   --  Time. It can be used to indicate an uninitialized date.
61
62   function Hour       (Date : Ada.Calendar.Time) return Hour_Number;
63   function Minute     (Date : Ada.Calendar.Time) return Minute_Number;
64   function Second     (Date : Ada.Calendar.Time) return Second_Number;
65   function Sub_Second (Date : Ada.Calendar.Time) return Second_Duration;
66   --  Hour, Minute, Second and Sub_Second returns the complete time data for
67   --  the Date (H:M:S.SS). See Ada.Calendar for Year, Month, Day accessors.
68   --  Second_Duration precision depends on the target clock precision.
69
70   function Day_Of_Week (Date : Ada.Calendar.Time) return Day_Name;
71   --  Return the day name
72
73   function Day_In_Year (Date : Ada.Calendar.Time) return Day_In_Year_Number;
74   --  Return the day number in the year. (1st January is day 1 and 31st
75   --  December is day 365 or 366 for leap year).
76
77   procedure Split
78     (Date       : Ada.Calendar.Time;
79      Year       : out Ada.Calendar.Year_Number;
80      Month      : out Ada.Calendar.Month_Number;
81      Day        : out Ada.Calendar.Day_Number;
82      Hour       : out Hour_Number;
83      Minute     : out Minute_Number;
84      Second     : out Second_Number;
85      Sub_Second : out Second_Duration);
86   --  Split a standard Ada.Calendar.Time value in date data (Year, Month, Day)
87   --  and Time data (Hour, Minute, Second, Sub_Second).
88
89   procedure Split_At_Locale
90     (Date       : Ada.Calendar.Time;
91      Year       : out Ada.Calendar.Year_Number;
92      Month      : out Ada.Calendar.Month_Number;
93      Day        : out Ada.Calendar.Day_Number;
94      Hour       : out Hour_Number;
95      Minute     : out Minute_Number;
96      Second     : out Second_Number;
97      Sub_Second : out Second_Duration);
98   --  Split a standard Ada.Calendar.Time value in date data (Year, Month, Day)
99   --  and Time data (Hour, Minute, Second, Sub_Second). This version of Split
100   --  utilizes the time zone and DST bias of the locale (equivalent to Clock).
101   --  Due to this simplified behavior, the implementation does not require
102   --  expensive system calls on targets such as Windows.
103   --  WARNING: Split_At_Locale is no longer aware of historic events and may
104   --  produce inaccurate results over DST changes which occurred in the past.
105
106   function Time_Of
107     (Year       : Ada.Calendar.Year_Number;
108      Month      : Ada.Calendar.Month_Number;
109      Day        : Ada.Calendar.Day_Number;
110      Hour       : Hour_Number;
111      Minute     : Minute_Number;
112      Second     : Second_Number;
113      Sub_Second : Second_Duration := 0.0) return Ada.Calendar.Time;
114   --  Return an Ada.Calendar.Time data built from the date and time values
115
116   function Time_Of_At_Locale
117     (Year       : Ada.Calendar.Year_Number;
118      Month      : Ada.Calendar.Month_Number;
119      Day        : Ada.Calendar.Day_Number;
120      Hour       : Hour_Number;
121      Minute     : Minute_Number;
122      Second     : Second_Number;
123      Sub_Second : Second_Duration := 0.0) return Ada.Calendar.Time;
124   --  Return an Ada.Calendar.Time data built from the date and time values.
125   --  This version of Time_Of utilizes the time zone and DST bias of the
126   --  locale (equivalent to Clock). Due to this simplified behavior, the
127   --  implementation does not require expensive system calls on targets such
128   --  as Windows.
129   --  WARNING: Split_At_Locale is no longer aware of historic events and may
130   --  produce inaccurate results over DST changes which occurred in the past.
131
132   function Week_In_Year (Date : Ada.Calendar.Time) return Week_In_Year_Number;
133   --  Return the week number as defined in ISO 8601. A week always starts on
134   --  a Monday and the first week of a particular year is the one containing
135   --  the first Thursday. A year may have 53 weeks when January 1st is a
136   --  Wednesday and the year is leap or January 1st is a Thursday. Note that
137   --  the last days of December may belong to the first week on the next year
138   --  and conversely, the first days of January may belong to the last week
139   --  of the last year.
140
141   procedure Year_Week_In_Year
142     (Date : Ada.Calendar.Time;
143      Year : out Ada.Calendar.Year_Number;
144      Week : out Week_In_Year_Number);
145   --  Return the week number as defined in ISO 8601 along with the year in
146   --  which the week occurs.
147
148   --  C timeval conversion
149
150   --  C timeval represent a duration (used in Select for example). This
151   --  structure is composed of a number of seconds and a number of micro
152   --  seconds. The timeval structure is not exposed here because its
153   --  definition is target dependent. Interface to C programs is done via a
154   --  pointer to timeval structure.
155
156   type timeval is private;
157
158   function To_Duration (T : not null access timeval) return Duration;
159   function To_Timeval  (D : Duration) return timeval;
160
161private
162   --  This is a dummy declaration that should be the largest possible timeval
163   --  structure of all supported targets.
164
165   type timeval is array (1 .. 2) of Interfaces.C.long;
166
167   function Julian_Day
168     (Year  : Ada.Calendar.Year_Number;
169      Month : Ada.Calendar.Month_Number;
170      Day   : Ada.Calendar.Day_Number) return Integer;
171   --  Compute Julian day number
172   --
173   --  The code of this function is a modified version of algorithm 199 from
174   --  the Collected Algorithms of the ACM. The author of algorithm 199 is
175   --  Robert G. Tantzen.
176
177   No_Time : constant Ada.Calendar.Time :=
178               Ada.Calendar.Time_Of
179                 (Ada.Calendar.Year_Number'First,
180                  Ada.Calendar.Month_Number'First,
181                  Ada.Calendar.Day_Number'First);
182
183end GNAT.Calendar;
184