发布一个c++写的简易datetime库

来源:互联网 发布:地磅单打印软件 编辑:程序博客网 时间:2024/06/05 20:51

发布一个c++写的简易datetime库

代码:

//DateTime.h#ifndef DATETIME_H#define DATETIME_H#include <ctime>#include <string>#include <stdint.h>using namespace std;class Year;class Month;class Day;class Hour;class Minute;class Second;class DateTime{public:    DateTime(time_t seconds=0);    DateTime(struct tm time_tm);    DateTime(int year,int month=0,int day=0,int hour=0,int minute=0,int second=0);    DateTime(const string& datetimeStr);//(yyyy-mm-dd hh:mm:ss) or (yyyymmdd)    int sec();    int min();    int hour();    int mday();    int mon();    int year();    int wday();    int yday();    DateTime operator+(const Year& year);    DateTime operator+(const Month& month);    DateTime operator+(const Day& day);    DateTime operator+(const Hour& hour);    DateTime operator+(const Minute& minute);    DateTime operator+(const Second& second);    DateTime operator-(const Year& year);    DateTime operator-(const Month& month);    DateTime operator-(const Day& day);    DateTime operator-(const Hour& hour);    DateTime operator-(const Minute& minute);    DateTime operator-(const Second& second);    bool operator>(const DateTime& rv);    bool operator==(const DateTime& rv);    bool operator<(const DateTime& rv);    bool operator>=(const DateTime& rv);    bool operator<=(const DateTime& rv);    bool operator!=(const DateTime& rv);    static bool isLeap(uint16_t year);    static uint16_t lastDayOfMonth(uint16_t year, uint16_t month);    uint16_t lastDayOfMonth();    /*    %F  2001-08-23    %T  14:55:02    %D  08/23/01    */    string to_string(const string& format="%F %T");    string to_num_string(short len=8);//yyyymmdd    struct tm to_tm();    time_t to_time();private:    time_t _seconds;//自1970-01-01 08:00:00起的秒数;};class Year{public:    Year(uint16_t year):_year(year){}    uint16_t _year;};class Month{public:    Month(uint16_t month):_month(month){}    uint16_t _month;};class Day{public:    Day(uint16_t day):_day(day){}    uint16_t _day;};class Hour{public:    Hour(uint16_t hour):_hour(hour){}    uint16_t _hour;};class Minute{public:    Minute(uint16_t minute):_minute(minute){}    uint16_t _minute;};class Second{public:    Second(uint16_t second):_second(second){}    uint16_t _second;};#endif // DATETIME_H
//DateTime.cpp#include "DateTime.h"#include <stdio.h>DateTime::DateTime(time_t seconds):_seconds(seconds){    if(_seconds == 0)    {        time(&_seconds);    }}DateTime::DateTime(struct tm time_tm){    _seconds=mktime(&time_tm);}DateTime::DateTime(int year,int month,int day,int hour,int minute,int second){    struct tm time_tm;    //time_t seconds;    time_tm.tm_year = year - 1900;    time_tm.tm_mon = month -1;    time_tm.tm_mday = day;    time_tm.tm_hour = hour;    time_tm.tm_min=  minute;    time_tm.tm_sec =  second;    time_tm.tm_isdst = 0;    _seconds=mktime(&time_tm);}DateTime::DateTime(const string& datetimeStr){    if(datetimeStr.length() == 19)    {        struct tm time_tm;        sscanf(datetimeStr.c_str(), "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);        time_tm.tm_year -= 1900;        time_tm.tm_mon -= 1;        _seconds=mktime(&time_tm);    }    else if(datetimeStr.length() == 8)    {        struct tm time_tm;        sscanf(datetimeStr.c_str(), "%4d%2d%2d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday);        time_tm.tm_year -= 1900;        time_tm.tm_mon -= 1;        time_tm.tm_hour = 0;        time_tm.tm_min = 0;        time_tm.tm_sec = 0;        _seconds=mktime(&time_tm);    }    else    {        _seconds=0;    }}int DateTime::sec(){    return to_tm().tm_sec;}int DateTime::min(){    return to_tm().tm_min;}int DateTime::hour(){    return to_tm().tm_hour;}int DateTime::mday(){    return to_tm().tm_mday;}int DateTime::mon(){    return to_tm().tm_mon+1;}int DateTime::year(){    return to_tm().tm_year+1900;}int DateTime::wday(){    return to_tm().tm_wday;}int DateTime::yday(){    return to_tm().tm_yday;}bool DateTime::isLeap(uint16_t year){    if(year%4==0&&year%100!=0||year%400==0)return true;    return false;}uint16_t DateTime::lastDayOfMonth(uint16_t year, uint16_t month){    if(month==0||month>12)return 0;    short leep_year[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};    return leep_year[isLeap(year)][month-1];}uint16_t DateTime::lastDayOfMonth(){    return lastDayOfMonth(year(),mon());}DateTime DateTime::operator+(const Year& year){    return operator+(Month(12));}DateTime DateTime::operator+(const Month& month){    struct tm time_tm = to_tm();    time_tm.tm_year += (time_tm.tm_mon+month._month)/12;    time_tm.tm_mon = (time_tm.tm_mon+month._month)%12;    uint16_t last_day = lastDayOfMonth(time_tm.tm_year+1900, time_tm.tm_mon+1);    if(time_tm.tm_mday>last_day)time_tm.tm_mday = last_day;    return DateTime(mktime(&time_tm));}DateTime DateTime::operator+(const Day& day){    return DateTime(_seconds+24*60*60*day._day);}DateTime DateTime::operator+(const Hour& hour){    return DateTime(_seconds+60*60*hour._hour);}DateTime DateTime::operator+(const Minute& minute){    return DateTime(_seconds+60*minute._minute);}DateTime DateTime::operator+(const Second& second){    return DateTime(_seconds+second._second);}DateTime DateTime::operator-(const Year& year){    return operator-(Month(12));}DateTime DateTime::operator-(const Month& month){    struct tm time_tm = to_tm();    if(month._month<=time_tm.tm_mon)time_tm.tm_mon-=month._month;    else    {        time_tm.tm_year-=((month._month-time_tm.tm_mon)/12+1);        time_tm.tm_mon = 12-(month._month-time_tm.tm_mon)%12;    }    uint16_t last_day = lastDayOfMonth(time_tm.tm_year+1900, time_tm.tm_mon+1);    if(time_tm.tm_mday>last_day)time_tm.tm_mday = last_day;    return DateTime(mktime(&time_tm));}DateTime DateTime::operator-(const Day& day){    return DateTime(_seconds-24*60*60*day._day);}DateTime DateTime::operator-(const Hour& hour){    return DateTime(_seconds-60*60*hour._hour);}DateTime DateTime::operator-(const Minute& minute){    return DateTime(_seconds-60*minute._minute);}DateTime DateTime::operator-(const Second& second){    return DateTime(_seconds-second._second);}bool DateTime::operator>(const DateTime& rv){    return _seconds>rv._seconds;}bool DateTime::operator==(const DateTime& rv){    return _seconds==rv._seconds;}bool DateTime::operator<(const DateTime& rv){    return _seconds<rv._seconds;}bool DateTime::operator>=(const DateTime& rv){    return _seconds>=rv._seconds;}bool DateTime::operator<=(const DateTime& rv){    return _seconds<=rv._seconds;}bool DateTime::operator!=(const DateTime& rv){    return _seconds!=rv._seconds;}string DateTime::to_string(const string& format){    char buffer[40]={0};    struct tm time_tm;    localtime_r(&_seconds,&time_tm);    strftime(buffer,sizeof(buffer),format.c_str(),&time_tm);    return string(buffer);}string DateTime::to_num_string(short len){    char buffer[20] ={0};    struct tm time_tm;    localtime_r(&_seconds,&time_tm);    snprintf(buffer,sizeof(buffer),"%04d%02d%02d%02d%02d%02d",time_tm.tm_year + 1900,time_tm.tm_mon+1,time_tm.tm_mday,        time_tm.tm_hour,time_tm.tm_min,time_tm.tm_sec);    return string(buffer).substr(0,len);}struct tm DateTime::to_tm(){    struct tm time_tm;    localtime_r(&_seconds, &time_tm);    return time_tm;}time_t DateTime::to_time(){    return _seconds;}

主要参考boost 的datetime库实现,有问题欢迎指正建议

0 0
原创粉丝点击