下面是linux日期包装器,没有用到localtime_r,其实应该也没有什么关系。
Data.h
#ifndef _DATE_H_ #define _DATE_H_ #include <time.h> class Date { protected: time_t sysTime; int year; int month; int day; int hour; int min; int sec; int week; struct tm *localDate; public: Date(); Date(time_t value); Date( int year, int month, int day, int hour = 0, int min = 0, int sec = 0); public: time_t getValue() { return sysTime; } public: int getYear(); int getMonth(); int getDay(); int getWeekDay(); public: int getHour(); int getMinute(); int getSecond(); }; #endif
Data.cpp
#include <Date.h> #include <stdio.h> #include <stdlib.h> Date::Date() { time(&sysTime); localDate = localtime(&sysTime); } Date::Date(SysTime value) { sysTime = value; localDate = localtime(&sysTime); } Date::Date( int year, int month, int day, int hour, int min, int sec) { struct tm tmval; tmval.tm_sec = sec; tmval.tm_min = min; tmval.tm_hour = hour; tmval.tm_mday = day; tmval.tm_mon = month - 1; tmval.tm_year = year - 1900; tmval.tm_wday = 0; tmval.tm_yday = 0; tmval.tm_isdst = 0; sysTime = mktime(&tmval); localDate = localtime(&sysTime); } int Date::getYear() { return localDate->tm_year + 1900; } int Date::getMonth() { return localDate->tm_mon + 1; } int Date::getDay() { return localDate->tm_mday; } int Date::getWeekDay() { return localDate->tm_wday; } int Date::getHour() { return localDate->tm_hour; } int Date::getMinute() { return localDate->tm_min; } int Date::getSecond() { return localDate->tm_sec; }