现在的位置: 首页 > 综合 > 正文

日期包装器

2018年03月31日 ⁄ 综合 ⁄ 共 1255字 ⁄ 字号 评论关闭

下面是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;
}

【上篇】
【下篇】

抱歉!评论已关闭.