summaryrefslogtreecommitdiff
path: root/src/simple_time.adb
blob: f206f13df590dada78aaebc7c71d46849975a54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59


with

    Ada.Calendar,
    Ada.Strings.Fixed;

use

    Ada.Calendar,
    Ada.Strings,
    Ada.Strings.Fixed;


package body Simple_Time is


    function Image
           (Moment : in Time)
        return String
    is
        Raw_Secs : Day_Duration := Seconds (Moment);

        Hours   : Integer := Integer (Raw_Secs) / 3600;
        Minutes : Integer := (Integer (Raw_Secs) - Hours * 3600) / 60;
        Seconds : Duration := Raw_Secs - Duration (Hours) * 3600 - Duration (Minutes) * 60;

        Year_Str : String (1 .. 4);
        Month_Str, Day_Str, Hour_Str, Minute_Str : String (1 .. 2);
        Second_Str : String (1 .. 9);
    begin
        Move (Trim (Year_Number'Image  (Year (Moment)),  Left), Year_Str,   Left, Right, '0');
        Move (Trim (Month_Number'Image (Month (Moment)), Left), Month_Str,  Left, Right, '0');
        Move (Trim (Day_Number'Image   (Day (Moment)),   Left), Day_Str,    Left, Right, '0');
        Move (Trim (Integer'Image      (Hours),          Left), Hour_Str,   Left, Right, '0');
        Move (Trim (Integer'Image      (Minutes),        Left), Minute_Str, Left, Right, '0');
        Move (Image (Seconds), Second_Str, Left, Right, '0');
        return Year_Str & '-' & Month_Str & '-' & Day_Str & " " &
                Hour_Str & ':' & Minute_Str & ':' & Second_Str;
    end Image;




    function Image
           (Moment : in Duration)
        return String
    is
        Duration_Trim : String := Trim (Duration'Image (Moment), Left);
    begin
        --  Drop unnecessary trailing zeros, of which there are always three.
        --  Is this a Linux-specific thing? Probably.
        return Duration_Trim (1 .. (Duration_Trim'Length - 3));
    end Image;


end Simple_Time;