2

I have a C++ program about set time, and the time output is unexpected.

When I try to print the time of an event, it consistently displays the time as 06:36 instead of the expected 00:00. I'm using the printEvent() function to print the event details, and the setDTStart() and easySetDTStart() functions to set the start time.

But when I set the parameter byTaiwan = true. The time correctly add 8 hours in the output.

I'm unsure why this discrepancy is happening and would appreciate any insights or suggestions on how to resolve this issue. Thanks.

//event.h
#ifndef UNTITLED1_EVENT_H
#define UNTITLED1_EVENT_H
#include <string>
#include <vector>
#include <chrono>

using namespace std;
class Event {
private:
    chrono::system_clock::time_point dateTimeStart;
    chrono::system_clock::time_point dateTimeEnd;
public:

    void printEvent();

    void setDTStart(chrono::system_clock::time_point timePoint, bool byTaiwan);

    void easySetDTStart(int year, int month, int day, int hour, int minute, int second, bool byTaiwan);
};
#endif //UNTITLED1_EVENT_H
//event.cpp
#include <chrono>
#include "event.h"
#include "iostream"
#include "iomanip"

using namespace std;

void Event::printEvent() {
    time_t time = chrono::system_clock::to_time_t(dateTimeStart);
    cout << "DTSTART:" << put_time(gmtime(&time),"%Y %m %d T %H %M %S") << endl;
}

void Event::setDTStart(chrono::system_clock::time_point timePoint, bool byTaiwan = true) {
    if(byTaiwan)
        timePoint += chrono::hours(8);
    dateTimeStart = timePoint;
}

void Event::easySetDTStart(int year, int month, int day, int hour, int minute, int second, bool byTaiwan = true) {
    chrono::years y{year - 1970};
    chrono::months m{month - 1};
    chrono::days d{day - 1};
    chrono::hours h{hour};
    chrono::minutes min{minute};
    chrono::seconds s{second};
    setDTStart(chrono::system_clock::time_point(y+m+d+h+min+s),byTaiwan);
}
//main.cpp
#include "event.h"

int main() {
    Event event;
    event.easySetDTStart(2000,1,1,0,0,0, false);
    event.printEvent();
    event.easySetDTStart(2000,1,1,0,0,0, true);
    event.printEvent();
}


//output
DTSTART:2000 01 01 T 06 36 00
DTSTART:2000 01 01 T 14 36 00
4
  • 1
    Have you tried stepping through the code with a debugger in order to see why the seemingly incorrect time is printed? Commented Jun 8, 2023 at 17:58
  • 4
    chrono::years and chrono::months are just the average duration. They're mostly useless because the actual duration varies based on month length and leap years. Commented Jun 8, 2023 at 18:07
  • What compiler are you using? Commented Jun 8, 2023 at 20:58
  • @interjay You are correct that chrono::years and chrono::months are just average durations. But they are quite useful as they can be used in either chronological computations or calendrical computations. See this post for an example of how to add months to a time point using both chronological and calendrical arithmetic, and a brief discussion on how both have valid use cases. Note too that with the calendrical arithmetic, one can choose to add the influence of time zones or not. Lots of choices... Commented Jun 9, 2023 at 1:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.