Skip to content

Instantly share code, notes, and snippets.

@vampy
Created April 7, 2015 20:27
Show Gist options
  • Save vampy/b1bcca0fe7267bf59658 to your computer and use it in GitHub Desktop.
Save vampy/b1bcca0fe7267bf59658 to your computer and use it in GitHub Desktop.
#include <ctime>
#include <string>
class Time
{
public:
static std::string getTime() // in HH:MM::SS
{
std::time_t t = std::time(nullptr);
std::tm *tm = std::localtime(&t);
const int size = 16;
char buffer[size];
if (std::strftime(buffer, size, "%H:%M:%S",tm) == 0)
{
throw std::runtime_error("Error trying to getTime");
}
return std::string(buffer);
}
static std::string getDate() // in day:month:year
{
std::time_t t = std::time(nullptr);
std::tm *tm = std::localtime(&t);
const int size = 16;
char buffer[size];
if (std::strftime(buffer, size, "%d:%m:%Y",tm) == 0)
{
throw std::runtime_error("Error trying to getDate");
}
return std::string(buffer);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment