Skip to content

Instantly share code, notes, and snippets.

@arrieta
Created April 22, 2019 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrieta/a46578054f66ff060f2803062a4e80e7 to your computer and use it in GitHub Desktop.
Save arrieta/a46578054f66ff060f2803062a4e80e7 to your computer and use it in GitHub Desktop.
UNIX Timestamp in C++ (Guaranteed in C++20; de facto standard pre-C++20)
/// @file timestamp.hpp
/// @brief Return UNIX timestamp.
/// @author J. Arrieta <Juan.Arrieta@nablazerolabs.com>
/// @copyright (C) 2019 Nabla Zero Labs
/// @license MIT
#pragma once
#include <chrono>
#include <cstdint>
namespace nzl {
/// @brief Return total nanoseconds elapsed since Jan 01, 1970 00:00:00 UTC.
/// @note Guaranteed in C++20; de facto standard pre-C++20.
inline std::uint64_t unix_nanos() noexcept {
using namespace std::chrono;
const auto elapsed = system_clock::now().time_since_epoch();
return duration_cast<nanoseconds>(elapsed).count();
}
} // namespace nzl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment