一個好用的日期時間類(c++)

來源:互聯網
上載者:User


/**********************************************************/
//name          : datetime.h
//function      : 時間日期類的定義
//copyright     :
//author        :
//date          : 2007-08-27
/**********************************************************/
#ifndef DATETIME_H_INCLUDE_
#define DATETIME_H_INCLUDE_
#include <time.h>
#include <sys/timeb.h>
#include <string>

class CDateTime
{
private:
 bool m_millisecondflag;
 struct timeb tp;
 time_t m_tTime;
 struct tm m_stTime;
public:
 CDateTime();
 CDateTime(time_t tTime);
    CDateTime(const struct tm& stTime);
 CDateTime(int iYear,int iMonth,int iDay,int iHour = 0,int iMinute = 0,int iSecond = 0);
 time_t GetValue();
    const struct tm& GetStruct();
 int Year();
 int Month();
 int Day();
 int Hour();
 int Minute();
 int Second();
 int DayOfWeek();
 int DayOfYear();
 int MilliSecond();
 //最大支援到差異 1000 * 61 * 60 毫秒
 int SubMilliSecond(CDateTime &dt);
 const std::string ShortDateTime();
 const std::string LongDateTime(char chSep1 = '-',char chSep2 = ':');
 const std::string LongDateTimeWithMilliSec(char chSep1 = '-',char chSep2 = ':');
 const std::string ShortDate();
 const std::string LongDate(char chSep = '-');

//增加或者減少幾個自然月
 const std::string GetNewDateByMonth(int newmonth);
 //增加或者減少幾天
 const std::string GetNewDateByDay(int newday);
};
#endif

 


/**********************************************************/
//name          : datetime.cpp
//function      : 時間日期類的實現
//copyright     :
//author        :
//date          : 2007-08-27
/**********************************************************/

#include <stdio.h>
#include "datetime.h"
using namespace std;

CDateTime::CDateTime():m_millisecondflag(true)

 ftime(&tp);
 m_tTime = tp.time;
#ifdef WIN32
 struct tm * ptr = localtime(&m_tTime);
 if(ptr)
 {
  m_stTime = *ptr;
 }
#else
 localtime_r(&m_tTime,&m_stTime);
#endif
}

CDateTime::CDateTime(time_t tTime):m_millisecondflag(false)
{
 m_tTime = tTime;
#ifdef WIN32
 struct tm * ptr = localtime(&m_tTime);
 if(ptr)
 {
  m_stTime = *ptr;
 }
#else
 localtime_r(&m_tTime,&m_stTime);
#endif
}

CDateTime::CDateTime(const struct tm& stTime):m_millisecondflag(false)
{
 m_stTime = stTime;
 m_tTime = mktime(&m_stTime);
}

CDateTime::CDateTime(int iYear,int iMonth, int iDay,
      int iHour,int iMinute,int iSecond)
      :m_millisecondflag(false)
{
 m_stTime.tm_year = iYear - 1900;
 m_stTime.tm_mon = iMonth - 1;
 m_stTime.tm_mday = iDay;
 m_stTime.tm_hour = iHour;
 m_stTime.tm_min = iMinute;
 m_stTime.tm_sec = iSecond;
 m_tTime = mktime(&m_stTime);
}

const std::string CDateTime::ShortDateTime()
{
 char sDateTime[15] = {0};
 sprintf(sDateTime,"%04d%02d%02d%02d%02d%02d",
  Year(),Month(),Day(),Hour(),Minute(),Second());
 return string(sDateTime);
}

const std::string CDateTime::LongDateTime(char chSep1,char chSep2)
{
 char sDateTime[20] = {0};
 sprintf(sDateTime,"%04d%c%02d%c%02d %02d%c%02d%c%02d",
  Year(),chSep1,Month(),chSep1,Day(),
  Hour(),chSep2,Minute(),chSep2,Second());
 return string(sDateTime);
}

const std::string CDateTime::LongDateTimeWithMilliSec(char chSep1,char chSep2)
{
 char sDateTime[30] = {0};
 sprintf(sDateTime,"%04d%c%02d%c%02d %02d%c%02d%c%02d%c%03d",
  Year(),chSep1,Month(),chSep1,Day(),
  Hour(),chSep2,Minute(),chSep2,Second(),chSep2,MilliSecond());
 return string(sDateTime);
}

const std::string CDateTime::ShortDate()
{
 char sDate[9] = {0};
 sprintf(sDate,"%04d%02d%02d",Year(),Month(),Day());
 return string(sDate);
}

const std::string CDateTime::LongDate(char chSep)
{
 char sDate[11] = {0};
 sprintf(sDate,"%04d%c%02d%c%02d",Year(),chSep,Month(),chSep,Day());
 return string(sDate);
}

time_t CDateTime::GetValue()
{
 return m_tTime;
}

inline const struct tm& CDateTime::GetStruct()

 return m_stTime;
}

inline int CDateTime::Year()
{
 return m_stTime.tm_year + 1900;
}

inline int CDateTime::Month()
{
 return m_stTime.tm_mon + 1;
}

inline int CDateTime::Day()
{
 return m_stTime.tm_mday;
}

inline int CDateTime::Hour()
{
 return m_stTime.tm_hour;
}

inline int CDateTime::Minute()
{
 return m_stTime.tm_min;
}

inline int CDateTime::Second()
{
 return m_stTime.tm_sec;
}

inline int CDateTime::DayOfWeek()
{
 return m_stTime.tm_wday;
}

inline int CDateTime::DayOfYear()
{
 return m_stTime.tm_yday;
}

inline int CDateTime::MilliSecond()
{
 return m_millisecondflag?tp.millitm:0;
}

int CDateTime::SubMilliSecond(CDateTime &dt)
{
 if(m_millisecondflag == false)
 {
  return 0;
 }

 int millisecnds = 0;
 if(this->GetValue() >= dt.GetValue())
 {
  millisecnds = (int)(GetValue() - dt.GetValue()) * 1000;
 }

 if(millisecnds > 1000 * 61 * 60)
 {
  millisecnds = 0;
 }

 millisecnds += MilliSecond() - dt.MilliSecond();

 return m_millisecondflag?millisecnds:0;
}

 

//增加或者減少幾個自然月
const std::string CDateTime::GetNewDateByMonth(int newmonth)
{
 int month = this->Month();
 int year = this->Year();
 month += newmonth;
 if(month % 12 == 0)
 {  
  year += month%12 - 1;
  month = 12;
 }
 else
 {
  year += month/12;
  month = month%12;
 }

 int day = this->Day();
 //校正本月是否有該天
 while(day > 0)
 {
  CDateTime dt(year,month,day);
  if(dt.Day() == day)
  {
   m_tTime = dt.GetValue();
   m_stTime = dt.GetStruct();
   break;
  }
  day--;
 }
 return this->LongDate();
}

//增加或者減少幾天
const std::string CDateTime::GetNewDateByDay(int newday)
{
 m_tTime += newday * 86400;
 #ifdef WIN32
 struct tm * ptr = localtime(&m_tTime);
 if(ptr)
 {
  m_stTime = *ptr;
 }
#else
 localtime_r(&m_tTime,&m_stTime);
#endif
 
 return this->LongDate();
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.