| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . C A L E N D A R . C O N V E R S I O N S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2008-2025, 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 |
with Interfaces.C; use Interfaces.C; |
| 33 |
|
| 34 |
package body Ada.Calendar.Conversions is |
| 35 |
|
| 36 |
----------------- |
| 37 |
-- To_Ada_Time -- |
| 38 |
----------------- |
| 39 |
|
| 40 |
function To_Ada_Time (Unix_Time : long) return Time is |
| 41 |
Val : constant Long_Integer := Long_Integer (Unix_Time); |
| 42 |
begin |
| 43 |
return Conversion_Operations.To_Ada_Time (Val); |
| 44 |
end To_Ada_Time; |
| 45 |
|
| 46 |
-------------------- |
| 47 |
-- To_Ada_Time_64 -- |
| 48 |
-------------------- |
| 49 |
|
| 50 |
function To_Ada_Time_64 (Unix_Time : long_long) return Time is |
| 51 |
Val : constant Long_Long_Integer := Long_Long_Integer (Unix_Time); |
| 52 |
begin |
| 53 |
return Conversion_Operations.To_Ada_Time_64 (Val); |
| 54 |
end To_Ada_Time_64; |
| 55 |
|
| 56 |
----------------- |
| 57 |
-- To_Ada_Time -- |
| 58 |
----------------- |
| 59 |
|
| 60 |
function To_Ada_Time |
| 61 |
(tm_year : int; |
| 62 |
tm_mon : int; |
| 63 |
tm_day : int; |
| 64 |
tm_hour : int; |
| 65 |
tm_min : int; |
| 66 |
tm_sec : int; |
| 67 |
tm_isdst : int) return Time |
| 68 |
is |
| 69 |
Year : constant Integer := Integer (tm_year); |
| 70 |
Month : constant Integer := Integer (tm_mon); |
| 71 |
Day : constant Integer := Integer (tm_day); |
| 72 |
Hour : constant Integer := Integer (tm_hour); |
| 73 |
Minute : constant Integer := Integer (tm_min); |
| 74 |
Second : constant Integer := Integer (tm_sec); |
| 75 |
DST : constant Integer := Integer (tm_isdst); |
| 76 |
begin |
| 77 |
return |
| 78 |
Conversion_Operations.To_Ada_Time |
| 79 |
(Year, Month, Day, Hour, Minute, Second, DST); |
| 80 |
end To_Ada_Time; |
| 81 |
|
| 82 |
----------------- |
| 83 |
-- To_Duration -- |
| 84 |
----------------- |
| 85 |
|
| 86 |
function To_Duration |
| 87 |
(tv_sec : long; |
| 88 |
tv_nsec : long) return Duration |
| 89 |
is |
| 90 |
Secs : constant Long_Integer := Long_Integer (tv_sec); |
| 91 |
Nano_Secs : constant Long_Integer := Long_Integer (tv_nsec); |
| 92 |
begin |
| 93 |
return Conversion_Operations.To_Duration (Secs, Nano_Secs); |
| 94 |
end To_Duration; |
| 95 |
|
| 96 |
-------------------- |
| 97 |
-- To_Duration_64 -- |
| 98 |
-------------------- |
| 99 |
|
| 100 |
function To_Duration_64 |
| 101 |
(tv_sec : long_long; |
| 102 |
tv_nsec : long) return Duration |
| 103 |
is |
| 104 |
Secs : constant Long_Long_Integer := Long_Long_Integer (tv_sec); |
| 105 |
Nano_Secs : constant Long_Integer := Long_Integer (tv_nsec); |
| 106 |
begin |
| 107 |
return Conversion_Operations.To_Duration_64 (Secs, Nano_Secs); |
| 108 |
end To_Duration_64; |
| 109 |
|
| 110 |
------------------------ |
| 111 |
-- To_Struct_Timespec -- |
| 112 |
------------------------ |
| 113 |
|
| 114 |
procedure To_Struct_Timespec |
| 115 |
(D : Duration; |
| 116 |
tv_sec : out long; |
| 117 |
tv_nsec : out long) |
| 118 |
is |
| 119 |
Secs : Long_Integer; |
| 120 |
Nano_Secs : Long_Integer; |
| 121 |
|
| 122 |
begin |
| 123 |
Conversion_Operations.To_Struct_Timespec (D, Secs, Nano_Secs); |
| 124 |
|
| 125 |
tv_sec := long (Secs); |
| 126 |
tv_nsec := long (Nano_Secs); |
| 127 |
end To_Struct_Timespec; |
| 128 |
|
| 129 |
--------------------------- |
| 130 |
-- To_Struct_Timespec_64 -- |
| 131 |
--------------------------- |
| 132 |
|
| 133 |
procedure To_Struct_Timespec_64 |
| 134 |
(D : Duration; |
| 135 |
tv_sec : out long_long; |
| 136 |
tv_nsec : out long) |
| 137 |
is |
| 138 |
Secs : Long_Long_Integer; |
| 139 |
Nano_Secs : Long_Integer; |
| 140 |
|
| 141 |
begin |
| 142 |
Conversion_Operations.To_Struct_Timespec_64 (D, Secs, Nano_Secs); |
| 143 |
|
| 144 |
tv_sec := long_long (Secs); |
| 145 |
tv_nsec := long (Nano_Secs); |
| 146 |
end To_Struct_Timespec_64; |
| 147 |
|
| 148 |
------------------ |
| 149 |
-- To_Struct_Tm -- |
| 150 |
------------------ |
| 151 |
|
| 152 |
procedure To_Struct_Tm |
| 153 |
(T : Time; |
| 154 |
tm_year : out int; |
| 155 |
tm_mon : out int; |
| 156 |
tm_day : out int; |
| 157 |
tm_hour : out int; |
| 158 |
tm_min : out int; |
| 159 |
tm_sec : out int) |
| 160 |
is |
| 161 |
Year : Integer; |
| 162 |
Month : Integer; |
| 163 |
Day : Integer; |
| 164 |
Hour : Integer; |
| 165 |
Minute : Integer; |
| 166 |
Second : Integer; |
| 167 |
|
| 168 |
begin |
| 169 |
Conversion_Operations.To_Struct_Tm |
| 170 |
(T, Year, Month, Day, Hour, Minute, Second); |
| 171 |
|
| 172 |
tm_year := int (Year); |
| 173 |
tm_mon := int (Month); |
| 174 |
tm_day := int (Day); |
| 175 |
tm_hour := int (Hour); |
| 176 |
tm_min := int (Minute); |
| 177 |
tm_sec := int (Second); |
| 178 |
end To_Struct_Tm; |
| 179 |
|
| 180 |
------------------ |
| 181 |
-- To_Unix_Time -- |
| 182 |
------------------ |
| 183 |
|
| 184 |
function To_Unix_Time (Ada_Time : Time) return long is |
| 185 |
Val : constant Long_Integer := |
| 186 |
Conversion_Operations.To_Unix_Time (Ada_Time); |
| 187 |
begin |
| 188 |
return long (Val); |
| 189 |
end To_Unix_Time; |
| 190 |
|
| 191 |
--------------------- |
| 192 |
-- To_Unix_Time_64 -- |
| 193 |
--------------------- |
| 194 |
|
| 195 |
function To_Unix_Time_64 (Ada_Time : Time) return long_long is |
| 196 |
Val : constant Long_Long_Integer := |
| 197 |
Conversion_Operations.To_Unix_Time_64 (Ada_Time); |
| 198 |
begin |
| 199 |
return long_long (Val); |
| 200 |
end To_Unix_Time_64; |
| 201 |
|
| 202 |
----------------------- |
| 203 |
-- To_Unix_Nano_Time -- |
| 204 |
----------------------- |
| 205 |
|
| 206 |
function To_Unix_Nano_Time (Ada_Time : Time) return long_long is |
| 207 |
pragma Unsuppress (Overflow_Check); |
| 208 |
Ada_Rep : constant Time_Rep := Time_Rep (Ada_Time); |
| 209 |
|
| 210 |
begin |
| 211 |
return long_long (Ada_Rep + Epoch_Offset); |
| 212 |
|
| 213 |
exception |
| 214 |
when Constraint_Error => |
| 215 |
raise Time_Error; |
| 216 |
end To_Unix_Nano_Time; |
| 217 |
|
| 218 |
end Ada.Calendar.Conversions; |