如何将时间返回为DD:HH:MM:SS?

时间:2019-05-16 16:05:15

标签: c++ class time formatting

我想开发一个应用程序,该应用程序从带有设置员的用户那里提供日,时,分和秒的输入。我正在从带有设置员的用户那里正确地获取值。

但是我不能以DD:HH:MM:SS的形式返回值。我编写了一个名为getDate的方法,并希望获得该方法中的所有值,但它仅返回Date2对象的第二个值。

这是我尝试过的。

#include "pch.h"
#include <iostream>
using namespace std;

class Date
{
public:
    int getDate(void)
    {
        return day, std::string(":"), hour, std::string(":"), minute, std::string(":"), second;
    }
    int getDay(void) {
        return day;
    }
    int getHour(void) {
        return hour;
    }
    int getMinute(void) {
        return minute;
    }
    int getSecond(void) {
        return second;
    }
    void setDay(int a) {
        day= a;
    }
    void setHour(int b) {
        hour = b;
    }
    void setMinute(int c) {
        minute = c;
    }
    void setSecond(int d) {
        second = d;
    }

private:    
    int day;
    int hour;
    int minute;
    int second;
};

// Main function for the program
int main()
{
    Date Date1;                
    Date Date2;                                
    int day= 0;     
    int hour= 0;
    int minute = 0;
    int second= 0;
    // Date1 specification
    Date1.setDay(23);
    Date1.setHour(14);
    Date1.setMinute(43);
    Date1.setSecond(21);

    // Tarih2  specification
    Date2.setDay(12);
    Date2.setHour(43);
    Date2.setMinute(25);
    Date2.setSecond(49);
    day = Date1.getDay();
    cout << "Day of Date1 : " << day << endl;
    hour = Date1.getHour();
    cout << "Hour of Date1 : " << hour << endl;
    minute = Date1.getMinute();
    cout << "Minute of Date1 : " << minute << endl;
    second = Date1.getSecond();
    cout << "Second of Date1 : " << second << endl;

    cout << "Date1 : " << Date1.getDate()<< endl;

    cout << "Date2 : " << Date2.getDate()<< endl;
    return 0;
}

4 个答案:

答案 0 :(得分:2)

您的int getDate(void)函数错误。要将成员变量的格式设置为DD:HH:MM:SS,可以

  1. 要么以{strong>返回的方式重写getDate函数 std::string after concatenating the class members 一起设为DD:HH:MM:SS格式。

    #include <iostream>
    #include <string>
    #include <sstream>  // std::stringstream
    
    class Date
    {
    private:
        // members
    public:
        std::string getDate() /* const noexcept */
        {
            std::stringstream sstr; // DD:HH:MM:SS
            sstr << day << ":" << hour << ":" << minute << ":" << second << '\n';
            return sstr.str();
        }
        // other codes
    };
    
    int main()
    {
        Date obj{};
        std::cout << obj.getDate(); // should print as DD:HH:MM:SS
    }
    
  2. 提供输出流 operator<<overload 这是通常的方法。

    #include <iostream>
    #include <sstream>   // std::stringstream
    
    class Date
    {
    private:
        // members
    public:
        friend std::ostream& operator<< (std::ostream& out, const Date& obj) /* noexcept */;
        // other member functions
    };
    // outside the class defenition
    std::ostream& operator<< (std::ostream& out, const Date& obj) /* noexcept */
    {
        std::stringstream sstr; // DD:HH:MM:SS
        sstr << obj.day << ":" << obj.hour << ":" << obj.minute << ":" << obj.second << '\n';
        return out << sstr.str();
    }
    
    int main()
    {
        Date obj{};
        std::cout << obj; // will console out in DD:HH:MM:SS format
    }
    

答案 1 :(得分:0)

return day, std::string(":"), hour, std::string(":"), minute, std::string(":"), second;

这是什么? 使用sprintf或其变体。

char buff[100] = {0};
sprintf_s(buff, 100, "%02u:%02u", hours, minutes);

答案 2 :(得分:0)

为什么不将所有内容都放在一个字符串中? 创建一个字符串,例如:string fullDate,然后将所有内容添加到其中,然后将其返回

答案 3 :(得分:0)

为什么不使用'tm'结构? struct tm - C++ Reference

这里有一些我总是“袖手旁观”的代码。

#include <iostream>
#include <chrono>
#include <sstream>
#include <iomanip>

using namespace std;

time_t GetDateTime_TT(int &ms)
{
  chrono::time_point<chrono::system_clock> TpNow = chrono::system_clock::now();
  chrono::system_clock::duration Durat = TpNow.time_since_epoch();
  long long Millis = chrono::duration_cast<chrono::milliseconds>(Durat).count();
  time_t Tt = chrono::system_clock::to_time_t(TpNow);
  ms = Millis % 1000;
  return Tt;
}

tm *GetDateTime_TM(int &ms)
{
  time_t Tt = GetDateTime_TT(ms);
  tm *Tm = localtime(&Tt);
  return Tm;
}

string DateTimeToString(tm *tm, const string &format)
{
  char buf[90];
  strftime(buf, 90, format.c_str(), tm);
  return string(buf);
}

tm StringToDateTime_TM(const string &value, const string &format)
{
  tm tm;
  tm.tm_year = 0;
  tm.tm_mon = 0;
  tm.tm_mday = 0;
  tm.tm_hour = 0;
  tm.tm_min = 0;
  tm.tm_sec = 0;
  tm.tm_yday = 0;
  tm.tm_wday = 0;
  tm.tm_isdst = 0;
  istringstream iss(value);
  iss >> get_time(&tm, format.c_str());
  return tm;
}

int main(int argc, char *argv[])
{
  int ms;
  tm *Now = GetDateTime_TM(ms);
  string DT = DateTimeToString(Now, "%Y-%m-%d %H:%M:%S");
  cout << "Now: " << DT.c_str() << "\n";
  tm *End = &StringToDateTime_TM("2019-12-31 23:59:59", "%Y-%m-%d %H:%M:%S");
  return 0;
}