一、学到的知识
ctime
库中的 time_t,tm,time(),locatime_s()的使用,它们可以帮助我们获取系统的本地时间- 注意 ctime 库中的时间结构体的
时间起始值
,比如年份起始是1900,月份起始是0,想要正确的显示时间,必须加上对应的值。 - 可以在 Time 类中加入
常见的时间格式
,方便外部直接调用 - C++ 的延迟函数 Sleep 已经被废弃了, 推荐使用
std::this_thread::sleep_for(std::chrono::milliseconds(Milliseconds));
需要加上 thread 库
二、代码
非完整项目代码
Time.h
/**
* @brief 学习C++
* @author Moota
* @Date 2023-02-22
*/
#pragma once
#include <ctime>
#include <thread>
#include "../../Container/String/String.h"
#include "../../../Core/Base/Object.h"
/**
* @brief 时间类,存储时间信息,支持获取时间相关的操作
*/
class FTime : public UObject
{
private:
// 时间戳
time_t Now;
// 时间信息结构体
tm LocalTime{};
// 起始的绝对时间
static tm AbsoluteTime;
public:
FTime();
public:
/**
* @brief 获得时间戳
* @return 时间戳
*/
FString GetTimestamp() const
{
return Now;
}
/**
* @brief 获得年份
* @return 年份(1900-xxxx)
*/
FString GetYear() const
{
return LocalTime.tm_year;
}
/**
* @brief 获得月份
* @return 月份(1-12)
*/
FString GetMonth() const
{
return LocalTime.tm_mon;
}
/**
* @brief 获得一月的第几天
* @return 天数(1-31)
*/
FString GetDay() const
{
return LocalTime.tm_mday;
}
/**
* @brief 获得小时
* @return 小时(0-23)
*/
FString GetHour() const
{
return LocalTime.tm_hour;
}
/**
* @brief 获得分钟
* @return 分钟(0-59)
*/
FString GetMinute() const
{
return LocalTime.tm_min;
}
/**
* @brief 获得秒数
* @return 秒数(0-60)
*/
FString GetSecond() const
{
return LocalTime.tm_sec;
}
/**
* @brief 获得一月的第几周
* @return 周数
*/
FString GetWeek() const
{
return LocalTime.tm_mday % 7 + 1;
}
/**
* @brief 获得一周的第几天
* @return 天数
*/
FString GetWeekDay() const
{
return LocalTime.tm_wday;
}
/**
* @brief 获得一年的第几天
* @return 天数
*/
FString GetYearDay() const
{
return LocalTime.tm_yday;
}
public:
/**
* @brief 获得年月日
* 格式为xx年xx月xx日
* 月,日不足两位时,不前补0
* @return 时间(xx年x月x日)
*/
FString GetYMD() const;
/**
* @brief 获得年月日
* 格式为xx-xx-xx
* 月,日不足两位时,不前补0
* @return 时间(xx-x-x)
*/
FString GetYMDWithHyphen() const;
/**
* @brief 获得年月日
* 格式为xx年xx月xx日
* 月,日不足两位时,前补0
* @return 时间(xx月xx月xx日)
*/
FString GetYMDWith00() const;
/**
* @brief 获得年月日
* 格式为xx-xx-xx
* 月,日不足两位时,前补0
* @return 时间(xx-xx-xx)
*/
FString GetYMDWith00WithHyphen() const;
/**
* @brief 获得时分秒
* 时,分,秒不足两位时,不前补0
* @return 时间(xx:x:x)
*/
FString GetHMS() const;
/**
* @brief 获得时分秒
* 时,分,秒不足两位时,前补0
* @return 时间(xx:xx:xx)
*/
FString GetHMSWith00() const;
public:
/**
* @brief 使用指定的毫秒数,延迟当前线程
* @todo 暂时将延迟的功能写在时间类,后续应该放置在线程类
*/
static void Delay(int Milliseconds)
{
std::this_thread::sleep_for(std::chrono::milliseconds(Milliseconds));
}
};
Time.cpp
#include "Time.h"
/**
* 数值根据tm结构体的起始值与当前时间的差值而定
*/
tm FTime::AbsoluteTime = {
0, 0, 0, 0, 1, 1900, 0, 1, 0
};
FTime::FTime()
{
Now = time(nullptr);
localtime_s(&LocalTime, &Now);
LocalTime = {
AbsoluteTime.tm_sec + LocalTime.tm_sec,
AbsoluteTime.tm_min + LocalTime.tm_min,
AbsoluteTime.tm_hour + LocalTime.tm_hour,
AbsoluteTime.tm_mday + LocalTime.tm_mday,
AbsoluteTime.tm_mon + LocalTime.tm_mon,
AbsoluteTime.tm_year + LocalTime.tm_year,
AbsoluteTime.tm_wday + LocalTime.tm_wday,
AbsoluteTime.tm_yday + LocalTime.tm_yday,
AbsoluteTime.tm_isdst + LocalTime.tm_isdst
};
}
FString FTime::GetYMD() const
{
return GetYear() + "年" + GetMonth() + "月" + GetDay() + "日";
}
FString FTime::GetYMDWithHyphen() const
{
return GetYear() + "-" + GetMonth() + "-" + GetDay() + "-";
}
FString FTime::GetYMDWith00() const
{
FString Month = GetMonth();
if (Month.GetSize() == 1) Month = "0" + Month;
FString Day = GetDay();
if (Day.GetSize() == 1) Day = "0" + Day;
return GetYear() + "年" + Month + "月" + Day + "日";
}
FString FTime::GetYMDWith00WithHyphen() const
{
FString Month = GetMonth();
if (Month.GetSize() == 1) Month = "0" + Month;
FString Day = GetDay();
if (Day.GetSize() == 1) Day = "0" + Day;
return GetYear() + "-" + Month + "-" + Day;
}
FString FTime::GetHMS() const
{
return GetHour() + ":" + GetMinute() + ":" + GetSecond();
}
FString FTime::GetHMSWith00() const
{
FString Hour = GetHour();
if (Hour.GetSize() == 1) Hour = "0" + Hour;
FString Minute = GetMinute();
if (Minute.GetSize() == 1) Minute = "0" + Minute;
FString Second = GetSecond();
if (Second.GetSize() == 1) Second = "0" + Second;
return Hour + ":" + Minute + ":" + Second;
}