Как получить секунды в удобочитаемые даты - PullRequest
0 голосов
/ 09 сентября 2018

Я пытаюсь превратить произвольные целочисленные значения, представляющие секунды с 1 января 1970 года, в удобочитаемые даты.

Это так близко, как я получил, но я продолжаю получать текущую дату. Как я могу получить struct tm даты, которая не является текущей датой?

#include <iostream> 
#include <string>
#include <time.h>

using namespace std;

int main() {
  struct tm * timeStruct;
  time_t myTime = 946684800; //s from 1970 to 2000
  int timeStamp = time(&myTime); //I thought this would set the date to the values of myTime, it just sets it to now
  timeStruct = localtime(&myTime);
  cout << timeStamp;
  cout << "\n";
  cout << asctime(timeStruct); //This should read Jan 1, 2000, instead it keeps giving me the current time
  cout << "\n";
  system("pause");
  return 0;
}

1 Ответ

0 голосов
/ 09 сентября 2018

time(&myTime) устанавливал значение myTime на текущее время (это очевидно , как и следовало ожидать).

Решение:

#include <iostream> 
#include <string>
#include <time.h>

using namespace std;

int main() {
  struct tm * timeStruct;
  time_t myTime = 946684800; //s from 1970 to 2000

  int timeStamp = myTime;
  timeStruct = localtime(&myTime);

  cout << timeStamp;

  cout << "\n";

  cout << asctime(timeStruct);

  cout << "\n";

  system("pause");

  return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...