[m-rev.] for review: print formatted dates and durations with io.print

Julien Fischer jfischer at opturion.com
Mon Dec 22 11:11:40 AEDT 2014


Print formatted dates and durations with io.print.

Change the io.print family of predicates to print out the formatted version of
dates and durations rather than their underlying representation.

library/io.m:
library/stream.string_writer.m:
 	As above.

 	Avoid a change of tense in the middle of the descriptive comment
 	for {io,stream.string_writer}.print.

tests/hard_coded/Mmakefile:
tests/hard_coded/print_date.{m,exp}:
 	Add a test for the above.

Julien.

diff --git a/library/io.m b/library/io.m
index 498a532..238d36b 100644
--- a/library/io.m
+++ b/library/io.m
@@ -388,8 +388,12 @@
      % If the argument is just a single string or character, it will be printed
      % out exactly as is (unquoted).  If the argument is of type integer (i.e.
      % an arbitrary precision integer), then its decimal representation will be
-    % printed.  If the argument is of type univ, then it will print out the
-    % value stored in the univ, but not the type.
+    % printed.  If the argument is of type univ, then the value stored in the
+    % the univ will be printed out, but not the type.  If the argument is of
+    % type date_time, it will be printed out in the same form as the string
+    % returned by the function date_to_string/1.  If the argument is of type
+    % duration, it will be printed out in the same form as the string
+    % returned by the function duration_to_string/1.
      %
      % print/5 is the same as print/4 except that it allows the caller to
      % specify how non-canonical types should be handled. print/3 and
diff --git a/library/stream.string_writer.m b/library/stream.string_writer.m
index 88e1be2..88c439d 100644
--- a/library/stream.string_writer.m
+++ b/library/stream.string_writer.m
@@ -51,8 +51,12 @@
      % If the argument is just a single string or character, it will be printed
      % out exactly as is (unquoted).  If the argument is of type integer (i.e.
      % an arbitrary precision integer), then its decimal representation will be
-    % printed.  If the argument is of type univ, then it will print out the
-    % value stored in the univ, but not the type.
+    % printed.  If the argument is of type univ, then the value stored in the
+    % the univ will be printed out, but not the type.  If the argument is of
+    % type date_time, it will be printed out in the same form as the string
+    % returned by the function date_to_string/1.  If the argument is of type
+    % duration, it will be printed out in the same form as the string
+    % returned by the function duration_to_string/1.
      %
      % print/5 is the same as print/4 except that it allows the caller to
      % specify how non-canonical types should be handled.  print/4 implicitly
@@ -178,6 +182,7 @@

  :- import_module array.
  :- import_module bitmap.
+:- import_module calendar.
  :- import_module int.
  :- import_module integer.
  :- import_module require.
@@ -264,6 +269,10 @@ print(Stream, NonCanon, Term, !State) :-
          write_univ(Stream, OrigUniv, !State)
      ; dynamic_cast(Term, BigInt) ->
          put(Stream, integer.to_string(BigInt), !State)
+    ; dynamic_cast(Term, DateTime) ->
+        put(Stream, date_to_string(DateTime), !State)
+    ; dynamic_cast(Term, Duration) ->
+        put(Stream, duration_to_string(Duration), !State)
      ;
          print_quoted(Stream, NonCanon, Term, !State)
      ).
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index a99bc57..6dc9480 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -232,6 +232,7 @@ ORDINARY_PROGS=	\
  	pretty_printing \
  	prince_frameopt \
  	print_bigint \
+	print_date \
  	print_stream \
  	profdeep_seg_fault \
  	promise_equiv_with_svars \
diff --git a/tests/hard_coded/print_date.exp b/tests/hard_coded/print_date.exp
new file mode 100644
index 0000000..d831d20
--- /dev/null
+++ b/tests/hard_coded/print_date.exp
@@ -0,0 +1,2 @@
+2014-12-19 16:05:00
+P5DT7H55M
diff --git a/tests/hard_coded/print_date.m b/tests/hard_coded/print_date.m
new file mode 100644
index 0000000..7e71d32
--- /dev/null
+++ b/tests/hard_coded/print_date.m
@@ -0,0 +1,20 @@
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+
+% Test io.print with dates and durations.
+
+:- module print_date.
+:- interface.
+
+:- import_module io.
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module calendar.
+
+main(!IO) :-
+    Date1 = det_init_date(2014, december, 19, 16, 5, 0, 0),
+    Date2 = det_init_date(2014, december, 25, 0, 0, 0, 0),
+    io.print_line(Date1, !IO),
+    Duration = duration(Date1, Date2),
+    io.print_line(Duration, !IO).



More information about the reviews mailing list