/* Lexi Fiction Writer
* Main ADT
* 2012/06/27 clonne
*
* The Module of Date for Fiction ADT
*/
#include <time.h>
#include <ADT/ADT.h>
using namespace Lexi;
typedef struct
{
int min;
int max;
}Range_t;
Range_t c_Ranges[6] =
{
// 由于Lexi在2012年开发,所以不存在2012年之前的日期
{2012, -1},
{1, 12},
// 一月内最多31天
{1, 31},
// 24小时制
{0, 23},
{0, 59},
{0, 59}
};
std::string ADT::Date::ToString()
{
char str_buf[512];
sprintf(str_buf, "%d/%02d/%02d %02d:%02d:%02d",
this->Get(YEAR),
this->Get(MONTH),
this->Get(DAY),
this->Get(HOUR),
this->Get(MIN),
this->Get(SECOND)
);
std::string date_str(str_buf);
return date_str;
}
bool ADT::Date::FromString(const std::string &date_str)
{
int y, m, d, h, min, sec;
if (6 == sscanf(date_str.c_str(), "%d/%d/%d %d:%d:%d",
&y, &m, &d, &h, &min, &sec)
)
{
if (this->InRange(YEAR, y) &&
this->InRange(MONTH, m) &&
this->InRange(DAY, d) &&
this->InRange(HOUR, h) &&
this->InRange(MIN, min) &&
this->InRange(SECOND, sec)
)
{
this->Set(YEAR, y);
this->Set(MONTH, m);
this->Set(DAY, d);
this->Set(HOUR, h);
this->Set(MIN, min);
this->Set(SECOND, sec);
return true;
}
}
return false;
}
int32_t ADT::Date::ToTimestamp()
{
struct tm local = {0};
local.tm_year = this->Get(YEAR)-1900;
local.tm_mon = this->Get(MONTH)-1;
local.tm_mday = this->Get(DAY);
local.tm_hour = this->Get(HOUR);
local.tm_min = this->Get(MIN);
local.tm_sec = this->Get(SECOND);
time_t t = mktime(&local);
return (int32_t)t;
}
bool ADT::Date::FromTimestamp(int32_t time)
{
time_t time_s = (time_t)time;
struct tm local = *(localtime(&time_s));
if (this->InRange(YEAR, local.tm_year+1900) &&
this->InRange(MONTH, local.tm_mon+1) &&
this->InRange(DAY, local.tm_mday) &&
this->InRange(HOUR, local.tm_hour) &&
this->InRange(MIN, local.tm_min) &&
this->InRange(SECOND, local.tm_sec)
)
{
this->Set(YEAR, local.tm_year+1900);
this->Set(MONTH, local.tm_mon+1);
this->Set(DAY, local.tm_mday);
this->Set(HOUR, local.tm_hour);
this->Set(MIN, local.tm_min);
this->Set(SECOND, local.tm_sec);
return true;
}
return false;
}
bool ADT::Date::Set(Type_e t, int num)
{
int range_i = (int)t;
if ((range_i >=0) && (range_i < 6))
{
if (this->InRange(t, num))
{
m_DateInt[range_i] = num;
return true;
}
}
return false;
}
int ADT::Date::Get(Type_e t)
{
int range_i = (int)t;
if ((range_i >=0) && (range_i < 6))
{
return m_DateInt[range_i];
}
return -1;
}
bool ADT::Date::InRange(Type_e t, int num)
{
int range_i = (int)t;
if ((range_i >=0) && (range_i < 6))
{
if (num >= c_Ranges[range_i].min)
{
if ((-1 != c_Ranges[range_i].max) && (num > c_Ranges[range_i].max))
{
return false;
}
return true;
}
}
return false;
}
void ADT::Date::CopyFrom(Date &dt)
{
this->Set(YEAR, dt.Get(YEAR));
this->Set(MONTH, dt.Get(MONTH));
this->Set(DAY, dt.Get(DAY));
this->Set(HOUR, dt.Get(HOUR));
this->Set(MIN, dt.Get(MIN));
this->Set(SECOND, dt.Get(SECOND));
return;
}
void ADT::Date::Reset()
{
time_t cur_time = time(NULL);
this->FromTimestamp((uint32_t)cur_time);
return;
}
ADT::Date::Date()
{
// 初始化关键字
m_DateInt[YEAR] = 0;
m_DateInt[MONTH] = 0;
m_DateInt[DAY] = 0;
m_DateInt[HOUR] = 0;
m_DateInt[MIN] = 0;
m_DateInt[SECOND] = 0;
// 更新时间
this->Reset();
}
ADT::Date::~Date(){}