To simplify calculations I would use a structure that does not contain the individual fields but a single or two values.
If you only want to support Windows you can use the
FILETIME structure (Windows)[
^].
Alternatively and for general OS support use the
timeval structure (Windows)[
^]. Here just add the members and when the addtion of the
tv_usec
s overflows (>= 1E6), increment
tv_sec
and subtract 1E6 from
tv_usec
.
For both structures there are conversion functions to other time formats so that you can get access to the individual date and time fields. The advantage of the
timeval
structure is that you have direct access to the fraction of second field when the converted format does not support such.
[EDIT]
If you still want to use your structure, write conversion functions to and from one of the above mentioned structures and use those to perform the caluclation.
Note also that your structure is very similar to the
SYSTEMTIME structure (Windows)[
^]. If you use that, there are existing conversion functions to and from
FILETIME
.
[/EDIT]
[EDIT2]
Untested examples from scratch:
timeval add(const timeval& tv, int ms)
{
timeval tv_res = { ms / 1000, 1000 * (ms % 1000)};
tv_res.tv_sec += tv.tv_sec;
tv_res.tv_usec += tv.tv_usec;
if (tv_res.tv_usec >= 1000000)
{
tv_res.tv_sec++;
tv_res.tv_usec -= 1000000;
}
return tv_res;
}
timestamp add(const timestamp& t, int ms)
{
return TimeValToTimeStamp(add(TimeStampToTimeVal(t), ms));
}
All you need now are the conversion functions (TimeValToTimeStamp() left as an exercise):
timeval TimeStampToTimeVal(const timestamp& t)
{
struct tm tmt = { 0 };
tmt.tm_year = t.year;
tmt.tm_mon = t.month;
tmt.tm_mday = t.day;
tmt.tm_hour = t.hour;
tmt.tm_min = t.minute;
tmt.tm_sec = t.sec;
timeval tv;
tv.sec = (long)mktime(&tmt);
tv.usec = t.ms * 1000;
return tv;
}
[/EDIT2]