[m-dev.] for review: add `time' module to standard library

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Oct 28 16:21:48 AEST 1999


On 28-Oct-1999, David Glen JEFFERY <dgj at cs.mu.OZ.AU> wrote:
> On 28-Oct-1999, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> > Estimated hours taken: 3
> > 	(plus unknown time by Thomas By)
> > library/time.m:
> > 	New module, originally written by Thomas By <T.By at dcs.shef.ac.uk>.
> 
> s/Thomas By/Tomas By/g
> 
> (This occurs a few places in the diff, including the www page entry).

Fixed.

> > ===================================================================
> > library/time.m
> > ===================================================================
> 
> > :- type tms --->
> >         tms(int,    % Utime
> >             int,    % Stime
> >             int,    % CUtime
> >             int).   % CStime
> > 
> > :- type time_t == int.
> > 
> > :- type tm --->
> >         tm(int,          % Seconds (0-60)
> >            int,          % Minutes (0-59)
> >            int,          % Hours (after midnight, 0-23)
> >            int,          % WeekDay (number since Sunday, 0-6)
> >            int,          % YearDay (number since Jan 1st, 0-365)
> >            int,          % Month (number since January, 0-11)
> >            int,          % Year (number since 1900)
> >            maybe(dst)).  % IsDST (is DST in effect?)
> 
> I think the types `tms' and `tm' deserve a brief comment above them explaining
> their purpose.

I've added some comments.
While adding those, I noticed a few other problems.
Here's a relative diff.

--- CHANGES2.orig	Thu Oct 28 15:26:38 1999
+++ CHANGES2	Thu Oct 28 16:18:14 1999
@@ -16,6 +16,11 @@
 		  to a `time_t *'; that won't have the desired effect
 		  if `Integer' and `time_t' are not the same size.
 	    * interface changes:
+	        - change the return type for difftime/1 from `int' to `float',
+		  to match the C `double' return type of the C difftime()
+		  function.
+	        - add the gmtime/1 function
+	        - make the `time_t' type abstract rather than concrete
 	        - changed the clocks_per_second procedure so that
 		  it did not take any io__state arguments
 		  (that's safe, since ANSI/ISO C requires the
@@ -28,6 +33,8 @@
 		- changed the code to throw exceptions on failure rather
 		  than returning results of type `io__res(T)'
 	    * stylistic changes:
+		- declare the fields of the tms struct as type clock_t
+		  rather than int
 		- modified the layout to match our standard
 		  Mercury coding conventions
 		- added a more detailed copyright notice

--- time.m.old3	Thu Oct 28 15:26:19 1999
+++ time.m	Thu Oct 28 16:02:48 1999
@@ -22,16 +22,23 @@
 :- use_module io.
 :- import_module std_util.
 
+	% The `clock_t' type represents times measured in clock ticks.
 :- type clock_t == int.
 
+	% The `tms' type holds information about the amount of processor
+	% time that a process and its child processes have consumed.
 :- type tms --->
-        tms(int,    % Utime
-            int,    % Stime
-            int,    % CUtime
-            int).   % CStime
-
-:- type time_t == int.
+        tms(clock_t,    % tms_utime: user time
+            clock_t,    % tms_stime: system time
+            clock_t,    % tms_cutime: user time of children
+            clock_t).   % tms_cstime: system time of children
+
+	% The `time_t' type is an abstract type that represents
+	% calendar times.
+:- type time_t.
 
+	% The `tm' type is a concrete type that represents calendar
+	% times, broken down into their constituent components.
 :- type tm --->
         tm(int,          % Seconds (0-60)
            int,          % Minutes (0-59)
@@ -92,11 +99,11 @@
 
 %-----------------------------------------------------------------------------%
 
-	% time__difftime(Time1, Time0) = N:
+	% time__difftime(Time1, Time0) = Diff:
 	%	Computes the number of seconds elapsed between
 	%	`Time1' and `Time0'.
 	%
-:- func time__difftime(time_t, time_t) = int.
+:- func time__difftime(time_t, time_t) = float.
 
 	% time__localtime(Time) = TM:
 	%	Converts the calendar time `Time' to a broken-down
@@ -105,6 +112,12 @@
 	%
 :- func time__localtime(time_t) = tm.
 
+	% time__gmtime(Time) = TM:
+	%	Converts the calendar time `Time' to a broken-down
+	%	representation, expressed as UTC (Universal Coordinated Time).
+	%
+:- func time__gmtime(time_t) = tm.
+
 	% time__mktime(TM) = Time:
 	%	Converts the broken-down time value to calendar time.
 	%	It also normalises the value by filling in day of
@@ -132,6 +145,12 @@
 
 :- implementation.
 
+:- import_module int, exception.
+
+% XXX The assumption that a C `time_t' can fit into a Mercury `Integer'
+% is not very portable.
+:- type time_t == int.
+
 :- pragma c_header_code("
 	#include <time.h>
 	#ifdef HAVE_SYS_TYPES_H
@@ -242,26 +261,24 @@
 
 %-----------------------------------------------------------------------------%
 
-%:- func time__difftime(time_t, time_t) = int.
+%:- func time__difftime(time_t, time_t) = float.
 
-time__difftime(T1, T0) = N :-
-	time__c_difftime(T1, T0, N).
+time__difftime(T1, T0) = Diff :-
+	time__c_difftime(T1, T0, Diff).
 
-:- pred time__c_difftime(int, int, int).
+:- pred time__c_difftime(int, int, float).
 :- mode time__c_difftime(in, in, out) is det.
 
-:- pragma c_code(time__c_difftime(T1::in, T0::in, N::out),
+:- pragma c_code(time__c_difftime(T1::in, T0::in, Diff::out),
 	[will_not_call_mercury],
 "{
-	N = (Integer) difftime((time_t) T1, (time_t) T0);
+	Diff = (Float) difftime((time_t) T1, (time_t) T0);
 }").
 
 %-----------------------------------------------------------------------------%
 
 %:- func time__localtime(time_t) = tm.
 
-:- import_module int, exception.
-
 time__localtime(Time) = TM :-
 	time__c_localtime(Time, Sec, Min, Hrs, WD, YD, Mnt, Yr, N),
 	( N = 0 ->
@@ -287,6 +304,48 @@
 	t = Time;
 
 	p = localtime(&t);
+
+	/* XXX do we need to handle the case where p == NULL here? */
+
+	Sec = (Integer) p->tm_sec;
+	Min = (Integer) p->tm_min;
+	Hrs = (Integer) p->tm_hour;
+	Mnt = (Integer) p->tm_mon;
+	Yr = (Integer) p->tm_year;
+	WD = (Integer) p->tm_wday;
+	YD = (Integer) p->tm_yday;
+	N = (Integer) p->tm_isdst;
+}").
+
+%:- func time__gmtime(time_t) = tm.
+
+time__gmtime(Time) = TM :-
+	time__c_gmtime(Time, Sec, Min, Hrs, WD, YD, Mnt, Yr, N),
+	( N = 0 ->
+		DST = yes(standard_time)
+	; N > 0 ->
+		DST = yes(daylight_time)
+	; % N < 0
+		DST = no
+	),
+	TM = tm(Sec, Min, Hrs, WD, YD, Mnt, Yr, DST).
+
+:- pred time__c_gmtime(int, int, int, int, int, int, int, int, int).
+:- mode time__c_gmtime(in, out, out, out, out, out, out, out, out) is det.
+
+:- pragma c_code(time__c_gmtime(Time::in, Sec::out, Min::out, Hrs::out,
+                                   WD::out, YD::out, Mnt::out,
+                                   Yr::out, N::out),
+	[will_not_call_mercury],
+"{
+	struct tm* p;
+	time_t t;
+
+	t = Time;
+
+	p = gmtime(&t);
+
+	/* XXX do we need to handle the case where p == NULL here? */
 
 	Sec = (Integer) p->tm_sec;
 	Min = (Integer) p->tm_min;
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list