티스토리 뷰
[원본 - 2013년 이글루스]
목적 : javascript의 Date() 객체와 같은 녀석을 C++ 클래스로 만들어 보고 싶었음
이슈 : Month 값을 0 ~ 11 로 하지 않고 1 ~ 12 로 입력/출력 하도록 했음
: Format 문자열의 경우, strftime() 의 그것을 사용함 msdn 이나 cpluslpus 참조
이하 Source Code:
CDate.h
#ifndef _DATE_H_
#define _DATE_H_
#include <time.h>
class CDate
{
private:
time_t m_nTime;
struct tm m_tm;
public:
CDate();
CDate( time_t time );
CDate( int year, int month, int day, int hour, int min, int sec );
CDate( const CDate& date );
~CDate();
public:
// Format String
int format( char* buf, int bufSize, const char* fmtString );
public:
int getMEndDay(); // Returns the end of day of the month (from 28-31)
int getMDay(); // Returns the day of the month (from 1-31)
int getWDay(); // Returns the day of the week (from 0-6)
int getFullYear(); // Returns the year (four digits)
int getHours(); // Returns the hour (from 0-23)
int getMinutes(); // Returns the minutes (from 0-59)
int getMonth(); // Returns the month (from 1-12)
int getSeconds(); // Returns the seconds (from 0-59)
time_t getTime(); // Returns the number of seconds since midnight Jan 1, 1970
void setDate( int year, int month, int day );
void setDate( int year, int month, int day, int hour, int min, int sec );
void setDay( int day ); // Sets the day of the month of a date object
void setFullYear( int year ); // Sets the year (four digits) of a date object
void setHours( int hour ); // Sets the hour of a date object
void setMinutes( int min ); // Set the minutes of a date object
void setMonth( int month ); // Sets the month of a date object
void setSeconds( int sec ); // Sets the seconds of a date object
void setTime( time_t time ); // Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970
};
#endif
CDate.cpp
#include "CDate.h"
#include <string.h>
inline static struct tm* _localtime( time_t* time, struct tm* _tm )
{
#ifdef _WIN32
localtime_s( _tm, time );
#else
localtime_r( time, _tm );
#endif
return _tm;
}
CDate::CDate()
{
m_nTime = time(NULL);
_localtime( &m_nTime, &m_tm );
}
CDate::CDate( time_t time )
{
m_nTime = time;
_localtime( &m_nTime, &m_tm );
}
CDate::CDate( int year, int month, int day, int hour, int min, int sec )
{
struct tm tmp_tm;
memset( &tmp_tm, 0, sizeof( tmp_tm ) );
tmp_tm.tm_year = year - 1900;
tmp_tm.tm_mon = month - 1;
tmp_tm.tm_mday = day;
tmp_tm.tm_hour = hour;
tmp_tm.tm_min = min;
tmp_tm.tm_sec = sec;
setTime( mktime( &tmp_tm ) );
}
CDate::CDate( const CDate& date )
{
m_nTime = date.m_nTime;
_localtime( &m_nTime, &m_tm );
}
CDate::~CDate()
{
}
int CDate::format( char* buf, int bufSize, const char* fmtString )
{
return strftime( buf, bufSize, fmtString, &m_tm );
}
int CDate::getMEndDay()
{
static unsigned char month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if( m_tm.tm_mon == 1 ) {
int year = m_tm.tm_year + 1900;
if( year%4==0 && ( year%100!=0 || year%400==0 ) ) {
return 29;
}
}
return (int)month_days[ m_tm.tm_mon ];
}
int CDate::getMDay()
{
return m_tm.tm_mday;
}
int CDate::getWDay()
{
return m_tm.tm_wday;
}
int CDate::getFullYear()
{
return m_tm.tm_year + 1900;
}
int CDate::getHours()
{
return m_tm.tm_hour;
}
int CDate::getMinutes()
{
return m_tm.tm_min;
}
int CDate::getMonth()
{
return m_tm.tm_mon + 1;
}
int CDate::getSeconds()
{
return m_tm.tm_sec;
}
time_t CDate::getTime()
{
return m_nTime;
}
void CDate::setDate( int year, int month, int day )
{
m_tm.tm_year = year - 1900;
m_tm.tm_mon = month - 1;
m_tm.tm_mday = day;
setTime( mktime( &m_tm ) );
}
void CDate::setDate( int year, int month, int day, int hour, int min, int sec )
{
m_tm.tm_year = year - 1900;
m_tm.tm_mon = month - 1;
m_tm.tm_mday = day;
m_tm.tm_hour = hour;
m_tm.tm_min = min;
m_tm.tm_sec = sec;
setTime( mktime( &m_tm ) );
}
void CDate::setDay( int day )
{
m_tm.tm_mday = day;
setTime( mktime( &m_tm ) );
}
void CDate::setFullYear( int year )
{
m_tm.tm_year = year - 1900;
setTime( mktime( &m_tm ) );
}
void CDate::setHours( int hour )
{
m_tm.tm_hour = hour;
setTime( mktime( &m_tm ) );
}
void CDate::setMinutes( int min )
{
m_tm.tm_min = min;
setTime( mktime( &m_tm ) );
}
void CDate::setMonth( int month )
{
m_tm.tm_mon = month - 1;
setTime( mktime( &m_tm ) );
}
void CDate::setSeconds( int sec )
{
m_tm.tm_sec = sec;
setTime( mktime( &m_tm ) );
}
void CDate::setTime( time_t time )
{
m_nTime = time;
_localtime( &m_nTime, &m_tm );
}
'개발 관련 정리 (구 블로그에서 백업) > Language 관련' 카테고리의 다른 글
[C/C++] 한글 초성 추출 (0) | 2018.11.26 |
---|---|
[C/C++] MACRO함수 사용시 일어날 수 있는 문제들 (0) | 2018.11.26 |
[C/C++/JAVA] 숫자를 고려한 파일 이름 정렬 (0) | 2018.11.26 |
[C/C++] 어쩌다 저지를 수 있는 for문 스코프 런타임 오류 (0) | 2018.11.26 |
[C/C++] 2차원 배열의 동적 할당 (0) | 2018.11.26 |
댓글