c++中获取当前的时间

本文深入探讨了C++中<ctime>头文件的使用,详细解析了time()和localtime()函数的功能,展示了如何获取和转换时间戳。同时,通过一个完整的代码示例,介绍了自定义Date和Time类的实现,包括日期加减和时间字符串的格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值