3

I have a simple function to print the current UTC time every second. Here is the code:

#include <iostream>
#include <thread>
#include <time.h>
#include <chrono>

using namespace std;

void printTime(){
    time_t now = time(0);
    tm *gmtm = gmtime(&now);
    char* dt = asctime(gmtm);
    cout << dt << endl;
}

int main(void) {
    while(true){
        printTime();
        this_thread::sleep_for(chrono::milliseconds(1000));
    }

    return(0);
}

My code prints a different time than the actual UTC time (compared to both my local machine and a website).

For example ...

My code prints Sun May 13 00:55:30 2018.

My local machine prints Sun May 13 00:55:22 2018 (I print it from a terminal with date -u).

Some website prints Sun May 13 00:55:22 2018 (same as my local machine).

What did I do wrong? Please help :(

[EDIT] I use Docker.

Hana Alaydrus
  • 2,225
  • 16
  • 19

0 Answers0