日期包装器

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值