I'm trying to develop a c++ library that provide two kind of times:
- UTC time(self explainitory)
- System time(time since the system has went on)
I have looked into the chrono library in cpp and im trying to implement my library, more of the same like chrono. the implementation is very complicated for me right now.
I'll post the complete code I have later on, but so far I support only UTC time. I have few problems in my code that I want to improve but I don't know how exactly.
#include "atltime.h"
#include "mmsystem.h"
namespace tc{
typedef struct date{
unsigned int day;
unsigned int month;
unsigned year;
date(unsigned int _day, unsigned int _month, unsigned int _year)
{
day = _day;
month = _month;
year = _year;
}
date() :day(0), month(0), year(0){}
}date;
template <class _Clock,
class _Time = typename _Clock::time,
class _Date = typename _Clock::date>
class time_point
{
public:
typedef _Clock clock;
typedef _Time time;
typedef _Date date;
time_point(const time& otherTime, const date& otherDate)
{
_MyTime = otherTime;
_MyDate = otherDate;
}
time get_time() const
{
return (_MyTime);
}
date get_date() const
{
return (_MyDate);
}
private:
_Time _MyTime;
_Date _MyDate;
};
}
struct utc_clock
{
typedef unsigned long long time;
typedef tc::date date;
typedef tc::time_point<utc_clock> time_point;
public:
static time_point now()
{
SYSTEMTIME systime;
GetSystemTime(&systime);
time_t cur_time = time(0);
struct tm *time_buf = localtime(&cur_time);
time t = (((time_buf->tm_sec + (time_buf->tm_min * 60) + (time_buf->tm_hour * 3600)) * 1000) + systime.wMilliseconds) * 1000ULL;
tc::date d((unsigned int)systime.wDay, (unsigned int)systime.wMonth, (unsigned int)systime.wYear);
return time_point(t, d);
}
static time to_micro(const time_point& t)
{
return t.get_time();
}
static time to_mili(const time_point& t)
{
return to_micro(t) / 1000;
}
static time to_sec(const time_point& t)
{
return to_mili(t) / 1000;
}
static time to_min(const time_point& t)
{
return to_sec(t) / 60;
}
static time to_hour(const time_point& t)
{
return to_min(t) / 60;
}
static char * to_utc_time_str(const time_point& t)
{
char *str = new char[100];
sprintf(str, "%02d:%02d:%02d:%03d:%03d", to_hour(t), to_min(t), to_sec(t), to_mili(t), to_micro(t));
return str;
}
static char * to_utc_date_str(const time_point& t)
{
char *str = new char[100];
sprintf(str, "%02d.%02d.%04d", t.get_date().day, t.get_date().month, t.get_date().year);
return str;
}
};
The usage goes like that:
int main()
{
tc::time_point<utc_clock> t = utc_clock::now();
char * str = utc_clock::to_utc_date_str(t);
unsigned long long micro, mili, second;
unsigned long long s;
micro = utc_clock::to_micro(t);
mili = utc_clock::to_mili(t);
second = utc_clock::to_sec(t);
s = micro + mili; // this is not good, need to use type-safe
//handler on return time, or have a converter to allow it.
cout << "Micro: " << micro << endl;
cout << "Mili: " << mili << endl;
cout << "Seconds: " << second << endl;
cout << "UTC Date is: " << str << endl;
getchar();
}
There are few things I think I can improve in my code, but don't know how to implement it.
all the function in
to_micro,to_mili,to_sec... etc can be written in otherway using operator overloading maybe? for example I can have'sc'operator that can convert to seconds based on the received time.sc(1000)// 1000 is in mili, will convert it automatically to 1.I want to make sure that doing arithmetical operation on different kind of times(mili/seconds/micro) is avoided. I'd be happy to get a direction over it.
Any other suggestions/idea will be greatly welcomed