I got an interesting issue. I attempted putting together a DateTime class to include in my library. I did very basic testing at this time, and much more to do. I currently have this problem:
Code:
DateTime dt1("1/15/2010");
DateTime dt2("5/2/2009");
cout << dt1.ToString() << endl;
cout << dt2.ToString() << endl;
Both output lines show "5/2/2009". I would expect both to be unique... does it have to do with the system clock and something staying static? I can't seem to tell.
My last option is not call the clock/time functions and just declare int variables in the class to keep track of the date and time. My goal is to let the user define any date/time, and by default it stores the current local date/time.
Here's my code:
Code:
namespace X3D
{
class DateTime
{
private:
time_t m_time;
tm *m_timeInfo;
char m_buffer[40];
public:
DateTime();
DateTime(const int month, const int day, const int year);
DateTime(const char *date);
const char *ToString(const char *format = DT_MMDDYYYY);
void AddDay(int count = 1);
void AddMonth(int count = 1);
void AddYear(int count = 1);
int GetDay();
int GetMonth();
int GetYear();
void SetDate(const int month, const int day, const int year);
void SetDate(const char *date);
void SetTime(const int hour, const int minute, const int second = 0);
void SetTime(const char *time);
const char *GetTime();
operator const char *() { return ToString(); }
};
}
.CPP file:
Code:
#include "X3D_DateTime.h"
/////////////////////////////////////////////////////////////
/// <summary>
/// DateTime constructor where default date is today's date.
/// </summary>
/////////////////////////////////////////////////////////////
X3D::DateTime::DateTime()
{
time(&m_time);
m_timeInfo = localtime(&m_time);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// DateTime constructor where requested date is set.
/// </summary>
/// <param name = "month"> The month to set. </param>
/// <param name = "day"> The day to set. </param>
/// <param name = "year"> The year to set. </param>
/////////////////////////////////////////////////////////////
X3D::DateTime::DateTime(const int month, const int day,
const int year)
{
time(&m_time);
m_timeInfo = localtime(&m_time);
m_timeInfo->tm_mon = month - 1; // Month range of 0-11
m_timeInfo->tm_mday = day;
m_timeInfo->tm_year = year - 1900; // Takes the number of years after 1900
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// DateTime constructor that converts date string.
/// </summary>
/// <param name = "date"> The date to parse. </param>
/////////////////////////////////////////////////////////////
X3D::DateTime::DateTime(const char *date)
{
int month, day, year;
std::stringstream ss;
ss << date;
// Get the month, day, and year.
ss >> month;
ss.ignore(1);
ss >> day;
ss.ignore(1);
ss >> year;
// Create date.
time(&m_time);
m_timeInfo = localtime(&m_time);
m_timeInfo->tm_mon = month - 1; // Month range of 0-11
m_timeInfo->tm_mday = day;
m_timeInfo->tm_year = year - 1900; // Takes the number of years after 1900
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Parses the date to a string object.
/// </summary>
/// <param name = "format"> The format to output. </param>
/// <returns> Returns a string representation. </param>
/////////////////////////////////////////////////////////////
const char *X3D::DateTime::ToString(const char *format)
{
strftime(m_buffer, 40, format, m_timeInfo);
return m_buffer;
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Increments the day.
/// </summary>
/// <param name = "count"> Increment count. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::AddDay(int count)
{
m_timeInfo->tm_mday += count;
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Increments the month.
/// </summary>
/// <param name = "count"> Increment count. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::AddMonth(int count)
{
m_timeInfo->tm_mon += count;
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Increments the year.
/// </summary>
/// <param name = "count"> Increment count. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::AddYear(int count)
{
m_timeInfo->tm_year += count;
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Gets the day.
/// </summary>
/// <returns> Returns the day. </param>
/////////////////////////////////////////////////////////////
int X3D::DateTime::GetDay()
{
strftime(m_buffer, 40, DT_DAY, m_timeInfo);
return atoi(m_buffer);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Gets the month.
/// </summary>
/// <returns> Returns the day. </param>
/////////////////////////////////////////////////////////////
int X3D::DateTime::GetMonth()
{
strftime(m_buffer, 40, DT_MONTH, m_timeInfo);
return atoi(m_buffer);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Gets the year.
/// </summary>
/// <returns> Returns the day. </param>
/////////////////////////////////////////////////////////////
int X3D::DateTime::GetYear()
{
strftime(m_buffer, 40, DT_YEAR, m_timeInfo);
return atoi(m_buffer);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Sets a new date.
/// </summary>
/// <param name = "month"> The month to set. </param>
/// <param name = "day"> The day to set. </param>
/// <param name = "year"> The year to set. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::SetDate(const int month, const int day,
const int year)
{
time(&m_time);
m_timeInfo = localtime(&m_time);
m_timeInfo->tm_mon = month - 1; // Month range of 0-11
m_timeInfo->tm_mday = day;
m_timeInfo->tm_year = year - 1900; // Takes the number of years after 1900
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Sets the date in string form.
/// </summary>
/// <param name = "date"> The date to parse. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::SetDate(const char *date)
{
int month, day, year;
std::stringstream ss;
ss << date;
// Get the month, day, and year.
ss >> month;
ss.ignore(1);
ss >> day;
ss.ignore(1);
ss >> year;
// Create date.
time(&m_time);
m_timeInfo = localtime(&m_time);
m_timeInfo->tm_mon = month - 1; // Month range of 0-11
m_timeInfo->tm_mday = day;
m_timeInfo->tm_year = year - 1900; // Takes the number of years after 1900
mktime(m_timeInfo);
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Sets a new time.
/// </summary>
/// <param name = "hour"> The hour to set. </param>
/// <param name = "minute"> The minute to set. </param>
/// <param name = "second"> The second to set. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::SetTime(const int hour, const int minute,
const int second)
{
m_timeInfo->tm_hour = hour;
m_timeInfo->tm_min = minute;
m_timeInfo->tm_sec = second;
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Sets a new time.
/// </summary>
/// <param name = "time"> The string value. </param>
/////////////////////////////////////////////////////////////
void X3D::DateTime::SetTime(const char *time)
{
int hour, minute, second = 0;
std::stringstream ss;
int len = 0;
// Get string length.
len = strlen(time);
ss << time;
// Get the month, day, and year.
ss >> hour;
ss.ignore(1);
m_timeInfo->tm_hour = hour;
if (len > 2) {
ss >> minute;
ss.ignore(1);
m_timeInfo->tm_min = minute;
} else {
m_timeInfo->tm_min = 0;
}
if (len > 4) {
ss >> second;
m_timeInfo->tm_sec = second;
} else {
m_timeInfo->tm_sec = 0;
}
}
/////////////////////////////////////////////////////////////
/// <summary>
/// Returns the time in string format.
/// </summary>
/// <returns> Returns the time in string format. </returns>
/////////////////////////////////////////////////////////////
const char *X3D::DateTime::GetTime()
{
strftime(m_buffer, 40, DT_TIME, m_timeInfo);
return m_buffer;
}