日期格式为C++
Date format in C++
这是我的类
#ifndef DATE_H
#define DATE_H
#include <initializer_list>
#include <string>
class Date {
public:
/**
* @brief default constructor
*/
Date();
/**
* @brief constructor with arguments
*/
Date(int t_year, int t_month, int t_day, int t_hour, int t_minute);
/**
* @brief constructor with a string
*/
Date(const std::string &dateString);
/**
* @brief return the year of a Date
* @return a integer indicate the year of a date
*/
int getYear(void) const;
/**
* @brief set the year of a date
* @param a integer indicate the new year of a date
*/
void setYear(const int t_year);
/**
* @brief return the month of a Date
* @return a integer indicate the month of a date
*/
int getMonth(void) const;
/**
* @brief set the month of a date
* @param a integer indicate the new month of a date
*/
void setMonth(const int t_month);
/**
* @brief return the day of a Date
* @return a integer indicate the day of a date
*/
int getDay(void) const;
/**
* @brief set the day of a date
* @param a integer indicate the new day of a date
*/
void setDay(const int t_day);
/**
* @brief return the hour of a Date
* @return a integer indicate the hour of a date
*/
int getHour(void) const;
/**
* @brief set the hour of a date
* @param a integer indicate the new hour of a date
*/
void setHour(const int t_hour);
/**
* @brief return the minute of a Date
* @return a integer indicate the minute of a date
*/
int getMinute(void) const;
/**
* @brief set the minute of a date
* @param a integer indicate the new minute of a date
*/
void setMinute(const int t_minute);
/**
* @brief check whether the date is valid or not
* @return the bool indicate valid or not
*/
static bool isValid(const Date &t_date);
/**
* @brief convert a string to date, if the format is not correct return
* 0000-00-00/00:00
* @return a date
*/
static Date stringToDate(const std::string &t_dateString);
/**
* @brief convert a date to string, if the date is invalid return
* 0000-00-00/00:00
*/
static std::string dateToString(const Date &t_date);
/**
* @brief overload the assign operator
*/
Date &operator=(const Date &t_date);
/**
* @brief check whether the CurrentDate is equal to the t_date
*/
bool operator==(const Date &t_date) const;
/**
* @brief check whether the CurrentDate is greater than the t_date
*/
bool operator>(const Date &t_date) const;
/**
* @brief check whether the CurrentDate is less than the t_date
*/
bool operator<(const Date &t_date) const;
/**
* @brief check whether the CurrentDate is greater or equal than the t_date
*/
bool operator>=(const Date &t_date) const;
/**
* @brief check whether the CurrentDate is less than or equal to the t_date
*/
bool operator<=(const Date &t_date) const;
private:
int m_year;
int m_month;
int m_day;
int m_hour;
int m_minute;
};
#endif
所以我得到了 3 个构造函数,1(默认 2( 带有参数 3( 带有字符串。
在第三个中,我得到一个格式的日期字符串(示例(:2018-10-25/14:00(即 yy-mm-dd/hh:mm(。所以我需要将这些值存储到我的私有变量(m_year,m_month,m_day,m_hour,m_minute(中。这有点像我需要以某种方式读取这种字符串格式,检测什么是什么并存储它,但是如何?
你可以尝试这样的事情:
#include <stdio.h>
#include <iostream>
#include <iomanip>
int main(int argc, char* argv[]) {
std::string date = "2018-05-07/09:00";
int t_year, t_month, t_day, t_hour, t_minute;
sscanf_s(date.c_str(), "%4d-%2d-%2d/%2d:%2d",
&t_year,
&t_month,
&t_day,
&t_hour,
&t_minute);
std::cout << t_year <<
"-" << std::setfill('0') << std::setw(2) << t_month <<
"-" << std::setfill('0') << std::setw(2) << t_day <<
"/" << std::setfill('0') << std::setw(2) << t_hour <<
":" << std::setfill('0') << std::setw(2) << t_minute << std::endl;
return 0;
}
输出:
2018-05-07/09:00
请注意,对于sscanf_s
,您需要使用 C 字符串。因此,您首先需要使用c_str()
转换date
。如果您在 Linux 下运行该程序,请使用sscanf
而不是sscanf_s
.
正确验证和操作日历日期和时间是一项非常困难的任务。实际上,只有少数人能够成功正确有效地做到这一点。
因此,如果可能的话,避免自己研究。在C++使用<chrono>
来处理日期和时间。如果您需要解析和格式化,请使用 Howard Hinnant 的日期库,该库基于<chrono>
构建。Boost.Date_Time也是可选的,但根据我的经验较弱。
您可以使用正则表达式来解决此问题。喜欢:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
string input("2018-10-25/14:00");
regex format("(\d+)-(\d+)-(\d+)/(\d+):(\d++)");
smatch result;
int year, month, day, hour, minute;
if (regex_match(input, result, format))
{
year = stoi(result[1].str());
month = stoi(result[2].str());
day = stoi(result[3].str());
hour = stoi(result[4].str());
minute = stoi(result[5].str());
}
cout << year << "-" << month << "-" << day
<< "/" << hour << ":" << minute << endl;
system("pause");
return 0;
}
详细信息: http://www.cplusplus.com/reference/regex/
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何将strftime中的格式错误作为异常捕获
- 将CHW格式的浮点向量转换为cv::Mat
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 如何以叮当格式设置评论的行长?
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- Vulkan 中的动态顶点缓冲区格式设置