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