Date Class
Overview
This section introduces the date class, which is used widely throughout
Udicus. We will first introduce the basic idea behind the class and
follow up with a list of the operator overloads and a discussion of
the member functions.
The date class was created to make calculations involving dates easier.
Dates are stored in two unsigned 64-bit integers. One of them is the
variable yr, which holds the year, and the other is ms, which holds
the number of milliseconds into the year. Both variables are public
and may be accessed directly as well as through member functions.
These two variables make comparisons and arithmetic much easier, but
some conversion is needed since dates are not typically written in a
year/millisecond format. When calculating the number of milliseconds
into the year, there are a couple points that you should keep in mind
to avoid mistakes. When time is expressed in the hh:mm:ss format, each
of the numbers represent the hours, minutes or seconds passed since
the beginning of the day. Day and month information differs from this
convention in that 23/05 does not mean 5 months and 23 days have passed
since the beginning of the year, instead you must subtract one from
each of the numbers to find how many months and days have passed. This
is important to remember when setting up a date variable. The user,
however, need not worry about the conversion from the date class format
to a more conventional format because the class provides functions that
return strings for milliseconds, seconds, minutes, hours, days, months
and years.
Operator Overloads
The following operators are defined for the date class:
‘=’
‘+ =’
‘- =’
‘= =’
‘>’
‘<’
The reader will notice that the binary ‘+’ and ‘-’
operators are not defined. This may cause some confusion when the reader
would like to perform an operation similar to the following:
Date1 = Date2 - Date1;
You can overcome this hurdle by using a temporary variable.
Temp_date = Date2;
Temp_date - = Date1;
Date1 = Temp_date;
There are two constructors in the date class. The basic constructor
creates a date object initialized to the current time, while the second
constructor initializes the object to the user specified year and millisecond.
Date();
Date(const unsigned __int64 yr, const unsigned __int64 ms);
Member Functions
The yr and ms variables can be modified in one of two ways. The first
way is to access them directly, and the second way is to use the ‘set ’
function.
void set(const unsigned __int64 yr, const unsigned __int64 ms);
The ‘read ’ function can read a date object from
a file whose pointer is passed to the function as a parameter. The ‘toFile ’
function writes a date object into a file.
bool read(FILE* fd);
bool toFile(FILE* fd);
The ‘get ’ function will copy the yr
and ms variables of a date object into two 64-bit unsigned
integers passed to the function by reference.
void get(unsigned __int64& yr, unsigned __int64& ms);
The ‘getYear ’ function returns a string containing
the year in ASCII. The string is always right justified. The justification
is done assuming that the string will be four characters long, therefore
this function will no longer work after 23:59:59 December 31st, 9999.
string getYear(void);
The getMonth , getHour , getMinute ,
getSecond and getMillisecond functions all
return strings containing their respective data. All of these are two
characters long with the exception of getMillisecond , which
is three characters long.
string getMonth();
string getHour();
string getMinute();
string getSecond();
string getMillisecond();
The getDay function returns the day as an integer.
int getDay();
Finally, there is a read_from_str function that accepts
a string in the “dd/mm/yyyy hh:mm:ss” format and a toString
function, which returns the date in the same format.
bool read_from_str(string& timedate);
string toString();
|