No more ctime(S) errors - Time(C++)


Time has three constructors. The parameterless constructor creates a Time equal to Time::REF(), as defined in the section, ``Time constants''. The four-parameter constructor expects its first three arguments to describe a date and the fourth argument to be a Place. It constructs a Time of midnight on the beginning of specified date at the specified Place (a ``local time'' relative to that Place). A version of the constructor without the fourth parameter does the same thing for the Place in which the host machine is located (see ``Place''.

For example:

       Time t1(1988,Time::january,1,Place::pacific());
       Time t2(1988,Time::january,1,Place::eastern());
       cout << (t1-t2) << endl;


      0d 03h 00m 00s

because midnight in the Pacific timezone occurs three hours later than midnight in the Eastern timezone.

In applications concerned with Times at Places other than the host machine location, it is the client's responsibility to keep track of the Place associated with each Time, and to remember to pass the same Place argument to any operation that returns Place-dependent information. For example:

       Time t(1988,Time::january,1,Place::eastern());
       cout << t.day_part() << endl;

will not necessarily print ``1''; rather, it will print the day of the month at the host machine location that corresponds, in absolute time, to 0h, Jan 1, 1988 at Place::eastern(). Assume that the machine running the above program is located in Place::pacific(). Then the program would actually print 31! To print the correct day number for Place::eastern(), the Place argument must be repeated:


In most of the examples that follow, we will use versions of Time functions that assume the host machine location. We will say more about defining and using Places in the section, ``Place''.

The four-parameter Time constructor requires that the arguments you pass must describe both (1) a valid date and (2) a Time lying within the range [Time::MIN(),Time::MAX()]. A date is valid if

For efficiency, the Time constructor does not check for bad arguments. Passing bad arguments to the constructor causes undefined behavior. [1]

If you have arguments from an unreliable source, you may check them using the auxiliary static member function Time::valid_date() (see the section, ``Auxiliary Time functions'')

       Time paranoid(unsigned y,Time::Month m,unsigned d){
           Time result;
               result = Time(y,m,d);
           return result;

Next topic: Julian date operations
Previous topic: Enumerations

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005