日期格式为C++

Date format in C++

本文关键字:C++ 格式 日期      更新时间:2023-10-16

这是我的类

#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/