Re: how to compare two time

From: Lueko Willms (l.willms_at_jpberlin.de)
Date: 10/11/04


Date: 11 Oct 2004 08:26:00 GMT


. Am 11.10.04
schrieb William.Cai@gmail.com (William Cai)
    auf /COMP/LANG/COBOL
     in ckcmcj$bf2$1@mail.cn99.com
  ueber Re: how to compare two time

WC> Hey, I know what you mean. But in C/C++ world, you just need to do
WC> the following thing.
WC>
WC> int begin = time(NULL);
WC> ... ...
WC> int end = time(NULL);
WC>
WC> int val = end - begin;

   You could even use the "difftime" function.

   BUT ... you wrote "int", but is "int" really the type for the
"time" function? The standard says the type is "time_t", and that is
implementor defined: "the range and precision of times representable
in clock_t and time_t are implementation-defined".

. Also, on the "time" function: "the encoding of the value is
unspecified", so you don't really know what "val" represents -
minutes? seconds? milliseconds?

  Similar the definition of the structure "tm", which "shall contain
at least the following members, _in any order"._

  See the C standard, chapter 7.23

WC> How about COBOL?

  The corresponding structure in COBOL to the "tm" structure is well
defined, and you can see it when you look at FUNCTION CURRENT-DATE or
FUNCTION DATE-COMPILED, and so well defined with the components in
order of significance so that it allows a sensible comparison of those
structures for GREATER or LESS, without looking at the individual
components (maybe one has to omit the time-zone offset, which is btw,
not included in C's "tm" structure).

  COBOL also provides date conversion functions, most importantly
INTEGER-OF-DATE and DATE-OF-INTEGER which provides or uses a serial
day number with the epoch of Janary 1, 1601 as day number 1.

  When you want to compare times, it is better to use these functions
instead of the ACCEPT ... FROM TIME and ACCEPT ... FROM DATE.
Converting clock times from hour, minute, second to an integer and
vice versa does not require special functions because of the regular
structure - an hour has always 60 mintues, and a minute has always 60
seconds, except when there is a leap second inserted.

  The difficulty arises only when you want to have a single number
representing a point in time, including both a serial day number and
the time of day.

  For this case, your compiler either provides a system function
giving the seconds or milliseconds passed since some not so recent
epoch (UNIX: Midnight at January 1, 1970) which you can use, or you
calculate your own number using the values provided by FUNCTION
CURRENT-DATE and e.g. FUNCTION INTEGER-OF-DATE(19700101), either
calculating the number of seconds elapsed, or, as some applications
do, using the time of day as a fractional part of a _real_ number for
date-time (instead of an integer). It depends on the _purpose_ of your
time comparison which you did not mention in your question.

  Sure, all those FUNCTIONs arrived in the COBOL standard quite late,
but then they have some improvements over C's "time" library, e.g. the
FUNCTION INTEGER-OF-DATE which for many applications is more important
than expressing a complete time stamp in seconds or fractions of
seconds.

  Maybe the main difference for YOU between COBOL and C or C++ is that
you have been tought the latter in school and hardly know the former.
This is about to change, it seems to me.

Have fun,
yours,
Lüko Willms http://www.mlwerke.de
/--------- L.WILLMS@jpberlin.de -- Alle Rechte vorbehalten --

"Kein Land kann seine Probleme in dieser globalisierten Welt allein
auf sich gestellt lösen. Entweder wir retten uns alle zusammen oder
wir gehen zusammen unter. Heute mehr denn je gilt das Wort von José
Martí: Das Vaterland ist die ganze Menschheit."
               - Fidel Castro, Caracas (Veneuzuela), 3. Februar 1999



Relevant Pages

  • Re: how to compare two time
    ... WC> int begin = time; ... WC> How about COBOL? ... When you want to compare times, it is better to use these functions ... FUNCTION INTEGER-OF-DATE which for many applications is more important ...
    (comp.lang.cobol)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... I am asking how a machine language BRANCH instruction (which has a SINGLE ... can be SLOWER than a machine language COMPARE instruction that has ... If 150 programmers are each compiling 5 COBOL programs ... possibility that ALTERED code in your environment may not be a good thing, ...
    (comp.lang.cobol)
  • Re: fastest sorted list type?
    ... I'm not aware of any scenario you've mentioned in which what you're sorting is "just an int". ... Yes, you have a scenario in which you are sorting a list of ints, but when you actually compare two entries in that list you aren't comparing the integers themselves, but rather the data in an object to which the int refers. ... I did have a look on google and wiki lists al the sort algorothms nicely, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: calling qsort
    ... int mycmp(const void *a, const void *b) ... the compare function. ...
    (comp.lang.c)
  • Re: Rolling Up Characters = Skewing the Game?
    ... I want a basis to compare die rolls systems ... I need point values for ability scores ... point values below 3 and above 18 to compare them. ... is still some detriment to having a 3 int fighter as opposed to an 8 ...
    (rec.games.frp.dnd)