1 | --------------------------------------------------------------- {{{1 ---------- |
---|---|
2 | --: Copyright © 2003 … 2023 Martin Krischik «krischik@users.sourceforge.net» |
3 | ------------------------------------------------------------------------------ |
4 | --: This library is free software; you can redistribute it and/or modify it |
5 | --: under the terms of the GNU Library General Public License as published by |
6 | --: the Free Software Foundation; either version 2 of the License, or (at your |
7 | --: option) any later version. |
8 | --: |
9 | --: This library is distributed in the hope that it will be useful, but |
10 | --: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | --: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | --: License for more details. |
13 | --: |
14 | --: You should have received a copy of the GNU Library General Public License |
15 | --: along with this library; if not, write to the Free Software Foundation, |
16 | --: Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | --------------------------------------------------------------- }}}1 ---------- |
18 | |
19 | pragma License (Modified_Gpl); |
20 | pragma Ada_2022; |
21 | |
22 | with Ada.Calendar.Formatting; |
23 | with Ada.Characters.Conversions; |
24 | |
25 | --- |
26 | -- @summary |
27 | -- Some calendar utility functions. |
28 | -- |
29 | -- @description |
30 | -- No, I have not created some new Calendar class - yet. Just a few calendar tools. |
31 | -- |
32 | package AdaCL.Calendar is |
33 | |
34 | procedure Split_Duration |
35 | (Seconds : Duration; |
36 | Hour : out Natural; |
37 | Minute : out Ada.Calendar.Formatting.Minute_Number; |
38 | Second : out Ada.Calendar.Formatting.Second_Number; |
39 | Sub_Second : out Ada.Calendar.Formatting.Second_Duration); |
40 | |
41 | --- |
42 | -- Convert a Day_Duration into Second_Number — which is basically the integer part of the duration |
43 | -- |
44 | --: @param Seconds to convert |
45 | --: @return the Second_Number of the duration |
46 | function To_Second_Number (Seconds : Ada.Calendar.Day_Duration) return Ada.Calendar.Formatting.Second_Number with |
47 | Pure_Function; |
48 | |
49 | --- |
50 | -- Convert a Day_Duration into Second_Duration — which is basically the fraction part of the duration |
51 | -- |
52 | --: @param Seconds to convert |
53 | --: @return the Second_Duration of the duration |
54 | function To_Second_Duration |
55 | (Seconds : Ada.Calendar.Day_Duration) return Ada.Calendar.Formatting.Second_Duration with |
56 | Pure_Function; |
57 | |
58 | --- |
59 | -- Image representation with Fractional seconds. |
60 | -- |
61 | --: @param Date value to create the image from |
62 | --: @param Fraction_Digits amount of fractional digits to add. |
63 | -- |
64 | function Date_Time_Image |
65 | (Date : Ada.Calendar.Time; |
66 | Fraction_Digits : Natural := 1) |
67 | return String with |
68 | Pure_Function, Post => Date_Time_Image'Result'Length = 20 + Fraction_Digits; |
69 | |
70 | --- |
71 | -- Wide image representation with Fractional seconds. |
72 | -- |
73 | --: @param Date value to create the image from |
74 | --: @param Fraction_Digits amount of fractional digits to add. |
75 | -- |
76 | function Date_Time_Wide_Image |
77 | (Date : Ada.Calendar.Time; |
78 | Fraction_Digits : Natural := 1) |
79 | return Wide_String is |
80 | (Ada.Characters.Conversions.To_Wide_String (Date_Time_Image (Date, Fraction_Digits))) with |
81 | Pure_Function, Inline, Post => Date_Time_Wide_Image'Result'Length = 20 + Fraction_Digits; |
82 | |
83 | --- |
84 | -- Wide wide image representation with Fractional seconds. |
85 | -- |
86 | --: @param Date value to create the image from |
87 | --: @param Fraction_Digits amount of fractional digits to add. |
88 | -- |
89 | function Date_Time_Wide_Wide_Image |
90 | (Date : Ada.Calendar.Time; |
91 | Fraction_Digits : Natural := 1) |
92 | return Wide_Wide_String is |
93 | (Ada.Characters.Conversions.To_Wide_Wide_String (Date_Time_Image (Date, Fraction_Digits))) with |
94 | Pure_Function, Inline, Post => Date_Time_Wide_Wide_Image'Result'Length = 20 + Fraction_Digits; |
95 | |
96 | --- |
97 | -- Image representation of a Duration in the form HHHH:MM:SStttt |
98 | -- |
99 | --: @param Seconds value to create the image from |
100 | --: @param Hour_Digits amount of digits for the hour to add. |
101 | --: @param Fraction_Digits amount of fractional digits to add. |
102 | -- |
103 | function Duration_Image |
104 | (Seconds : Duration; |
105 | Hour_Digits : Positive := 4; |
106 | Fraction_Digits : Natural := 1) |
107 | return String with |
108 | Pure_Function, |
109 | Pre => Hour_Digits >= 2 and then Seconds <= 3_600.0 * (10**Hour_Digits), |
110 | Post => Duration_Image'Result'Length = Hour_Digits + 7 + Fraction_Digits; |
111 | |
112 | --- |
113 | -- Image representation of a Duration in the form HHHH:MM:SStttt |
114 | -- |
115 | --: @param Seconds value to create the image from |
116 | --: @param Hour_Digits amount of digits for the hour to add. |
117 | --: @param Fraction_Digits amount of fractional digits to add. |
118 | -- |
119 | function Duration_Wide_Image |
120 | (Seconds : Duration; |
121 | Hour_Digits : Positive := 4; |
122 | Fraction_Digits : Natural := 1) |
123 | return Wide_String is |
124 | (Ada.Characters.Conversions.To_Wide_String (Duration_Image (Seconds, Hour_Digits, Fraction_Digits))) with |
125 | Pure_Function, |
126 | Inline, |
127 | Pre => Hour_Digits >= 2 and then Seconds <= 3_600.0 * (10**Hour_Digits), |
128 | Post => Duration_Wide_Image'Result'Length = Hour_Digits + 7 + Fraction_Digits; |
129 | |
130 | --- |
131 | -- Image representation of a Duration in the form HHHH:MM:SStttt |
132 | -- |
133 | --: @param Seconds value to create the image from |
134 | --: @param Hour_Digits amount of digits for the hour to add. |
135 | --: @param Fraction_Digits amount of fractional digits to add. |
136 | -- |
137 | function Duration_Wide_Wide_Image |
138 | (Seconds : Duration; |
139 | Hour_Digits : Positive := 4; |
140 | Fraction_Digits : Natural := 1) |
141 | return Wide_Wide_String is |
142 | (Ada.Characters.Conversions.To_Wide_Wide_String (Duration_Image (Seconds, Hour_Digits, Fraction_Digits))) with |
143 | Pure_Function, |
144 | Inline, |
145 | Pre => Hour_Digits >= 2 and then Seconds <= 3_600.0 * (10**Hour_Digits), |
146 | Post => Duration_Wide_Wide_Image'Result'Length = Hour_Digits + 7 + Fraction_Digits; |
147 | |
148 | end AdaCL.Calendar; |
149 | |
150 | ---------------------------------------------------------------- {{{ ---------- |
151 | --: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab : |
152 | --: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr : |
153 | --: vim: set spell spelllang=en_gb |