Natools

Check-in [21534f262f]
Login
Overview
Comment:time_io-rfc_3339: add extra arguments to work around implementations without leap second support
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 21534f262f497758fa31d0c337963132147f9814
User & Date: nat on 2014-08-20 19:49:24
Other Links: manifest | tags
Context
2014-08-21
20:52
time_io-tests: add test for leap second workaround on implementations that don't support them check-in: ba34be19be user: nat tags: trunk
2014-08-20
19:49
time_io-rfc_3339: add extra arguments to work around implementations without leap second support check-in: 21534f262f user: nat tags: trunk
2014-08-19
21:09
time_io-tests: check leap second support before using it in tests check-in: 03c5f8582b user: nat tags: trunk
Changes

Modified src/natools-time_io-rfc_3339.adb from [254f3dab5b] to [aba9d550db].

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

   --------------------
   -- Time To String --
   --------------------

   function Image
     (Date : Ada.Calendar.Time;
      Subsecond_Digits : Natural := 0)

     return String is
   begin
      return Image
        (Date,
         Ada.Calendar.Time_Zones.UTC_Time_Offset (Date),
         Subsecond_Digits);

   end Image;


   function Image
     (Date : Ada.Calendar.Time;
      Time_Zone : Ada.Calendar.Time_Zones.Time_Offset;
      Subsecond_Digits : Natural := 0)

     return String
   is
      function Subsecond_Image
        (Subsecond : Ada.Calendar.Formatting.Second_Duration)
        return String;

      function Time_Zone_Image return String;







|
>





|
>






|
>







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

   --------------------
   -- Time To String --
   --------------------

   function Image
     (Date : Ada.Calendar.Time;
      Subsecond_Digits : Natural := 0;
      Force_Leap_Second : Boolean := False)
     return String is
   begin
      return Image
        (Date,
         Ada.Calendar.Time_Zones.UTC_Time_Offset (Date),
         Subsecond_Digits,
         Force_Leap_Second);
   end Image;


   function Image
     (Date : Ada.Calendar.Time;
      Time_Zone : Ada.Calendar.Time_Zones.Time_Offset;
      Subsecond_Digits : Natural := 0;
      Force_Leap_Second : Boolean := False)
     return String
   is
      function Subsecond_Image
        (Subsecond : Ada.Calendar.Formatting.Second_Duration)
        return String;

      function Time_Zone_Image return String;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
        (Date,
         Year, Month, Day,
         Hour, Minute, Second,
         Subsecond,
         Leap_Second,
         Time_Zone);

      if Leap_Second then
         pragma Assert (Second = 59);
         Used_Second := 60;
      else
         Used_Second := Second;
      end if;

      return







|







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
        (Date,
         Year, Month, Day,
         Hour, Minute, Second,
         Subsecond,
         Leap_Second,
         Time_Zone);

      if Leap_Second or Force_Leap_Second then
         pragma Assert (Second = 59);
         Used_Second := 60;
      else
         Used_Second := Second;
      end if;

      return
227
228
229
230
231
232
233












234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

258
259
260
261
262
263
264


   procedure Value
     (Image : in String;
      Date : out Ada.Calendar.Time;
      Time_Zone : out Ada.Calendar.Time_Zones.Time_Offset)
   is












      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;
      Subsecond : Ada.Calendar.Formatting.Second_Duration := 0.0;
      Leap_Second : Boolean;
   begin
      Year := Natural'Value (Image (Image'First .. Image'First + 3));
      Month := Natural'Value (Image (Image'First + 5 .. Image'First + 6));
      Day := Natural'Value (Image (Image'First + 8 .. Image'First + 9));
      Hour := Natural'Value (Image (Image'First + 11 .. Image'First + 12));
      Minute := Natural'Value
        (Image (Image'First + 14 .. Image'First + 15));

      declare
         Number : constant Natural
           := Natural'Value (Image (Image'First + 17 .. Image'First + 18));
      begin
         if Number = 60 then
            Leap_Second := True;
            Second := 59;
         else

            Second := Number;
         end if;
      end;

      if Image (Image'First + 19) = Subsecond_Separator then
         declare
            I : Positive := Image'First + 20;







>
>
>
>
>
>
>
>
>
>
>
>







<
















>







230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279


   procedure Value
     (Image : in String;
      Date : out Ada.Calendar.Time;
      Time_Zone : out Ada.Calendar.Time_Zones.Time_Offset)
   is
      Discarded : Boolean;
   begin
      Value (Image, Date, Time_Zone, Discarded);
   end Value;


   procedure Value
     (Image : in String;
      Date : out Ada.Calendar.Time;
      Time_Zone : out Ada.Calendar.Time_Zones.Time_Offset;
      Leap_Second : out Boolean)
   is
      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;
      Subsecond : Ada.Calendar.Formatting.Second_Duration := 0.0;

   begin
      Year := Natural'Value (Image (Image'First .. Image'First + 3));
      Month := Natural'Value (Image (Image'First + 5 .. Image'First + 6));
      Day := Natural'Value (Image (Image'First + 8 .. Image'First + 9));
      Hour := Natural'Value (Image (Image'First + 11 .. Image'First + 12));
      Minute := Natural'Value
        (Image (Image'First + 14 .. Image'First + 15));

      declare
         Number : constant Natural
           := Natural'Value (Image (Image'First + 17 .. Image'First + 18));
      begin
         if Number = 60 then
            Leap_Second := True;
            Second := 59;
         else
            Leap_Second := False;
            Second := Number;
         end if;
      end;

      if Image (Image'First + 19) = Subsecond_Separator then
         declare
            I : Positive := Image'First + 20;
285
286
287
288
289
290
291













292
293
294
295
296
297
298
299
            when '+' =>
               null;
            when others =>
               raise Constraint_Error
                 with "Invalid time zone separator in RFC 3339 date";
         end case;
      end if;














      Date := Ada.Calendar.Formatting.Time_Of
        (Year, Month, Day,
         Hour, Minute, Second,
         Subsecond, Leap_Second, Time_Zone);
   end Value;

end Natools.Time_IO.RFC_3339;







>
>
>
>
>
>
>
>
>
>
>
>
>




|



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
            when '+' =>
               null;
            when others =>
               raise Constraint_Error
                 with "Invalid time zone separator in RFC 3339 date";
         end case;
      end if;

      if Leap_Second then
         begin
            Date := Ada.Calendar.Formatting.Time_Of
              (Year, Month, Day,
               Hour, Minute, Second,
               Subsecond, True, Time_Zone);
            return;
         exception
            when Ada.Calendar.Time_Error =>
               null;
         end;
      end if;

      Date := Ada.Calendar.Formatting.Time_Of
        (Year, Month, Day,
         Hour, Minute, Second,
         Subsecond, False, Time_Zone);
   end Value;

end Natools.Time_IO.RFC_3339;

Modified src/natools-time_io-rfc_3339.ads from [25471d80ad] to [d3cf5765f6].

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
   Subsecond_Separator : constant Character := '.';

   function Is_Valid (Image : String) return Boolean;
      --  Check whether Image is a valid RFC-3339 date

   function Image
     (Date : Ada.Calendar.Time;
      Subsecond_Digits : Natural := 0)

     return String
     with Post => Is_Valid (Image'Result);
      --  Return the RFC-3339 representation of Date in current time zone

   function Image
     (Date : Ada.Calendar.Time;
      Time_Zone : Ada.Calendar.Time_Zones.Time_Offset;
      Subsecond_Digits : Natural := 0)

     return String
     with Post => Is_Valid (Image'Result);
      --  Return the RFC-3339 representation of Date in Time_Zone

   function Value (Image : String) return Ada.Calendar.Time
     with Pre => Is_Valid (Image) or else raise Constraint_Error;
      --  Return the time associated with the given RFC-3339 representation

   procedure Value
     (Image : in String;
      Date : out Ada.Calendar.Time;
      Time_Zone : out Ada.Calendar.Time_Zones.Time_Offset)








     with Pre => Is_Valid (Image) or else raise Constraint_Error;
      --  Return the time associated with the given RFC-3339 representation

end Natools.Time_IO.RFC_3339;







|
>







|
>












>
>
>
>
>
>
>
>




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
69
70
71
   Subsecond_Separator : constant Character := '.';

   function Is_Valid (Image : String) return Boolean;
      --  Check whether Image is a valid RFC-3339 date

   function Image
     (Date : Ada.Calendar.Time;
      Subsecond_Digits : Natural := 0;
      Force_Leap_Second : Boolean := False)
     return String
     with Post => Is_Valid (Image'Result);
      --  Return the RFC-3339 representation of Date in current time zone

   function Image
     (Date : Ada.Calendar.Time;
      Time_Zone : Ada.Calendar.Time_Zones.Time_Offset;
      Subsecond_Digits : Natural := 0;
      Force_Leap_Second : Boolean := False)
     return String
     with Post => Is_Valid (Image'Result);
      --  Return the RFC-3339 representation of Date in Time_Zone

   function Value (Image : String) return Ada.Calendar.Time
     with Pre => Is_Valid (Image) or else raise Constraint_Error;
      --  Return the time associated with the given RFC-3339 representation

   procedure Value
     (Image : in String;
      Date : out Ada.Calendar.Time;
      Time_Zone : out Ada.Calendar.Time_Zones.Time_Offset)
     with Pre => Is_Valid (Image) or else raise Constraint_Error;
      --  Return the time associated with the given RFC-3339 representation

   procedure Value
     (Image : in String;
      Date : out Ada.Calendar.Time;
      Time_Zone : out Ada.Calendar.Time_Zones.Time_Offset;
      Leap_Second : out Boolean)
     with Pre => Is_Valid (Image) or else raise Constraint_Error;
      --  Return the time associated with the given RFC-3339 representation

end Natools.Time_IO.RFC_3339;