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
Rakinert
  • 31
  • 3
  • 1
    Have you tried stepping through the code with a debugger in order to see why the seemingly incorrect time is printed? – Quimby Jun 08 '23 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. – interjay Jun 08 '23 at 18:07
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) - also [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) - *especially* in a header - don't *ever* use `using namespace ...` at global scope in a header, you'll polute everything that includes it. – Jesper Juhl Jun 08 '23 at 18:27
  • What compiler are you using? – Howard Hinnant Jun 08 '23 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](https://stackoverflow.com/a/43018120/576911) 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... – Howard Hinnant Jun 09 '23 at 01:03

0 Answers0