Skip to content

Instantly share code, notes, and snippets.

@mitsu-ksgr
Created October 21, 2014 01:35
Show Gist options
  • Save mitsu-ksgr/2a9aee35282327d7e942 to your computer and use it in GitHub Desktop.
Save mitsu-ksgr/2a9aee35282327d7e942 to your computer and use it in GitHub Desktop.
【C++】時間取得のてすと
#include <iostream>
#include <ctime>
#include <thread>
#include <iomanip>
void sleep(unsigned long sleep_time)
{
std::chrono::milliseconds duration(sleep_time);
std::this_thread::sleep_for(duration);
}
//! get GMT.
inline std::time_t getGMT() {
return std::time(nullptr);
}
//! get local time.
inline std::time_t getLocalTime() {
std::time_t tt = std::time(nullptr);
struct tm *tinfo = std::localtime(&tt);
return mktime(tinfo);
}
int main()
{
std::time_t t = std::time(nullptr);
std::cout << " UTC : " << std::put_time(std::gmtime(&t), "%c %Z") << std::endl;
std::cout << " t = " << t << std::endl;
std::cout << "getGMT = " << getGMT() << std::endl;
std::cout << "local: " << std::put_time(std::localtime(&t), "%c %Z") << std::endl;
std::cout << " t = " << t << std::endl;
std::cout << "getLocalTime = " << getLocalTime() << std::endl;
std::time_t tt = std::time(nullptr);
std::cout << "------------" << std::endl;
std::cout << "std::time(nullptr) = " << tt << std::endl;
std::gmtime(&tt);
std::cout << "std::gmtime(&tt) = " << tt << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment