Natools

Check-in [a3f3d5c90c]
Login
Overview
Comment:time_keys: fix subsecond rounding bug
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a3f3d5c90ced9b73a69ff88cc965ebbcb656533a
User & Date: nat on 2015-04-13 18:15:46
Other Links: manifest | tags
Context
2015-05-02
14:04
printers: add a new class-wide procedure to append strings, which often improves readibility check-in: 94f83b5035 user: nat tags: trunk
2015-04-13
18:15
time_keys: fix subsecond rounding bug check-in: a3f3d5c90c user: nat tags: trunk
2015-04-11
21:09
time_keys-tests: add a test showing a subsecond rounding bug check-in: d3c251409e user: nat tags: trunk
Changes

Modified src/natools-time_keys.adb from [b24ddd3065] to [a46b79791f].

10
11
12
13
14
15
16
17

18
19
20
21
22
23
24
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24







-
+







-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

with Ada.Calendar.Formatting;
with Ada.Calendar.Arithmetic;

package body Natools.Time_Keys is

   function Extract_Sub_Second (Key : String) return Duration;
      --  Read the end of Buffer and compute the Sub_Second part


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67






68



















































69
70
71
72
73
74
75
44
45
46
47
48
49
50



51
52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128







-
-
-







-






+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







   -----------------------

   function To_Key
     (Time : Ada.Calendar.Time;
      Max_Sub_Second_Digits : in Natural := 120)
     return String
   is
      Buffer : String (1 .. 7 + Max_Sub_Second_Digits);
      Last : Positive;
      N : Natural;
      Year : Ada.Calendar.Year_Number;
      Month : Ada.Calendar.Month_Number;
      Day : Ada.Calendar.Day_Number;
      Hour : Ada.Calendar.Formatting.Hour_Number;
      Minute : Ada.Calendar.Formatting.Minute_Number;
      Second : Ada.Calendar.Formatting.Second_Number;
      Sub_Second : Ada.Calendar.Formatting.Second_Duration;
      D, Base : Duration;
      Leap_Second : Boolean;
   begin
      Ada.Calendar.Formatting.Split
        (Time,
         Year, Month, Day, Hour, Minute, Second, Sub_Second,
         Leap_Second);
      return To_Key
        (Year, Month, Day,
         Hour, Minute, Second, Sub_Second,
         Leap_Second,
         Max_Sub_Second_Digits);
   end To_Key;


   function To_Key
     (Year : Ada.Calendar.Year_Number;
      Month : Ada.Calendar.Month_Number;
      Day : Ada.Calendar.Day_Number;
      Hour : Ada.Calendar.Formatting.Hour_Number := 0;
      Minute : Ada.Calendar.Formatting.Minute_Number := 0;
      Second : Ada.Calendar.Formatting.Second_Number := 0;
      Sub_Second : Ada.Calendar.Formatting.Second_Duration := 0.0;
      Leap_Second : Boolean := False;
      Max_Sub_Second_Digits : Natural := 120)
     return String
   is
      procedure Increment_Buffer;

      Buffer : String (1 .. 7 + Max_Sub_Second_Digits);
      Last : Positive;

      procedure Increment_Buffer is
      begin
         while Last > 7 and then Buffer (Last) = '~' loop
            Last := Last - 1;
         end loop;

         if Last > 7 then
            Buffer (Last) := Image (Value (Buffer (Last)) + 1);
            return;
         end if;

         if Second <= 58 then
            Buffer (7) := I_Image (Second + 1);
            Last := 7;

         elsif Minute <= 58 then
            Buffer (6) := I_Image (Minute + 1);
            Last := 6;

         elsif Hour <= 22 then
            Buffer (5) := I_Image (Hour + 1);
            Last := 5;

         else
            Buffer (1 .. 4) := To_Key (Ada.Calendar.Arithmetic."+"
              (Ada.Calendar.Formatting.Time_Of (Year, Month, Day), 1));
            Last := 4;
         end if;
      end Increment_Buffer;

      N : Natural;
      D, Base : Duration;
   begin
      Buffer (1) := I_Image (Year / 64);
      Buffer (2) := I_Image (Year mod 64);
      Buffer (3) := I_Image (Month);
      Buffer (4) := I_Image (Day);
      Buffer (5) := I_Image (Hour);
      Buffer (6) := I_Image (Minute);

101
102
103
104
105
106
107

108





109
110
111
112
113
114
115
154
155
156
157
158
159
160
161

162
163
164
165
166
167
168
169
170
171
172
173







+
-
+
+
+
+
+







      Base := 1.0;
      loop
         Last := Last + 1;
         Base := Base / 64.0;
         N := Natural (D);

         if Last = Buffer'Last or Base = 0.0 then
            if N < 64 then
            Buffer (Last) := I_Image (N);
               Buffer (Last) := I_Image (N);
            else
               Last := Last - 1;
               Increment_Buffer;
            end if;
            exit;
         end if;

         if Duration (N) > D then
            N := N - 1;
            pragma Assert (Duration (N) <= D);
         end if;

Modified src/natools-time_keys.ads from [fbf3e16aff] to [37f99cf248].

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
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
60
61
62
63
64
65
66
67
68







-
+















+
+
+
+
+
+
+
+
+
+
+
+
+
+







-- This means a second granularity can be achieved with 7 characters. The   --
-- most compact way of encoding such a timestamp would be counting seconds, --
-- like UNIX time. The time covered by this format is rought 2^37 seconds,  --
-- which would mean 5 bytes or 7 base-64 digits (though 6 would be enough   --
-- for a useful time range).                                                --
------------------------------------------------------------------------------

with Ada.Calendar;
with Ada.Calendar.Formatting;

package Natools.Time_Keys is

   function Is_Valid (Key : String) return Boolean;
      --  Check whether Key is a valid encoded time.
      --  WARNING: this function returns true for invalid dates,
      --  like February 30th.

   function To_Key
     (Time : Ada.Calendar.Time;
      Max_Sub_Second_Digits : in Natural := 120)
     return String
     with Post => Is_Valid (To_Key'Result);
      --  Convert a time into a key

   function To_Key
     (Year : Ada.Calendar.Year_Number;
      Month : Ada.Calendar.Month_Number;
      Day : Ada.Calendar.Day_Number;
      Hour : Ada.Calendar.Formatting.Hour_Number := 0;
      Minute : Ada.Calendar.Formatting.Minute_Number := 0;
      Second : Ada.Calendar.Formatting.Second_Number := 0;
      Sub_Second : Ada.Calendar.Formatting.Second_Duration := 0.0;
      Leap_Second : Boolean := False;
      Max_Sub_Second_Digits : Natural := 120)
     return String
     with Post => Is_Valid (To_Key'Result);
      --  Convert a split time representation into a key

   function To_Time (Key : String) return Ada.Calendar.Time
     with Pre => Is_Valid (Key);
      --  Convert a valid key into the original time

private

   subtype Base_64_Digit is Character with Static_Predicate