scsh-users
[Top] [All Lists]

Re: date/time calculations

To: Thomas Wager <twager@ti.com>
Subject: Re: date/time calculations
From: Martin Gasbichler <gasbichl@informatik.uni-tuebingen.de>
Date: Tue, 26 Feb 2002 11:53:20 +0100
Cc: scsh-news@zurich.ai.mit.edu
Sender: gasbichl@informatik.uni-tuebingen.de
>>>>> "Thomas" == Thomas Wager <twager@ti.com> writes:

Thomas> Hi all,
Thomas> I'm quite new to scsh and tried some date calculations. The following
Thomas> error seems a little bit strange to me, from what I read in the manual
Thomas> this should work:

Thomas> Make a date object from the current date (works):
>> (make-date  0 3 16 25 2 2002)
Thomas> '#{%date}

Sure, no argument checking is going on here.

Thomas> Call (time) with the current date (works, too):
>> (time (date))
Thomas> 1014651710

Thomas> Call (time) with the date created above (fails ??):
>> (time (make-date  0 3 16 25 2 2002)))


Thomas> Error: 79
>> "Value too large for defined data type"
>> #{Procedure 11277 (%date->time in scsh-level-0)}
>> 0
>> 3
>> 16
>> 25
>> 2
1> 

Thomas> I have no idea why this happens... BTW, it's version 0.6.1 on Solaris 8.
Thomas> Hopefully someone can point me to what I am doing wrong here.

Well, the Unix time representation is known to overflow somewhere
around 2030 or so. You are specifying a date in the year 3902 since
the year argument is expected to be the number of years since
1900. I don't think you are really interested in this future date ;-)
Likewise, the month you give is March since months are enumerated
starting at 0...

There is however a bug in this code that Rolf-Thomas Happe already
observed: on some systems the return value -1 is not considered an
error. Here are the patches to fix this. In the case of an error we do
not signal an errno-error but use the normal error procedure.


Index: time1.c
===================================================================
RCS file: /cvsroot/scsh/scsh-0.6/scsh/time1.c,v
retrieving revision 1.10
diff -u -r1.10 time1.c
--- time1.c     12 Feb 2002 15:49:30 -0000      1.10
+++ time1.c     26 Feb 2002 10:48:37 -0000
@@ -224,7 +224,7 @@
 {
     time_t t;
     struct tm d;
-
+    int err = 0;
     d.tm_sec  = s48_extract_fixnum (sec); 
     d.tm_min  = s48_extract_fixnum (min);
     d.tm_hour = s48_extract_fixnum (hour);
@@ -238,12 +238,8 @@
        environ = utc_env;                              /* time temporarily. */
        tzset(); /* NetBSD, SunOS POSIX-noncompliance requires this. */
        d.tm_isdst = 0; /* FreeBSD, at least, needs this or it sulks. */
-       errno = 0;
        t = mktime(&d);
-       /* t == -1 => you probably have an error. */
-       if ((t == -1) && (errno != 0))
-         /* Sorry, we only have a version with 5 arguments..*/
-         s48_raise_os_error_5 (errno, sec, min, hour, mday, month);
+       if (t == -1) err = 1;
        t -= s48_extract_fixnum(tz_secs);
        environ = oldenv;
        }
@@ -260,8 +256,7 @@
 
        d.tm_isdst = -1;
        t = mktime(&d);
-       if ((t == -1) && (errno != 0))
-         s48_raise_os_error_5 (errno, sec, min, hour, mday, month);
+       if (t == -1) err = 1;
        revert_env(oldenv);
        }
 
@@ -270,11 +265,10 @@
        errno = 0;
        d.tm_isdst = -1;
        t = mktime(&d);
-       if ((t == -1) && (errno != 0))
-         s48_raise_os_error_5 (errno, sec, min, hour, mday, month);
+       if (t == -1) err = 1;
     }
 
-    return s48_enter_integer(t);
+    return s48_cons (err ? S48_TRUE : S48_FALSE, s48_enter_integer(t));
 }
Index: time.scm
===================================================================
RCS file: /cvsroot/scsh/scsh-0.6/scsh/time.scm,v
retrieving revision 1.9
diff -c -r1.9 time.scm
*** time.scm    12 Sep 2001 14:08:24 -0000      1.9
--- time.scm    26 Feb 2002 10:49:31 -0000
***************
*** 113,127 ****
    (if (pair? args)
        (if (null? (cdr args))
          (let ((date (check-arg date? (car args) time)))
!           (%date->time (date:seconds   date)
!                        (date:minute    date)
!                        (date:hour      date)
!                        (date:month-day date)
!                        (date:month     date)
!                        (date:year      date)
!                        (date:tz-name   date) ; #f or string
!                        (date:tz-secs   date) ; #f or int
!                        (date:summer?   date)))
          (error "Too many arguments to TIME procedure" args))
        (%time)))       ; Fast path for (time).
  
--- 113,131 ----
    (if (pair? args)
        (if (null? (cdr args))
          (let ((date (check-arg date? (car args) time)))
!           (let ((err?.time
!                  (%date->time (date:seconds   date)
!                               (date:minute    date)
!                               (date:hour      date)
!                               (date:month-day date)
!                               (date:month     date)
!                               (date:year      date)
!                               (date:tz-name   date) ; #f or string
!                               (date:tz-secs   date) ; #f or int
!                               (date:summer?   date))))
!             (if (car err?.time)
!                 (error "Error converting date to time." args)
!                 (cdr err?.time))))
          (error "Too many arguments to TIME procedure" args))
        (%time)))       ; Fast path for (time).
  

-- 
Martin

<Prev in Thread] Current Thread [Next in Thread>