C ++中的日期格式

时间:2018-10-25 15:32:01

标签: c++ string

这是我的课程

#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)中。有点像我需要以某种方式读取此字符串格式,检测什么是什么并将其存储,但是如何?

3 个答案:

答案 0 :(得分:1)

获取日历日期和时间以进行正确验证和操作非常困难。实际上,只有少数人能够成功正确有效地做到这一点。

因此,如果可能,请避免自行研究。在C ++中,使用<chrono>处理日期和时间。如果需要解析和格式化,请使用基于<chrono>的{​​{3}}。 Howard Hinnant's date library也可以选择,但根据我的经验较弱。

答案 1 :(得分:0)

您可以尝试这样的事情:

#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字符串。因此,您首先需要将date转换为c_str()。如果您在Linux下运行程序,请使用sscanf而不是sscanf_s

答案 2 :(得分:0)

您可以使用正则表达式来解决此问题。喜欢:

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