| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . C A L E N D A R . F O R M A T T I N G -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2006-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 Ada.Calendar.Time_Zones; use Ada.Calendar.Time_Zones; |
| 33 |
|
| 34 |
package body Ada.Calendar.Formatting is |
| 35 |
|
| 36 |
-------------------------- |
| 37 |
-- Implementation Notes -- |
| 38 |
-------------------------- |
| 39 |
|
| 40 |
-- All operations in this package are target and time representation |
| 41 |
-- independent, thus only one source file is needed for multiple targets. |
| 42 |
|
| 43 |
procedure Check_Char (S : String; C : Character; Index : Integer); |
| 44 |
-- Subsidiary to the two versions of Value. Determine whether the input |
| 45 |
-- string S has character C at position Index. Raise Constraint_Error if |
| 46 |
-- there is a mismatch. |
| 47 |
|
| 48 |
procedure Check_Digit (S : String; Index : Integer); |
| 49 |
-- Subsidiary to the two versions of Value. Determine whether the character |
| 50 |
-- of string S at position Index is a digit. This catches invalid input |
| 51 |
-- such as 1983-*1-j3 u5:n7:k9 which should be 1983-01-03 05:07:09. Raise |
| 52 |
-- Constraint_Error if there is a mismatch. |
| 53 |
|
| 54 |
procedure Split_Duration |
| 55 |
(Seconds : Duration; |
| 56 |
Hour : out Natural; |
| 57 |
Minute : out Minute_Number; |
| 58 |
Second : out Second_Number; |
| 59 |
Sub_Second : out Second_Duration); |
| 60 |
-- Version of Split that allows durations < 100 hours. |
| 61 |
-- Will raise Time_Error if Seconds >= 100 hours. |
| 62 |
|
| 63 |
---------------- |
| 64 |
-- Check_Char -- |
| 65 |
---------------- |
| 66 |
|
| 67 |
procedure Check_Char (S : String; C : Character; Index : Integer) is |
| 68 |
begin |
| 69 |
if S (Index) /= C then |
| 70 |
raise Constraint_Error; |
| 71 |
end if; |
| 72 |
end Check_Char; |
| 73 |
|
| 74 |
----------------- |
| 75 |
-- Check_Digit -- |
| 76 |
----------------- |
| 77 |
|
| 78 |
procedure Check_Digit (S : String; Index : Integer) is |
| 79 |
begin |
| 80 |
if S (Index) not in '0' .. '9' then |
| 81 |
raise Constraint_Error; |
| 82 |
end if; |
| 83 |
end Check_Digit; |
| 84 |
|
| 85 |
--------- |
| 86 |
-- Day -- |
| 87 |
--------- |
| 88 |
|
| 89 |
function Day |
| 90 |
(Date : Time; |
| 91 |
Time_Zone : Time_Zones.Time_Offset := 0) return Day_Number |
| 92 |
is |
| 93 |
Y : Year_Number; |
| 94 |
Mo : Month_Number; |
| 95 |
D : Day_Number; |
| 96 |
H : Hour_Number; |
| 97 |
Mi : Minute_Number; |
| 98 |
Se : Second_Number; |
| 99 |
Ss : Second_Duration; |
| 100 |
Le : Boolean; |
| 101 |
|
| 102 |
begin |
| 103 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone); |
| 104 |
return D; |
| 105 |
end Day; |
| 106 |
|
| 107 |
----------------- |
| 108 |
-- Day_Of_Week -- |
| 109 |
----------------- |
| 110 |
|
| 111 |
function Day_Of_Week (Date : Time) return Day_Name is |
| 112 |
begin |
| 113 |
return Day_Name'Val (Formatting_Operations.Day_Of_Week (Date)); |
| 114 |
end Day_Of_Week; |
| 115 |
|
| 116 |
---------- |
| 117 |
-- Hour -- |
| 118 |
---------- |
| 119 |
|
| 120 |
function Hour |
| 121 |
(Date : Time; |
| 122 |
Time_Zone : Time_Zones.Time_Offset := 0) return Hour_Number |
| 123 |
is |
| 124 |
Y : Year_Number; |
| 125 |
Mo : Month_Number; |
| 126 |
D : Day_Number; |
| 127 |
H : Hour_Number; |
| 128 |
Mi : Minute_Number; |
| 129 |
Se : Second_Number; |
| 130 |
Ss : Second_Duration; |
| 131 |
Le : Boolean; |
| 132 |
|
| 133 |
begin |
| 134 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone); |
| 135 |
return H; |
| 136 |
end Hour; |
| 137 |
|
| 138 |
----------- |
| 139 |
-- Image -- |
| 140 |
----------- |
| 141 |
|
| 142 |
function Image |
| 143 |
(Elapsed_Time : Duration; |
| 144 |
Include_Time_Fraction : Boolean := False) return String |
| 145 |
is |
| 146 |
To_Char : constant array (0 .. 9) of Character := "0123456789"; |
| 147 |
Hour : Natural; |
| 148 |
Minute : Minute_Number; |
| 149 |
Second : Second_Number; |
| 150 |
Sub_Second : Duration; |
| 151 |
SS_Nat : Natural; |
| 152 |
|
| 153 |
-- Determine the two slice bounds for the result string depending on |
| 154 |
-- whether the input is negative and whether fractions are requested. |
| 155 |
|
| 156 |
First : constant Integer := (if Elapsed_Time < 0.0 then 1 else 2); |
| 157 |
Last : constant Integer := (if Include_Time_Fraction then 12 else 9); |
| 158 |
|
| 159 |
Result : String := "-00:00:00.00"; |
| 160 |
|
| 161 |
begin |
| 162 |
Split_Duration (abs Elapsed_Time, Hour, Minute, Second, Sub_Second); |
| 163 |
|
| 164 |
-- Hour processing, positions 2 and 3 |
| 165 |
|
| 166 |
Result (2) := To_Char (Hour / 10); |
| 167 |
Result (3) := To_Char (Hour mod 10); |
| 168 |
|
| 169 |
-- Minute processing, positions 5 and 6 |
| 170 |
|
| 171 |
Result (5) := To_Char (Minute / 10); |
| 172 |
Result (6) := To_Char (Minute mod 10); |
| 173 |
|
| 174 |
-- Second processing, positions 8 and 9 |
| 175 |
|
| 176 |
Result (8) := To_Char (Second / 10); |
| 177 |
Result (9) := To_Char (Second mod 10); |
| 178 |
|
| 179 |
-- Optional sub second processing, positions 11 and 12 |
| 180 |
|
| 181 |
if Include_Time_Fraction and then Sub_Second > 0.0 then |
| 182 |
|
| 183 |
-- Prevent rounding up when converting to natural, avoiding the zero |
| 184 |
-- case to prevent rounding down to a negative number. |
| 185 |
|
| 186 |
SS_Nat := Natural (Duration'(Sub_Second * 100.0) - 0.5); |
| 187 |
|
| 188 |
Result (11) := To_Char (SS_Nat / 10); |
| 189 |
Result (12) := To_Char (SS_Nat mod 10); |
| 190 |
end if; |
| 191 |
|
| 192 |
return Result (First .. Last); |
| 193 |
end Image; |
| 194 |
|
| 195 |
----------- |
| 196 |
-- Image -- |
| 197 |
----------- |
| 198 |
|
| 199 |
function Image |
| 200 |
(Date : Time; |
| 201 |
Include_Time_Fraction : Boolean := False; |
| 202 |
Time_Zone : Time_Zones.Time_Offset := 0) return String |
| 203 |
is |
| 204 |
To_Char : constant array (0 .. 9) of Character := "0123456789"; |
| 205 |
|
| 206 |
Year : Year_Number; |
| 207 |
Month : Month_Number; |
| 208 |
Day : Day_Number; |
| 209 |
Hour : Hour_Number; |
| 210 |
Minute : Minute_Number; |
| 211 |
Second : Second_Number; |
| 212 |
Sub_Second : Duration; |
| 213 |
SS_Nat : Natural; |
| 214 |
Leap_Second : Boolean; |
| 215 |
|
| 216 |
-- The result length depends on whether fractions are requested. |
| 217 |
|
| 218 |
Result : String := "0000-00-00 00:00:00.00"; |
| 219 |
Last : constant Positive := |
| 220 |
Result'Last - (if Include_Time_Fraction then 0 else 3); |
| 221 |
|
| 222 |
begin |
| 223 |
Split (Date, Year, Month, Day, |
| 224 |
Hour, Minute, Second, Sub_Second, Leap_Second, Time_Zone); |
| 225 |
|
| 226 |
-- Year processing, positions 1, 2, 3 and 4 |
| 227 |
|
| 228 |
Result (1) := To_Char (Year / 1000); |
| 229 |
Result (2) := To_Char (Year / 100 mod 10); |
| 230 |
Result (3) := To_Char (Year / 10 mod 10); |
| 231 |
Result (4) := To_Char (Year mod 10); |
| 232 |
|
| 233 |
-- Month processing, positions 6 and 7 |
| 234 |
|
| 235 |
Result (6) := To_Char (Month / 10); |
| 236 |
Result (7) := To_Char (Month mod 10); |
| 237 |
|
| 238 |
-- Day processing, positions 9 and 10 |
| 239 |
|
| 240 |
Result (9) := To_Char (Day / 10); |
| 241 |
Result (10) := To_Char (Day mod 10); |
| 242 |
|
| 243 |
Result (12) := To_Char (Hour / 10); |
| 244 |
Result (13) := To_Char (Hour mod 10); |
| 245 |
|
| 246 |
-- Minute processing, positions 15 and 16 |
| 247 |
|
| 248 |
Result (15) := To_Char (Minute / 10); |
| 249 |
Result (16) := To_Char (Minute mod 10); |
| 250 |
|
| 251 |
-- Second processing, positions 18 and 19 |
| 252 |
|
| 253 |
Result (18) := To_Char (Second / 10); |
| 254 |
Result (19) := To_Char (Second mod 10); |
| 255 |
|
| 256 |
-- Optional sub second processing, positions 21 and 22 |
| 257 |
|
| 258 |
if Include_Time_Fraction and then Sub_Second > 0.0 then |
| 259 |
|
| 260 |
-- Prevent rounding up when converting to natural, avoiding the zero |
| 261 |
-- case to prevent rounding down to a negative number. |
| 262 |
|
| 263 |
SS_Nat := Natural (Duration'(Sub_Second * 100.0) - 0.5); |
| 264 |
|
| 265 |
Result (21) := To_Char (SS_Nat / 10); |
| 266 |
Result (22) := To_Char (SS_Nat mod 10); |
| 267 |
end if; |
| 268 |
|
| 269 |
return Result (Result'First .. Last); |
| 270 |
end Image; |
| 271 |
|
| 272 |
------------ |
| 273 |
-- Minute -- |
| 274 |
------------ |
| 275 |
|
| 276 |
function Minute |
| 277 |
(Date : Time; |
| 278 |
Time_Zone : Time_Zones.Time_Offset := 0) return Minute_Number |
| 279 |
is |
| 280 |
Y : Year_Number; |
| 281 |
Mo : Month_Number; |
| 282 |
D : Day_Number; |
| 283 |
H : Hour_Number; |
| 284 |
Mi : Minute_Number; |
| 285 |
Se : Second_Number; |
| 286 |
Ss : Second_Duration; |
| 287 |
Le : Boolean; |
| 288 |
|
| 289 |
begin |
| 290 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone); |
| 291 |
return Mi; |
| 292 |
end Minute; |
| 293 |
|
| 294 |
----------- |
| 295 |
-- Month -- |
| 296 |
----------- |
| 297 |
|
| 298 |
function Month |
| 299 |
(Date : Time; |
| 300 |
Time_Zone : Time_Zones.Time_Offset := 0) return Month_Number |
| 301 |
is |
| 302 |
Y : Year_Number; |
| 303 |
Mo : Month_Number; |
| 304 |
D : Day_Number; |
| 305 |
H : Hour_Number; |
| 306 |
Mi : Minute_Number; |
| 307 |
Se : Second_Number; |
| 308 |
Ss : Second_Duration; |
| 309 |
Le : Boolean; |
| 310 |
|
| 311 |
begin |
| 312 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone); |
| 313 |
return Mo; |
| 314 |
end Month; |
| 315 |
|
| 316 |
------------ |
| 317 |
-- Second -- |
| 318 |
------------ |
| 319 |
|
| 320 |
function Second (Date : Time) return Second_Number is |
| 321 |
Y : Year_Number; |
| 322 |
Mo : Month_Number; |
| 323 |
D : Day_Number; |
| 324 |
H : Hour_Number; |
| 325 |
Mi : Minute_Number; |
| 326 |
Se : Second_Number; |
| 327 |
Ss : Second_Duration; |
| 328 |
Le : Boolean; |
| 329 |
|
| 330 |
begin |
| 331 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le); |
| 332 |
return Se; |
| 333 |
end Second; |
| 334 |
|
| 335 |
---------------- |
| 336 |
-- Seconds_Of -- |
| 337 |
---------------- |
| 338 |
|
| 339 |
function Seconds_Of |
| 340 |
(Hour : Hour_Number; |
| 341 |
Minute : Minute_Number; |
| 342 |
Second : Second_Number := 0; |
| 343 |
Sub_Second : Second_Duration := 0.0) return Day_Duration is |
| 344 |
|
| 345 |
begin |
| 346 |
-- Validity checks |
| 347 |
|
| 348 |
if not Hour'Valid |
| 349 |
or else not Minute'Valid |
| 350 |
or else not Second'Valid |
| 351 |
or else not Sub_Second'Valid |
| 352 |
then |
| 353 |
raise Constraint_Error; |
| 354 |
end if; |
| 355 |
|
| 356 |
return Day_Duration (Hour * 3_600) + |
| 357 |
Day_Duration (Minute * 60) + |
| 358 |
Day_Duration (Second) + |
| 359 |
Sub_Second; |
| 360 |
end Seconds_Of; |
| 361 |
|
| 362 |
-------------------- |
| 363 |
-- Split_Duration -- |
| 364 |
-------------------- |
| 365 |
|
| 366 |
procedure Split_Duration |
| 367 |
(Seconds : Duration; |
| 368 |
Hour : out Natural; |
| 369 |
Minute : out Minute_Number; |
| 370 |
Second : out Second_Number; |
| 371 |
Sub_Second : out Second_Duration) |
| 372 |
is |
| 373 |
Secs : Natural; |
| 374 |
begin |
| 375 |
-- Check that Seconds is below 100 hours |
| 376 |
|
| 377 |
if Seconds >= 3600.0 * 100.0 then |
| 378 |
raise Time_Error; |
| 379 |
end if; |
| 380 |
|
| 381 |
Secs := (if Seconds = 0.0 then 0 else Natural (Seconds - 0.5)); |
| 382 |
|
| 383 |
Sub_Second := Second_Duration (Seconds - Duration (Secs)); |
| 384 |
Hour := Natural (Secs / 3_600); |
| 385 |
Secs := Secs mod 3_600; |
| 386 |
Minute := Minute_Number (Secs / 60); |
| 387 |
Second := Second_Number (Secs mod 60); |
| 388 |
end Split_Duration; |
| 389 |
|
| 390 |
----------- |
| 391 |
-- Split -- |
| 392 |
----------- |
| 393 |
|
| 394 |
procedure Split |
| 395 |
(Seconds : Day_Duration; |
| 396 |
Hour : out Hour_Number; |
| 397 |
Minute : out Minute_Number; |
| 398 |
Second : out Second_Number; |
| 399 |
Sub_Second : out Second_Duration) |
| 400 |
is |
| 401 |
Unchecked_Hour : Natural; |
| 402 |
begin |
| 403 |
-- Validity checks |
| 404 |
|
| 405 |
if not Seconds'Valid then |
| 406 |
raise Constraint_Error; |
| 407 |
end if; |
| 408 |
|
| 409 |
Split_Duration (Seconds, Unchecked_Hour, Minute, Second, Sub_Second); |
| 410 |
|
| 411 |
if Unchecked_Hour > Hour_Number'Last then |
| 412 |
raise Time_Error; |
| 413 |
end if; |
| 414 |
|
| 415 |
Hour := Unchecked_Hour; |
| 416 |
end Split; |
| 417 |
|
| 418 |
----------- |
| 419 |
-- Split -- |
| 420 |
----------- |
| 421 |
|
| 422 |
procedure Split |
| 423 |
(Date : Time; |
| 424 |
Year : out Year_Number; |
| 425 |
Month : out Month_Number; |
| 426 |
Day : out Day_Number; |
| 427 |
Seconds : out Day_Duration; |
| 428 |
Leap_Second : out Boolean; |
| 429 |
Time_Zone : Time_Zones.Time_Offset := 0) |
| 430 |
is |
| 431 |
H : Integer; |
| 432 |
M : Integer; |
| 433 |
Se : Integer; |
| 434 |
Su : Duration; |
| 435 |
Tz : constant Long_Integer := Long_Integer (Time_Zone); |
| 436 |
|
| 437 |
begin |
| 438 |
Formatting_Operations.Split |
| 439 |
(Date => Date, |
| 440 |
Year => Year, |
| 441 |
Month => Month, |
| 442 |
Day => Day, |
| 443 |
Day_Secs => Seconds, |
| 444 |
Hour => H, |
| 445 |
Minute => M, |
| 446 |
Second => Se, |
| 447 |
Sub_Sec => Su, |
| 448 |
Leap_Sec => Leap_Second, |
| 449 |
Use_TZ => True, |
| 450 |
Is_Historic => True, |
| 451 |
Time_Zone => Tz); |
| 452 |
|
| 453 |
-- Validity checks |
| 454 |
|
| 455 |
if not Year'Valid |
| 456 |
or else not Month'Valid |
| 457 |
or else not Day'Valid |
| 458 |
or else not Seconds'Valid |
| 459 |
then |
| 460 |
raise Time_Error; |
| 461 |
end if; |
| 462 |
end Split; |
| 463 |
|
| 464 |
----------- |
| 465 |
-- Split -- |
| 466 |
----------- |
| 467 |
|
| 468 |
procedure Split |
| 469 |
(Date : Time; |
| 470 |
Year : out Year_Number; |
| 471 |
Month : out Month_Number; |
| 472 |
Day : out Day_Number; |
| 473 |
Hour : out Hour_Number; |
| 474 |
Minute : out Minute_Number; |
| 475 |
Second : out Second_Number; |
| 476 |
Sub_Second : out Second_Duration; |
| 477 |
Time_Zone : Time_Zones.Time_Offset := 0) |
| 478 |
is |
| 479 |
Dd : Day_Duration; |
| 480 |
Le : Boolean; |
| 481 |
Tz : constant Long_Integer := Long_Integer (Time_Zone); |
| 482 |
|
| 483 |
begin |
| 484 |
Formatting_Operations.Split |
| 485 |
(Date => Date, |
| 486 |
Year => Year, |
| 487 |
Month => Month, |
| 488 |
Day => Day, |
| 489 |
Day_Secs => Dd, |
| 490 |
Hour => Hour, |
| 491 |
Minute => Minute, |
| 492 |
Second => Second, |
| 493 |
Sub_Sec => Sub_Second, |
| 494 |
Leap_Sec => Le, |
| 495 |
Use_TZ => True, |
| 496 |
Is_Historic => True, |
| 497 |
Time_Zone => Tz); |
| 498 |
|
| 499 |
-- Validity checks |
| 500 |
|
| 501 |
if not Year'Valid |
| 502 |
or else not Month'Valid |
| 503 |
or else not Day'Valid |
| 504 |
or else not Hour'Valid |
| 505 |
or else not Minute'Valid |
| 506 |
or else not Second'Valid |
| 507 |
or else not Sub_Second'Valid |
| 508 |
then |
| 509 |
raise Time_Error; |
| 510 |
end if; |
| 511 |
end Split; |
| 512 |
|
| 513 |
----------- |
| 514 |
-- Split -- |
| 515 |
----------- |
| 516 |
|
| 517 |
procedure Split |
| 518 |
(Date : Time; |
| 519 |
Year : out Year_Number; |
| 520 |
Month : out Month_Number; |
| 521 |
Day : out Day_Number; |
| 522 |
Hour : out Hour_Number; |
| 523 |
Minute : out Minute_Number; |
| 524 |
Second : out Second_Number; |
| 525 |
Sub_Second : out Second_Duration; |
| 526 |
Leap_Second : out Boolean; |
| 527 |
Time_Zone : Time_Zones.Time_Offset := 0) |
| 528 |
is |
| 529 |
Dd : Day_Duration; |
| 530 |
Tz : constant Long_Integer := Long_Integer (Time_Zone); |
| 531 |
|
| 532 |
begin |
| 533 |
Formatting_Operations.Split |
| 534 |
(Date => Date, |
| 535 |
Year => Year, |
| 536 |
Month => Month, |
| 537 |
Day => Day, |
| 538 |
Day_Secs => Dd, |
| 539 |
Hour => Hour, |
| 540 |
Minute => Minute, |
| 541 |
Second => Second, |
| 542 |
Sub_Sec => Sub_Second, |
| 543 |
Leap_Sec => Leap_Second, |
| 544 |
Use_TZ => True, |
| 545 |
Is_Historic => True, |
| 546 |
Time_Zone => Tz); |
| 547 |
|
| 548 |
-- Validity checks |
| 549 |
|
| 550 |
if not Year'Valid |
| 551 |
or else not Month'Valid |
| 552 |
or else not Day'Valid |
| 553 |
or else not Hour'Valid |
| 554 |
or else not Minute'Valid |
| 555 |
or else not Second'Valid |
| 556 |
or else not Sub_Second'Valid |
| 557 |
then |
| 558 |
raise Time_Error; |
| 559 |
end if; |
| 560 |
end Split; |
| 561 |
|
| 562 |
---------------- |
| 563 |
-- Sub_Second -- |
| 564 |
---------------- |
| 565 |
|
| 566 |
function Sub_Second (Date : Time) return Second_Duration is |
| 567 |
Y : Year_Number; |
| 568 |
Mo : Month_Number; |
| 569 |
D : Day_Number; |
| 570 |
H : Hour_Number; |
| 571 |
Mi : Minute_Number; |
| 572 |
Se : Second_Number; |
| 573 |
Ss : Second_Duration; |
| 574 |
Le : Boolean; |
| 575 |
|
| 576 |
begin |
| 577 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le); |
| 578 |
return Ss; |
| 579 |
end Sub_Second; |
| 580 |
|
| 581 |
------------- |
| 582 |
-- Time_Of -- |
| 583 |
------------- |
| 584 |
|
| 585 |
function Time_Of |
| 586 |
(Year : Year_Number; |
| 587 |
Month : Month_Number; |
| 588 |
Day : Day_Number; |
| 589 |
Seconds : Day_Duration := 0.0; |
| 590 |
Leap_Second : Boolean := False; |
| 591 |
Time_Zone : Time_Zones.Time_Offset := 0) return Time |
| 592 |
is |
| 593 |
H : constant Integer := 1; |
| 594 |
M : constant Integer := 1; |
| 595 |
Se : constant Integer := 1; |
| 596 |
Ss : constant Duration := 0.1; |
| 597 |
Tz : constant Long_Integer := Long_Integer (Time_Zone); |
| 598 |
|
| 599 |
begin |
| 600 |
-- Validity checks |
| 601 |
|
| 602 |
if not Year'Valid |
| 603 |
or else not Month'Valid |
| 604 |
or else not Day'Valid |
| 605 |
or else not Seconds'Valid |
| 606 |
or else not Time_Zone'Valid |
| 607 |
then |
| 608 |
raise Constraint_Error; |
| 609 |
end if; |
| 610 |
|
| 611 |
return |
| 612 |
Formatting_Operations.Time_Of |
| 613 |
(Year => Year, |
| 614 |
Month => Month, |
| 615 |
Day => Day, |
| 616 |
Day_Secs => Seconds, |
| 617 |
Hour => H, |
| 618 |
Minute => M, |
| 619 |
Second => Se, |
| 620 |
Sub_Sec => Ss, |
| 621 |
Leap_Sec => Leap_Second, |
| 622 |
Use_Day_Secs => True, |
| 623 |
Use_TZ => True, |
| 624 |
Is_Historic => True, |
| 625 |
Time_Zone => Tz); |
| 626 |
end Time_Of; |
| 627 |
|
| 628 |
------------- |
| 629 |
-- Time_Of -- |
| 630 |
------------- |
| 631 |
|
| 632 |
function Time_Of |
| 633 |
(Year : Year_Number; |
| 634 |
Month : Month_Number; |
| 635 |
Day : Day_Number; |
| 636 |
Hour : Hour_Number; |
| 637 |
Minute : Minute_Number; |
| 638 |
Second : Second_Number; |
| 639 |
Sub_Second : Second_Duration := 0.0; |
| 640 |
Leap_Second : Boolean := False; |
| 641 |
Time_Zone : Time_Zones.Time_Offset := 0) return Time |
| 642 |
is |
| 643 |
Dd : constant Day_Duration := Day_Duration'First; |
| 644 |
Tz : constant Long_Integer := Long_Integer (Time_Zone); |
| 645 |
|
| 646 |
begin |
| 647 |
-- Validity checks |
| 648 |
|
| 649 |
if not Year'Valid |
| 650 |
or else not Month'Valid |
| 651 |
or else not Day'Valid |
| 652 |
or else not Hour'Valid |
| 653 |
or else not Minute'Valid |
| 654 |
or else not Second'Valid |
| 655 |
or else not Sub_Second'Valid |
| 656 |
or else not Time_Zone'Valid |
| 657 |
then |
| 658 |
raise Constraint_Error; |
| 659 |
end if; |
| 660 |
|
| 661 |
return |
| 662 |
Formatting_Operations.Time_Of |
| 663 |
(Year => Year, |
| 664 |
Month => Month, |
| 665 |
Day => Day, |
| 666 |
Day_Secs => Dd, |
| 667 |
Hour => Hour, |
| 668 |
Minute => Minute, |
| 669 |
Second => Second, |
| 670 |
Sub_Sec => Sub_Second, |
| 671 |
Leap_Sec => Leap_Second, |
| 672 |
Use_Day_Secs => False, |
| 673 |
Use_TZ => True, |
| 674 |
Is_Historic => True, |
| 675 |
Time_Zone => Tz); |
| 676 |
end Time_Of; |
| 677 |
|
| 678 |
----------- |
| 679 |
-- Value -- |
| 680 |
----------- |
| 681 |
|
| 682 |
function Value |
| 683 |
(Date : String; |
| 684 |
Time_Zone : Time_Zones.Time_Offset := 0) return Time |
| 685 |
is |
| 686 |
D : String (1 .. 22); |
| 687 |
Year : Year_Number; |
| 688 |
Month : Month_Number; |
| 689 |
Day : Day_Number; |
| 690 |
Hour : Hour_Number; |
| 691 |
Minute : Minute_Number; |
| 692 |
Second : Second_Number; |
| 693 |
Sub_Second : Second_Duration := 0.0; |
| 694 |
|
| 695 |
begin |
| 696 |
-- Validity checks |
| 697 |
|
| 698 |
if not Time_Zone'Valid then |
| 699 |
raise Constraint_Error; |
| 700 |
end if; |
| 701 |
|
| 702 |
-- Length checks |
| 703 |
|
| 704 |
if Date'Length /= 19 |
| 705 |
and then Date'Length /= 22 |
| 706 |
then |
| 707 |
raise Constraint_Error; |
| 708 |
end if; |
| 709 |
|
| 710 |
-- After the correct length has been determined, it is safe to copy the |
| 711 |
-- Date in order to avoid Date'First + N indexing. |
| 712 |
|
| 713 |
D (1 .. Date'Length) := Date; |
| 714 |
|
| 715 |
-- Format checks |
| 716 |
|
| 717 |
Check_Char (D, '-', 5); |
| 718 |
Check_Char (D, '-', 8); |
| 719 |
Check_Char (D, ' ', 11); |
| 720 |
Check_Char (D, ':', 14); |
| 721 |
Check_Char (D, ':', 17); |
| 722 |
|
| 723 |
if Date'Length = 22 then |
| 724 |
Check_Char (D, '.', 20); |
| 725 |
end if; |
| 726 |
|
| 727 |
-- Leading zero checks |
| 728 |
|
| 729 |
Check_Digit (D, 6); |
| 730 |
Check_Digit (D, 9); |
| 731 |
Check_Digit (D, 12); |
| 732 |
Check_Digit (D, 15); |
| 733 |
Check_Digit (D, 18); |
| 734 |
|
| 735 |
if Date'Length = 22 then |
| 736 |
Check_Digit (D, 21); |
| 737 |
end if; |
| 738 |
|
| 739 |
-- Value extraction |
| 740 |
|
| 741 |
Year := Year_Number (Year_Number'Value (D (1 .. 4))); |
| 742 |
Month := Month_Number (Month_Number'Value (D (6 .. 7))); |
| 743 |
Day := Day_Number (Day_Number'Value (D (9 .. 10))); |
| 744 |
Hour := Hour_Number (Hour_Number'Value (D (12 .. 13))); |
| 745 |
Minute := Minute_Number (Minute_Number'Value (D (15 .. 16))); |
| 746 |
Second := Second_Number (Second_Number'Value (D (18 .. 19))); |
| 747 |
|
| 748 |
-- Optional part |
| 749 |
|
| 750 |
if Date'Length = 22 then |
| 751 |
Sub_Second := Second_Duration (Second_Duration'Value (D (20 .. 22))); |
| 752 |
end if; |
| 753 |
|
| 754 |
-- Sanity checks |
| 755 |
|
| 756 |
if not Year'Valid |
| 757 |
or else not Month'Valid |
| 758 |
or else not Day'Valid |
| 759 |
or else not Hour'Valid |
| 760 |
or else not Minute'Valid |
| 761 |
or else not Second'Valid |
| 762 |
or else not Sub_Second'Valid |
| 763 |
then |
| 764 |
raise Constraint_Error; |
| 765 |
end if; |
| 766 |
|
| 767 |
return Time_Of (Year, Month, Day, |
| 768 |
Hour, Minute, Second, Sub_Second, False, Time_Zone); |
| 769 |
|
| 770 |
exception |
| 771 |
when others => raise Constraint_Error; |
| 772 |
end Value; |
| 773 |
|
| 774 |
----------- |
| 775 |
-- Value -- |
| 776 |
----------- |
| 777 |
|
| 778 |
function Value (Elapsed_Time : String) return Duration is |
| 779 |
D : String (1 .. 11); |
| 780 |
Hour : Natural; |
| 781 |
Minute : Minute_Number; |
| 782 |
Second : Second_Number; |
| 783 |
Sub_Second : Second_Duration := 0.0; |
| 784 |
|
| 785 |
begin |
| 786 |
-- Length checks |
| 787 |
|
| 788 |
if Elapsed_Time'Length /= 8 |
| 789 |
and then Elapsed_Time'Length /= 11 |
| 790 |
then |
| 791 |
raise Constraint_Error; |
| 792 |
end if; |
| 793 |
|
| 794 |
-- After the correct length has been determined, it is safe to copy the |
| 795 |
-- Elapsed_Time in order to avoid Date'First + N indexing. |
| 796 |
|
| 797 |
D (1 .. Elapsed_Time'Length) := Elapsed_Time; |
| 798 |
|
| 799 |
-- Format checks |
| 800 |
|
| 801 |
Check_Char (D, ':', 3); |
| 802 |
Check_Char (D, ':', 6); |
| 803 |
|
| 804 |
if Elapsed_Time'Length = 11 then |
| 805 |
Check_Char (D, '.', 9); |
| 806 |
end if; |
| 807 |
|
| 808 |
-- Leading zero checks |
| 809 |
|
| 810 |
Check_Digit (D, 1); |
| 811 |
Check_Digit (D, 4); |
| 812 |
Check_Digit (D, 7); |
| 813 |
|
| 814 |
if Elapsed_Time'Length = 11 then |
| 815 |
Check_Digit (D, 10); |
| 816 |
end if; |
| 817 |
|
| 818 |
-- Value extraction |
| 819 |
|
| 820 |
Hour := Natural (Natural'Value (D (1 .. 2))); |
| 821 |
Minute := Minute_Number (Minute_Number'Value (D (4 .. 5))); |
| 822 |
Second := Second_Number (Second_Number'Value (D (7 .. 8))); |
| 823 |
|
| 824 |
-- Optional part |
| 825 |
|
| 826 |
if Elapsed_Time'Length = 11 then |
| 827 |
Sub_Second := Second_Duration (Second_Duration'Value (D (9 .. 11))); |
| 828 |
end if; |
| 829 |
|
| 830 |
-- Sanity checks |
| 831 |
|
| 832 |
if not Hour'Valid |
| 833 |
or else not Minute'Valid |
| 834 |
or else not Second'Valid |
| 835 |
or else not Sub_Second'Valid |
| 836 |
then |
| 837 |
raise Constraint_Error; |
| 838 |
end if; |
| 839 |
|
| 840 |
return Duration (Hour * 3600) |
| 841 |
+ Duration (Minute * 60) |
| 842 |
+ Duration (Second) |
| 843 |
+ Sub_Second; |
| 844 |
|
| 845 |
exception |
| 846 |
-- CE is mandated, but preserve trace if CE already. |
| 847 |
when Constraint_Error => raise; |
| 848 |
when others => raise Constraint_Error; |
| 849 |
end Value; |
| 850 |
|
| 851 |
---------- |
| 852 |
-- Year -- |
| 853 |
---------- |
| 854 |
|
| 855 |
function Year |
| 856 |
(Date : Time; |
| 857 |
Time_Zone : Time_Zones.Time_Offset := 0) return Year_Number |
| 858 |
is |
| 859 |
Y : Year_Number; |
| 860 |
Mo : Month_Number; |
| 861 |
D : Day_Number; |
| 862 |
H : Hour_Number; |
| 863 |
Mi : Minute_Number; |
| 864 |
Se : Second_Number; |
| 865 |
Ss : Second_Duration; |
| 866 |
Le : Boolean; |
| 867 |
|
| 868 |
begin |
| 869 |
Split (Date, Y, Mo, D, H, Mi, Se, Ss, Le, Time_Zone); |
| 870 |
return Y; |
| 871 |
end Year; |
| 872 |
|
| 873 |
end Ada.Calendar.Formatting; |