C++定义在<ctime>头文件中
一、time(time_t*)函数 函数定义如下:
time_t time (time_t* timer);
获取系统当前日历时间 UTC 1970-01-01 00:00:00开始的unix时间戳。
返回值:如果成功,获取当前系统日历时间,否则返回 -1。
二、本地时间转换函数
localtime(time_t*) struct tm * localtime (const time_t * timer);
将日历时间转换为本地时间,从1970年起始的时间戳转换为1900年起始的时间数据结构
附上一个相关代码
demo.cpp
#include "date.h"
#include <windows.h>
#include <iostream>
#include "date.cpp"
#include "time.cpp"
using std::cout;
using std::endl;
int main()
{
try
{
Date exam(2020, 7, 3);
cout << "now is " << exam.toString('-') << " " << exam.getTime().toString() << endl;
}
catch (const char *exp)
{
std::cout << exp << std::endl;
}
std::cout << "this is the end of the program" << std::endl;
system("pause");
return 0;
}
time.h
#pragma once
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <cstdio>
using namespace std;
class Time
{
private:
int second;
int minute;
int hour;
public:
Time(int, int, int);
Time();
int getSec() const;
int getMin() const;
int getHour() const;
bool setSec(int);
bool setMin(int);
bool setHour(int);
string toString();
};
time.cpp
#include "time.h"
Time::Time(int h, int m, int s)
{
if (h >= 0 && h <= 12)
if (m >= 0 && h <= 60)
if (s >= 0 && s <= 60)
{
second = s;
minute = m;
hour = h;
}
else
throw "hour is invalid";
else
throw "minute is invalid";
else
throw "second is invalid";
}
Time::Time()
{
time_t now;
time(&now);
struct tm *t_now;
t_now = localtime(&now);
hour = t_now->tm_hour;
minute = t_now->tm_min;
second = t_now->tm_sec;
}
int Time::getSec() const
{
return second;
}
int Time::getMin() const
{
return minute;
}
int Time::getHour() const
{
return hour;
}
bool Time::setSec(int sec)
{
if (sec >= 0 && sec <= 60)
{
second = sec;
return true;
}
else
return false;
}
bool Time::setMin(int minute_)
{
if (minute_ >= 0 && minute_ <= 60)
{
minute = minute_;
return true;
}
else
return false;
}
bool Time::setHour(int hour_)
{
if (hour_ >= 0 && hour_ <= 12)
{
hour = hour_;
return true;
}
else
return false;
}
string Time::toString()
{
return to_string(hour) + ":" + to_string(minute) + ":" + to_string(second);
}
date.h
#pragma once
#include <string>
#include "time.h"
using std::string;
class Date
{
public:
Date(int, int, int);
Date();
void addDay(int);
string toString(char);
static Date *getInstance();
bool setYear(int year_);
int getYear() const;
bool setMon(int month_);
int getMon() const;
bool setDay(int day_);
int getDay() const;
Time getTime() const;
private:
int year;
int month;
int day;
static const int maxDay[2][13];
static Date *mpDate;
Time time;
};
date.cpp
#pragma once
#include <iostream>
#include <ctime>
#include <cassert>
#include "date.h"
using std::cout;
using std::endl;
const int Date::maxDay[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
Date::Date(int year_, int month_ = 1, int day_ = 1)
{
if (year_ >= 1900 && year_ <= 9999)
if (month_ >= 1 && month_ <= 12)
if (day_ >= 1 && day_ <= maxDay[(year_ % 4 == 0 && year_ % 100 != 0) || year_ % 400 == 0][month_])
{
year = year_;
month = month_;
day = day_;
}
else
throw "day is invalid";
else
throw "month is invalid!";
else
throw "year is invalid!";
}
Date::Date()
{
time_t now;
std::time(&now);
struct tm *t_now;
t_now = localtime(&now);
year = t_now->tm_year + 1900;
month = t_now->tm_mon + 1;
day = t_now->tm_mday;
}
void Date::addDay(int n)
{
day += n;
if (day > maxDay[(year % 4 == 0 && year % 100 != 0) || year % 400 == 0][month])
{
day %= maxDay[(year % 4 == 0 && year % 100 != 0) || year % 400 == 0][month];
month++;
if (month > 12)
{
year++;
month %= 12;
}
}
}
string Date::toString(char spor)
{
return std::to_string(year) + spor + std::to_string(month) + spor + std::to_string(day);
}
bool Date::setYear(int year_)
{
if (year_ >= 1900 && year_ <= 2120)
{
year = year_;
return true;
}
else
return false;
}
int Date::getYear() const
{
return year;
}
bool Date::setMon(int month_)
{
if (month_ >= 1 && month_ <= 12)
{
month = month_;
return true;
}
else
return false;
}
int Date::getMon() const
{
return month;
}
bool Date::setDay(int day_)
{
if (day_ >= 1 && day_ <= maxDay[(year % 4 == 0 && year % 100 != 0) || year % 400 == 0][month])
{
day = day_;
return true;
}
else
return false;
}
int Date::getDay() const
{
return day;
}
Time Date::getTime() const
{
return time;
}
原文链接: https://www.cnblogs.com/stu-jyj3621/p/12900364.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/348784
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!