How to convert boost local_date_time to time_t - c++

I have:
time_t dataFromTodayByAddingYearsMonthsDays(int years, int months, int days)
{
using namespace boost::local_time;
local_date_time local = local_sec_clock::local_time(time_zone_ptr());
local += boost::gregorian::years(years);
local += boost::gregorian::months(months);
local += boost::gregorian::days(days);
return ???;
}
How do I convert this boost local_date_time beast to time_t?

Here's the beef of the answer:
time_t to_time_t(boost::posix_time::ptime const& pt) //! assumes UTC
{
return (pt - boost::posix_time::from_time_t(0)).total_seconds();
}
I'd write the date calculation more succinctly, while you're at it:
int main()
{
using namespace boost::local_time;
auto now = local_sec_clock::local_time(time_zone_ptr()),
then = now + ymd_duration { 1, 3, -4 };
std::cout << now << ", " << to_time_t(now.utc_time()) << "\n";
std::cout << then << ", " << to_time_t(then.utc_time()) << "\n";
}
See it Live On Coliru, prints
2014-May-12 21:50:06 UTC, 1399931406
2015-Aug-08 21:50:06 UTC, 1439070606
Full Code:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/local_time/local_time_io.hpp>
struct ymd_duration { int years, months, day; };
template <typename T>
T operator+(T const& pt, ymd_duration delta)
{
using namespace boost::gregorian;
return pt + years(delta.years) + months(delta.months) + days(delta.day);
}
time_t to_time_t(boost::posix_time::ptime const& pt) //! assumes UTC
{
return (pt - boost::posix_time::from_time_t(0)).total_seconds();
}
int main()
{
using namespace boost::local_time;
auto now = local_sec_clock::local_time(time_zone_ptr()),
then = now + ymd_duration { 1, 3, -4 };
std::cout << now << ", " << to_time_t(now.utc_time()) << "\n";
std::cout << then << ", " << to_time_t(then.utc_time()) << "\n";
}

time_t dataFromTodayByAddingYearsMonthsDays(int years, int months, int days)
{
using namespace boost::local_time;
using namespace boost::posix_time;
local_date_time local = local_sec_clock::local_time(time_zone_ptr());
local += boost::gregorian::years(years);
local += boost::gregorian::months(months);
local += boost::gregorian::days(days);
ptime utc = local.utc_time();
ptime epoch(boost::gregorian::date(1970, 1, 1));
time_duration::sec_type diff = (utc - epoch).total_seconds();
return time_t(diff);
}

Related

Parse datetime with timezone using Boost.Date_Time?

I want to parse date time with time-zone using Boost Date Time IO library.
#include <boost/date_time.hpp>
#include <ctime>
#include <sstream>
using namespace boost::gregorian;
using namespace boost::posix_time;
std::chrono::system_clock::time_point ParseDate(const std::wstring& dateText, const wchar_t* const format) {
ptime time;
std::wstringstream buffer(dateText);
buffer.imbue(std::locale(std::locale::classic(), new wtime_input_facet(format)));
buffer >> time;
auto timeInfo = to_tm(time);
auto result = std::chrono::system_clock::from_time_t(std::mktime(&timeInfo));
return result;
}
TEST_CLASS(DateUtilsTest) {
public:
TEST_METHOD(ShouldParseUtcDate) {
auto timePoint = ParseDate(L"2016-12-03T07:09:01-05:00", L"%Y-%m-%dT%H:%M:%S%Q");
auto time = std::chrono::system_clock::to_time_t(timePoint);
auto timePoint2 = ParseDate(L"2016-12-03T07:09:01", L"%Y-%m-%dT%H:%M:%S");
auto time2 = std::chrono::system_clock::to_time_t(timePoint2);
Assert::IsTrue(time != time2);
}
}
Here's an online example: https://wandbox.org/permlink/9GEhah5l4uzhgDta
The above test failed because time == time2.
It seems that the time-zone portion doesn't have any impact to the parsing result.
Do you know how to parse a date time string with time-zone using Boost?
The real case is string like "2017-12-21T10:47:58.299Z" (ISO 8601 format, generated by JavaScript: (new Date()).toISOString()), but I didn't find any doc for this format, any ideas?
Environment:
Boost-1.65.1
System is Windows 10
System time-zone is GMT+8
Visual Studio 2015U3
I've looked at it long and hard. It seems you're almost completely out of luck:
So you can try making it work with %ZP.
Doing The Heroics
I did the heroics, only to find out that the support for wtime_zone and friends is ... incomplete in the library.
Here it is in all it g(l)ory:
Live On Coliru
#include <boost/date_time.hpp>
#include <boost/date_time/local_time/local_time_io.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/time_zone_base.hpp>
#include <ctime>
#include <chrono>
#include <sstream>
namespace DT = boost::date_time;
namespace LT = boost::local_time;
namespace PT = boost::posix_time;
template <typename CharT = wchar_t> struct TypeDefs {
using ptime = PT::ptime;
using tz_base = DT::time_zone_base<ptime, CharT>;
using tz_ptr = boost::shared_ptr<DT::time_zone_base<PT::ptime, CharT> >;
using ptz_t = LT::posix_time_zone_base<CharT>;
using ldt_t = LT::local_date_time_base<ptime, tz_base>;
};
namespace boost { namespace local_time {
//! input operator for local_date_time
template <class CharT, class Traits, typename Defs = TypeDefs<CharT>, typename local_date_time = typename Defs::ldt_t>
inline
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, local_date_time& ldt)
{
using time_zone_ptr = typename Defs::tz_ptr;
using posix_time_zone = typename Defs::ptz_t;
boost::io::ios_flags_saver iflags(is);
typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
if (strm_sentry) {
try {
typedef typename local_date_time::utc_time_type utc_time_type;
typedef typename date_time::time_input_facet<utc_time_type, CharT> time_input_facet;
// intermediate objects
std::basic_string<CharT> tz_str;
utc_time_type pt(DT::not_a_date_time);
std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
if(std::has_facet<time_input_facet>(is.getloc())) {
std::use_facet<time_input_facet>(is.getloc()).get_local_time(sit, str_end, is, pt, tz_str);
}
else {
time_input_facet* f = new time_input_facet();
std::locale l = std::locale(is.getloc(), f);
is.imbue(l);
f->get_local_time(sit, str_end, is, pt, tz_str);
}
if(tz_str.empty()) {
time_zone_ptr null_ptr;
// a null time_zone_ptr creates a local_date_time that is UTC
ldt = local_date_time(pt, null_ptr);
}
else {
time_zone_ptr tz_ptr(new posix_time_zone(tz_str));
// the "date & time" constructor expects the time label to *not* be utc.
// a posix_tz_string also expects the time label to *not* be utc.
ldt = local_date_time(pt.date(), pt.time_of_day(), tz_ptr, local_date_time::EXCEPTION_ON_ERROR);
}
}
catch(...) {
// mask tells us what exceptions are turned on
std::ios_base::iostate exception_mask = is.exceptions();
// if the user wants exceptions on failbit, we'll rethrow our
// date_time exception & set the failbit
if(std::ios_base::failbit & exception_mask) {
try { is.setstate(std::ios_base::failbit); }
catch(std::ios_base::failure&) {} // ignore this one
throw; // rethrow original exception
}
else {
// if the user want's to fail quietly, we simply set the failbit
is.setstate(std::ios_base::failbit);
}
}
}
return is;
}
} }
template <typename CharT = wchar_t> struct DateUtilsBase : TypeDefs<CharT> {
using base = TypeDefs<CharT>;
using typename base::ldt_t;
using typename base::tz_ptr;
using typename base::ptime;
static std::tm to_tm(ldt_t const& lt) {
std::tm v = PT::to_tm(lt.local_time());
v.tm_isdst = lt.is_dst()? 1:0;
return v;
}
static tz_ptr s_GMT;
static std::chrono::system_clock::time_point Parse(const std::basic_string<CharT>& dateText, const CharT* const format) {
ldt_t value(LT::special_values::not_a_date_time, s_GMT);
std::basic_istringstream<CharT> buffer(dateText);
buffer.imbue(std::locale(std::locale::classic(), new DT::time_input_facet<ptime, CharT>(format)));
std::basic_string<CharT> dummy;
if (buffer >> value && (buffer >> dummy).eof()) {
std::cout << "DEBUG: " << value.utc_time() << " EOF:" << buffer.eof() << "\n";
auto timeInfo = PT::to_tm(value.utc_time());
return std::chrono::system_clock::from_time_t(std::mktime(&timeInfo));
} else {
return std::chrono::system_clock::time_point::min();
}
}
};
template <> typename DateUtilsBase<wchar_t>::tz_ptr DateUtilsBase<wchar_t>::s_GMT { new ptz_t(L"GMT") } ;
template <> typename DateUtilsBase<char>::tz_ptr DateUtilsBase<char>::s_GMT { new ptz_t("GMT") } ;
#if 1
using DateUtils = DateUtilsBase<wchar_t>;
#define T(lit) L##lit
#else
using DateUtils = DateUtilsBase<char>;
#define T(lit) lit
#endif
int main() {
using namespace std::chrono_literals;
using C = std::chrono::system_clock;
std::cout << std::boolalpha << std::unitbuf;
C::time_point with_zone, without_zone;
// all three equivalent:
with_zone = DateUtils::Parse(T("2016-12-03T07:09:01 PST-05:00"), T("%Y-%m-%dT%H:%M:%S%ZP"));
with_zone = DateUtils::Parse(T("2016-12-03T07:09:01 -05:00"), T("%Y-%m-%dT%H:%M:%S%ZP"));
with_zone = DateUtils::Parse(T("2016-12-03T07:09:01-05:00"), T("%Y-%m-%dT%H:%M:%S%ZP"));
without_zone = DateUtils::Parse(T("2016-12-03T07:09:01"), T("%Y-%m-%dT%H:%M:%S"));
std::cout << "time_point equal? " << (with_zone == without_zone) << "\n";
{
std::time_t t_with_zone = C::to_time_t(with_zone);
std::time_t t_without_zone = C::to_time_t(without_zone);
std::cout << "time_t equal? " << (t_with_zone == t_without_zone) << "\n";
}
std::cout << (without_zone - with_zone) / 1h << " hours difference\n";
}
Yep. That's a bit of a monstrosity. It prints:
DEBUG: 2016-Dec-03 12:09:01 EOF:true
DEBUG: 2016-Dec-03 12:09:01 EOF:true
DEBUG: 2016-Dec-03 12:09:01 EOF:true
DEBUG: 2016-Dec-03 07:09:01 EOF:true
time_point equal? false
time_t equal? false
-5 hours difference
Back To Sanity
In fact, the library authors (wisely) decided that even though streams would be wide or narrow, the local_date_time (or really, just the strings in their time-zone representations) need not be. This is why the library supplied operator>> only supports local_date_time and employs the internal helper function convert_string_type to coerce to narrow-char timezone info:
time_zone_ptr tz_ptr(new posix_time_zone(date_time::convert_string_type<CharT,char>(tz_str)));
With that in mind let's remove a lot "generic-y" cruft. What remains is the addition of error-handling:
if (buffer >> value && (buffer >> dummy).eof()) {
//std::cout << "DEBUG: " << value.utc_time() << " EOF:" << buffer.eof() << "\n";
auto timeInfo = boost::posix_time::to_tm(value.utc_time());
return std::chrono::system_clock::from_time_t(std::mktime(&timeInfo));
} else {
return std::chrono::system_clock::time_point::min();
}
Live On Coliru
#include <boost/date_time.hpp>
#include <boost/date_time/local_time/local_time_io.hpp>
#include <ctime>
#include <chrono>
#include <sstream>
struct DateUtils {
using ptime = boost::posix_time::ptime;
using time_zone_ptr = boost::local_time::time_zone_ptr;
using local_date_time = boost::local_time::local_date_time;
template <typename CharT>
static std::chrono::system_clock::time_point Parse(const std::basic_string<CharT>& dateText, const CharT* const format) {
static time_zone_ptr s_GMT(new boost::local_time::posix_time_zone("GMT"));
local_date_time value(boost::local_time::special_values::not_a_date_time, s_GMT);
std::basic_istringstream<CharT> buffer(dateText);
buffer.imbue(std::locale(std::locale::classic(), new boost::date_time::time_input_facet<ptime, CharT>(format)));
std::basic_string<CharT> dummy;
if (buffer >> value && (buffer >> dummy).eof()) {
//std::cout << "DEBUG: " << value.utc_time() << " EOF:" << buffer.eof() << "\n";
auto timeInfo = boost::posix_time::to_tm(value.utc_time());
return std::chrono::system_clock::from_time_t(std::mktime(&timeInfo));
} else {
return std::chrono::system_clock::time_point::min();
}
}
};
#if 1
using CharT = wchar_t;
#define T(lit) L##lit
#else
using CharT = char;
#define T(lit) lit
#endif
int main() {
using namespace std::chrono_literals;
using C = std::chrono::system_clock;
std::cout << std::boolalpha << std::unitbuf;
C::time_point with_zone, without_zone;
// all three equivalent:
with_zone = DateUtils::Parse<CharT>(T("2016-12-03T07:09:01 PST-05:00"), T("%Y-%m-%dT%H:%M:%S%ZP"));
with_zone = DateUtils::Parse<CharT>(T("2016-12-03T07:09:01 -05:00"), T("%Y-%m-%dT%H:%M:%S%ZP"));
with_zone = DateUtils::Parse<CharT>(T("2016-12-03T07:09:01-05:00"), T("%Y-%m-%dT%H:%M:%S%ZP"));
without_zone = DateUtils::Parse<CharT>(T("2016-12-03T07:09:01"), T("%Y-%m-%dT%H:%M:%S"));
std::cout << "time_point equal? " << (with_zone == without_zone) << "\n";
{
std::time_t t_with_zone = C::to_time_t(with_zone);
std::time_t t_without_zone = C::to_time_t(without_zone);
std::cout << "time_t equal? " << (t_with_zone == t_without_zone) << "\n";
}
std::cout << (without_zone - with_zone) / 1h << " hours difference\n";
}
Whew. From 151 LoC down to 64 LoC. Better
Prints:
time_point equal? false
time_t equal? false
-5 hours difference
Summary:
read (all) the notes in the docs
use %ZP as the only supported input format
use local_date_time because that format string is ignored with ptime (it says so in the notes)
use error handling to make sure no "unparsed" things are left behind in the input

Subtracting minutes from Boost ptime

I'm trying to clamp a posix_time::ptime instance into 5 minute intervals.
For example:
1. 10:31:00 -> 10:30:00
2. 11:59:00 -> 11:55:00
My understanding is that one can subtract time from a ptime based on a period. The code below however seems to increment the time not decrement it.
#include <iostream>
#include "boost/date_time.hpp"
using namespace boost::posix_time;
boost::posix_time::ptime make(int h, int m, int s)
{
return boost::posix_time::ptime
(boost::gregorian::date(1900,1,1),
boost::posix_time::time_duration(h,m,s,0));
}
int main()
{
boost::posix_time::ptime t = make(22,2,0);
int extra_mins = t.time_of_day().minutes() % 5;
t -= boost::posix_time::minutes(extra_mins);
if (t.time_of_day().minutes() != 0)
std::cout << "ERROR";
return 0;
}
I'm not able to immediately see the error, as it seems to be correctly decrement a duration from a time point.
I don't know what you mean with
I'm not able to immediately see the error, as it seems to be correctly decrement a duration from a time point.
I'm also not able to see the error, because the behaviour is what I expect. I don't, of course, know what you're expecting. Here goes:
#include <iostream>
#include "boost/date_time.hpp"
#include "boost/date_time/posix_time/posix_time_io.hpp"
using namespace boost::posix_time;
boost::posix_time::ptime make(int h, int m, int s)
{
return boost::posix_time::ptime
(boost::gregorian::date(1900,1,1),
boost::posix_time::time_duration(h,m,s,0));
}
int main()
{
boost::posix_time::ptime t = make(22,2,0);
std::cout << "made: " << t << "\n";
int extra_mins = t.time_of_day().minutes() % 5;
std::cout << "extra_mins: " << extra_mins << "\n";
t -= boost::posix_time::minutes(extra_mins);
std::cout << "t - extra_mins: " << t << "\n";
if (t.time_of_day().minutes() != 0)
std::cout << "ERROR";
}
Output:
made: 1900-Jan-01 22:02:00
extra_mins: 2
t - extra_mins: 1900-Jan-01 22:00:00
Which looks pretty okay to me. See ti Live On Coliru too.

Generating a random date

I wrote this code snippet to generate random dates:
std::time_t curr_time = time(0);
std::time_t ten_years = 365 * 12 * 30 * 24 * 60;
std::time_t rand_date = curr_time - std::rand() % ten_years;
tm *ltm = std::localtime(&rand_date);
std::cout << ltm->tm_year + 1900 << " " << ltm->tm_mon + 1 << " " << ltm->tm_mday << std::endl;
However it always gives me the current date. What am i doing wrong?
std::rand() may return rather small values, 0..32767 is the minimum range, and does so on some popular 32-bit platforms (MSVC among them). With time_t in seconds this only gives you about eight hours of random noise.
Try combining the results from a pair of std::rand calls instead. E.g.
(std::time_t) std::rand() * RAND_MAX + std::rand() or switch to a better random number generator.
I would suggest you don't modify a time_t directly, as the implementation is not specified by the standard. Better to convert it a la this question: How to add one day to a time obtained from time()
I would suggest to do it differently, based on the CPP Reference:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
time_t timer;
struct tm x_years;
struct tm* current;
int how_many_years = 10;
srand (time(NULL));
int randomYear = (rand()%how_many_years)+1;
int randomMonth = (rand()%12)+1;
int randomDays = (rand()%30)+1;
time(&timer); /* get current time; same as: timer = time(NULL) */
current = localtime(&timer);
x_years.tm_hour = 0;
x_years.tm_min = 0;
x_years.tm_sec = 0;
x_years.tm_year = current->tm_year - randomYear;
x_years.tm_mon = (current->tm_mon - randomMonth) <= 0 ? current->tm_mon + (12-randomMonth) : current->tm_mon - randomMonth;
x_years.tm_mday = (current->tm_mday - randomDays) <= 0 ? current->tm_mday + (30-randomDays) : current->tm_mday - randomDays;
//returns seconds ever since the random generated date until now
cout << "Years rolled back: " << randomYear << endl;
cout << "Months rolled back: " << randomMonth << endl;
cout << "Days rolled back: " << randomDays << endl;
cout << endl;
cout << "Current Year: " << current->tm_year+1900 << endl;
cout << "Current Month: " << current->tm_mon << endl;
cout << "Current Day: " << current->tm_mday << endl;
cout << endl;
cout << "Year: " << x_years.tm_year+1900 << endl;
cout << "Month: " << x_years.tm_mon << endl;
cout << "Day: " << x_years.tm_mday << endl;
}
EDIT
I have edited the code, and with it, you can even select how many years you want to go back. Basically, you go back X years in the time, and you can get the date rollbacked in the x_years struct.
Hope this helped!
The following solution uses C++11 with an internal uniform_int_distribution:
// uniform_time_dist.h
#include <chrono>
#include <random>
template <class TimePoint>
class uniform_time_distribution{
public:
uniform_time_distribution(TimePoint start, TimePoint end)
: m_start(start), m_end(end),
m_seconds(std::chrono::duration_cast<std::chrono::seconds>(end - start))
{}
template <class Generator>
TimePoint operator()(Generator && g){
std::uniform_int_distribution<std::chrono::seconds::rep> d(0, m_seconds.count());
return m_start + std::chrono::seconds(d(g));
}
private:
TimePoint m_start;
TimePoint m_end;
std::chrono::seconds m_seconds;
};
You can use it like any other distribution function with a generator:
// uniform_time_dist.h -- continuation
template <class TimePoint>
TimePoint randomTime(TimePoint start, TimePoint end){
static std::random_device rd;
static std::mt19937 gen(rd());
uniform_time_distribution<TimePoint> t(start, end);
return t(gen);
}
You can combine this method with your time_t functions by using clock::to_time_t:
#include <iostream>
#include "uniform_time_dist.h" // see above
using namespace std::chrono;
int main(){
auto k = system_clock::to_time_t(randomTime(
system_clock::now(),
system_clock::now() + hours(24 * 365 * 10)));
std::cout << std::ctime(&k);
}
Note that the class above is just a small sketch, you should be able to improve it vastly in order to match the other distribution functions.
I managed to create a solution based on Phil's answer:
time_t currTime = time(0);
tm *ltm = std::localtime(&currTime);
ltm->tm_mday = std::rand() % 3650 * -1;
time_t next = mktime(ltm);
ltm = std::localtime(&next);
std::cout << ltm->tm_year + 1900 << " " << ltm->tm_mon + 1 << " " << ltm->tm_mday << std::endl;
If you are using boost libs you can use this class that I wrote to get random dates:
#include <iostream>
#include <ctime>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
using namespace std;
using namespace boost;
using namespace boost::posix_time;
using namespace boost::gregorian;
class Randomizer {
private:
static const bool debug_mode = false;
random::mt19937 rng_;
// The private constructor so that the user can not directly instantiate
Randomizer() {
if(debug_mode==true){
this->rng_ = random::mt19937();
}else{
this->rng_ = random::mt19937(current_time_nanoseconds());
}
};
int current_time_nanoseconds(){
struct timespec tm;
clock_gettime(CLOCK_REALTIME, &tm);
return tm.tv_nsec;
}
// C++ 03
// ========
// Dont forget to declare these two. You want to make sure they
// are unacceptable otherwise you may accidentally get copies of
// your singleton appearing.
Randomizer(Randomizer const&); // Don't Implement
void operator=(Randomizer const&); // Don't implement
public:
static Randomizer& get_instance(){
// The only instance of the class is created at the first call get_instance ()
// and will be destroyed only when the program exits
static Randomizer instance;
return instance;
}
bool method() { return true; };
int rand(unsigned int floor, unsigned int ceil){
random::uniform_int_distribution<> rand_ = random::uniform_int_distribution<> (floor,ceil);
return (rand_(rng_));
}
// Is not considering the millisecons
time_duration rand_time_duration(){
boost::posix_time::time_duration floor(0, 0, 0, 0);
boost::posix_time::time_duration ceil(23, 59, 59, 0);
unsigned int rand_seconds = rand(floor.total_seconds(), ceil.total_seconds());
return seconds(rand_seconds);
}
date rand_date_from_epoch_to_now(){
date now = second_clock::local_time().date();
return rand_date_from_epoch_to_ceil(now);
}
date rand_date_from_epoch_to_ceil(date ceil_date){
date epoch = ptime(date(1970,1,1)).date();
return rand_date_in_interval(epoch, ceil_date);
}
date rand_date_in_interval(date floor_date, date ceil_date){
return rand_ptime_in_interval(ptime(floor_date), ptime(ceil_date)).date();
}
ptime rand_ptime_from_epoch_to_now(){
ptime now = second_clock::local_time();
return rand_ptime_from_epoch_to_ceil(now);
}
ptime rand_ptime_from_epoch_to_ceil(ptime ceil_date){
ptime epoch = ptime(date(1970,1,1));
return rand_ptime_in_interval(epoch, ceil_date);
}
ptime rand_ptime_in_interval(ptime floor_date, ptime ceil_date){
time_duration const diff = ceil_date - floor_date;
long long gap_seconds = diff.total_seconds();
long long step_seconds = Randomizer::get_instance().rand(0, gap_seconds);
return floor_date + seconds(step_seconds);
}
};

How to convert std::chrono::time_point to calendar datetime string with fractional seconds?

How to convert std::chrono::time_point to calendar datetime string with fractional seconds?
For example:
"10-10-2012 12:38:40.123456"
If system_clock, this class have time_t conversion.
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std::chrono;
int main()
{
system_clock::time_point p = system_clock::now();
std::time_t t = system_clock::to_time_t(p);
std::cout << std::ctime(&t) << std::endl; // for example : Tue Sep 27 14:21:13 2011
}
example result:
Thu Oct 11 19:10:24 2012
EDIT:
But, time_t does not contain fractional seconds.
Alternative way is to use time_point::time_since_epoch() function. This function returns duration from epoch.
Follow example is milli second resolution's fractional.
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std::chrono;
int main()
{
high_resolution_clock::time_point p = high_resolution_clock::now();
milliseconds ms = duration_cast<milliseconds>(p.time_since_epoch());
seconds s = duration_cast<seconds>(ms);
std::time_t t = s.count();
std::size_t fractional_seconds = ms.count() % 1000;
std::cout << std::ctime(&t) << std::endl;
std::cout << fractional_seconds << std::endl;
}
example result:
Thu Oct 11 19:10:24 2012
925
Self-explanatory code follows which first creates a std::tm corresponding to 10-10-2012 12:38:40, converts that to a std::chrono::system_clock::time_point, adds 0.123456 seconds, and then prints that out by converting back to a std::tm. How to handle the fractional seconds is in the very last step.
#include <iostream>
#include <chrono>
#include <ctime>
int main()
{
// Create 10-10-2012 12:38:40 UTC as a std::tm
std::tm tm = {0};
tm.tm_sec = 40;
tm.tm_min = 38;
tm.tm_hour = 12;
tm.tm_mday = 10;
tm.tm_mon = 9;
tm.tm_year = 112;
tm.tm_isdst = -1;
// Convert std::tm to std::time_t (popular extension)
std::time_t tt = timegm(&tm);
// Convert std::time_t to std::chrono::system_clock::time_point
std::chrono::system_clock::time_point tp =
std::chrono::system_clock::from_time_t(tt);
// Add 0.123456 seconds
// This will not compile if std::chrono::system_clock::time_point has
// courser resolution than microseconds
tp += std::chrono::microseconds(123456);
// Now output tp
// Convert std::chrono::system_clock::time_point to std::time_t
tt = std::chrono::system_clock::to_time_t(tp);
// Convert std::time_t to std::tm (popular extension)
tm = std::tm{0};
gmtime_r(&tt, &tm);
// Output month
std::cout << tm.tm_mon + 1 << '-';
// Output day
std::cout << tm.tm_mday << '-';
// Output year
std::cout << tm.tm_year+1900 << ' ';
// Output hour
if (tm.tm_hour <= 9)
std::cout << '0';
std::cout << tm.tm_hour << ':';
// Output minute
if (tm.tm_min <= 9)
std::cout << '0';
std::cout << tm.tm_min << ':';
// Output seconds with fraction
// This is the heart of the question/answer.
// First create a double-based second
std::chrono::duration<double> sec = tp -
std::chrono::system_clock::from_time_t(tt) +
std::chrono::seconds(tm.tm_sec);
// Then print out that double using whatever format you prefer.
if (sec.count() < 10)
std::cout << '0';
std::cout << std::fixed << sec.count() << '\n';
}
For me this outputs:
10-10-2012 12:38:40.123456
Your std::chrono::system_clock::time_point may or may not be precise enough to hold microseconds.
Update
An easier way is to just use this date library. The code simplifies down to (using C++14 duration literals):
#include "date.h"
#include <iostream>
#include <type_traits>
int
main()
{
using namespace date;
using namespace std::chrono;
auto t = sys_days{10_d/10/2012} + 12h + 38min + 40s + 123456us;
static_assert(std::is_same<decltype(t),
time_point<system_clock, microseconds>>{}, "");
std::cout << t << '\n';
}
which outputs:
2012-10-10 12:38:40.123456
You can skip the static_assert if you don't need to prove that the type of t is a std::chrono::time_point.
If the output isn't to your liking, for example you would really like dd-mm-yyyy ordering, you could:
#include "date.h"
#include <iomanip>
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
using namespace std;
auto t = sys_days{10_d/10/2012} + 12h + 38min + 40s + 123456us;
auto dp = floor<days>(t);
auto time = make_time(t-dp);
auto ymd = year_month_day{dp};
cout.fill('0');
cout << ymd.day() << '-' << setw(2) << static_cast<unsigned>(ymd.month())
<< '-' << ymd.year() << ' ' << time << '\n';
}
which gives exactly the requested output:
10-10-2012 12:38:40.123456
Update
Here is how to neatly format the current time UTC with milliseconds precision:
#include "date.h"
#include <iostream>
int
main()
{
using namespace std::chrono;
std::cout << date::format("%F %T\n", time_point_cast<milliseconds>(system_clock::now()));
}
which just output for me:
2016-10-17 16:36:02.975
C++17 will allow you to replace time_point_cast<milliseconds> with floor<milliseconds>. Until then date::floor is available in "date.h".
std::cout << date::format("%F %T\n", date::floor<milliseconds>(system_clock::now()));
Update C++20
In C++20 this is now simply:
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
auto t = sys_days{10d/10/2012} + 12h + 38min + 40s + 123456us;
std::cout << t << '\n';
}
Or just:
std::cout << std::chrono::system_clock::now() << '\n';
std::format will be available to customize the output.
In general, you can't do this in any straightforward fashion. time_point is essentially just a duration from a clock-specific epoch.
If you have a std::chrono::system_clock::time_point, then you can use std::chrono::system_clock::to_time_t to convert the time_point to a time_t, and then use the normal C functions such as ctime or strftime to format it.
Example code:
std::chrono::system_clock::time_point tp = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(tp);
std::tm timetm = *std::localtime(&time);
std::cout << "output : " << std::put_time(&timetm, "%c %Z") << "+"
<< std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()).count() % 1000 << std::endl;
This worked for me for a format like YYYY.MM.DD-HH.MM.SS.fff. Attempting to make this code capable of accepting any string format will be like reinventing the wheel (i.e. there are functions for all this in Boost.
std::chrono::system_clock::time_point string_to_time_point(const std::string &str)
{
using namespace std;
using namespace std::chrono;
int yyyy, mm, dd, HH, MM, SS, fff;
char scanf_format[] = "%4d.%2d.%2d-%2d.%2d.%2d.%3d";
sscanf(str.c_str(), scanf_format, &yyyy, &mm, &dd, &HH, &MM, &SS, &fff);
tm ttm = tm();
ttm.tm_year = yyyy - 1900; // Year since 1900
ttm.tm_mon = mm - 1; // Month since January
ttm.tm_mday = dd; // Day of the month [1-31]
ttm.tm_hour = HH; // Hour of the day [00-23]
ttm.tm_min = MM;
ttm.tm_sec = SS;
time_t ttime_t = mktime(&ttm);
system_clock::time_point time_point_result = std::chrono::system_clock::from_time_t(ttime_t);
time_point_result += std::chrono::milliseconds(fff);
return time_point_result;
}
std::string time_point_to_string(std::chrono::system_clock::time_point &tp)
{
using namespace std;
using namespace std::chrono;
auto ttime_t = system_clock::to_time_t(tp);
auto tp_sec = system_clock::from_time_t(ttime_t);
milliseconds ms = duration_cast<milliseconds>(tp - tp_sec);
std::tm * ttm = localtime(&ttime_t);
char date_time_format[] = "%Y.%m.%d-%H.%M.%S";
char time_str[] = "yyyy.mm.dd.HH-MM.SS.fff";
strftime(time_str, strlen(time_str), date_time_format, ttm);
string result(time_str);
result.append(".");
result.append(to_string(ms.count()));
return result;
}
I would have put this in a comment on the accepted answer, since that's where it belongs, but I can't. So, just in case anyone gets unreliable results, this could be why.
Be careful of the accepted answer, it fails if the time_point is before the epoch.
This line of code:
std::size_t fractional_seconds = ms.count() % 1000;
will yield unexpected values if ms.count() is negative (since size_t is not meant to hold negative values).
In my case I use chrono and c function localtime_r which is thread-safe (in opposition to std::localtime).
#include <iostream>
#include <chrono>
#include <ctime>
#include <time.h>
#include <iomanip>
int main() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
std::chrono::milliseconds now2 = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
struct tm currentLocalTime;
localtime_r(&currentTime, &currentLocalTime);
char timeBuffer[80];
std::size_t charCount { std::strftime( timeBuffer, 80,
"%b %d %T",
&currentLocalTime)
};
if (charCount == 0) return -1;
std::cout << timeBuffer << "." << std::setfill('0') << std::setw(3) << now2.count() % 1000 << std::endl;
return 0;
}
If you are to format a system_clock::time_point in the format of numpy datetime64, you could use:
std::string format_time_point(system_clock::time_point point)
{
static_assert(system_clock::time_point::period::den == 1000000000 && system_clock::time_point::period::num == 1);
std::string out(29, '0');
char* buf = &out[0];
std::time_t now_c = system_clock::to_time_t(point);
std::strftime(buf, 21, "%Y-%m-%dT%H:%M:%S.", std::localtime(&now_c));
sprintf(buf+20, "%09ld", point.time_since_epoch().count() % 1000000000);
return out;
}
sample output: 2019-11-19T17:59:58.425802666

How to get current time and date in C++?

Is there a cross-platform way to get the current date and time in C++?
Since C++ 11 you can use std::chrono::system_clock::now()
Example (copied from en.cppreference.com):
#include <iostream>
#include <chrono>
#include <ctime>
int main()
{
auto start = std::chrono::system_clock::now();
// Some computation here
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s"
<< std::endl;
}
This should print something like this:
finished computation at Mon Oct 2 00:59:08 2017
elapsed time: 1.88232s
C++ shares its date/time functions with C. The tm structure is probably the easiest for a C++ programmer to work with - the following prints today's date:
#include <ctime>
#include <iostream>
int main() {
std::time_t t = std::time(0); // get time now
std::tm* now = std::localtime(&t);
std::cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< "\n";
}
You can try the following cross-platform code to get current date/time:
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
int main() {
std::cout << "currentDateTime()=" << currentDateTime() << std::endl;
getchar(); // wait for keyboard input
}
Output:
currentDateTime()=2012-05-06.21:47:59
Please visit here for more information about date/time format
std C libraries provide time().
This is seconds from the epoch and can be converted to date and H:M:S using standard C functions. Boost also has a time/date library that you can check.
time_t timev;
time(&timev);
New answer for an old question:
The question does not specify in what timezone. There are two reasonable possibilities:
In UTC.
In the computer's local timezone.
For 1, you can use this date library and the following program:
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << system_clock::now() << '\n';
}
Which just output for me:
2015-08-18 22:08:18.944211
The date library essentially just adds a streaming operator for std::chrono::system_clock::time_point. It also adds a lot of other nice functionality, but that is not used in this simple program.
If you prefer 2 (the local time), there is a timezone library that builds on top of the date library. Both of these libraries are open source and cross platform, assuming the compiler supports C++11 or C++14.
#include "tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
auto local = make_zoned(current_zone(), system_clock::now());
std::cout << local << '\n';
}
Which for me just output:
2015-08-18 18:08:18.944211 EDT
The result type from make_zoned is a date::zoned_time which is a pairing of a date::time_zone and a std::chrono::system_clock::time_point. This pair represents a local time, but can also represent UTC, depending on how you query it.
With the above output, you can see that my computer is currently in a timezone with a UTC offset of -4h, and an abbreviation of EDT.
If some other timezone is desired, that can also be accomplished. For example to find the current time in Sydney , Australia just change the construction of the variable local to:
auto local = make_zoned("Australia/Sydney", system_clock::now());
And the output changes to:
2015-08-19 08:08:18.944211 AEST
Update for C++20
This library is now largely adopted for C++20. The namespace date is gone and everything is in namespace std::chrono now. And use zoned_time in place of make_time. Drop the headers "date.h" and "tz.h" and just use <chrono>.
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
auto local = zoned_time{current_zone(), system_clock::now()};
std::cout << local << '\n'; // 2021-05-03 15:02:44.130182 EDT
}
As I write this, partial implementations are just beginning to emerge on some platforms.
the C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date and time manipulation from C, along with a couple of date/time input and output functions that take into account localization.
// Current date/time based on current system
time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;
// Convert now to tm struct for UTC
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}
auto time = std::time(nullptr);
std::cout << std::put_time(std::localtime(&time), "%F %T%z"); // ISO 8601 format.
Get the current time either using std::time() or std::chrono::system_clock::now() (or another clock type).
std::put_time() (C++11) and strftime() (C) offer a lot of formatters to output those times.
#include <iomanip>
#include <iostream>
int main() {
auto time = std::time(nullptr);
std::cout
// ISO 8601: %Y-%m-%d %H:%M:%S, e.g. 2017-07-31 00:42:00+0200.
<< std::put_time(std::gmtime(&time), "%F %T%z") << '\n'
// %m/%d/%y, e.g. 07/31/17
<< std::put_time(std::gmtime(&time), "%D");
}
The sequence of the formatters matters:
std::cout << std::put_time(std::gmtime(&time), "%c %A %Z") << std::endl;
// Mon Jul 31 00:00:42 2017 Monday GMT
std::cout << std::put_time(std::gmtime(&time), "%Z %c %A") << std::endl;
// GMT Mon Jul 31 00:00:42 2017 Monday
The formatters of strftime() are similar:
char output[100];
if (std::strftime(output, sizeof(output), "%F", std::gmtime(&time))) {
std::cout << output << '\n'; // %Y-%m-%d, e.g. 2017-07-31
}
Often, the capital formatter means "full version" and lowercase means abbreviation (e.g. Y: 2017, y: 17).
Locale settings alter the output:
#include <iomanip>
#include <iostream>
int main() {
auto time = std::time(nullptr);
std::cout << "undef: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "en_US: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("en_GB.utf8"));
std::cout << "en_GB: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("de_DE.utf8"));
std::cout << "de_DE: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_time(std::gmtime(&time), "%c");
}
Possible output (Coliru, Compiler Explorer):
undef: Tue Aug 1 08:29:30 2017
en_US: Tue 01 Aug 2017 08:29:30 AM GMT
en_GB: Tue 01 Aug 2017 08:29:30 GMT
de_DE: Di 01 Aug 2017 08:29:30 GMT
ja_JP: 2017年08月01日 08時29分30秒
ru_RU: Вт 01 авг 2017 08:29:30
I've used std::gmtime() for conversion to UTC. std::localtime() is provided to convert to local time.
Heed that asctime()/ctime() which were mentioned in other answers are marked as deprecated now and strftime() should be preferred.
(For fellow googlers)
There is also Boost::date_time :
#include <boost/date_time/posix_time/posix_time.hpp>
boost::posix_time::ptime date_time = boost::posix_time::microsec_clock::universal_time();
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
Yes and you can do so with formatting rules specified by the currently-imbued locale:
#include <iostream>
#include <iterator>
#include <string>
class timefmt
{
public:
timefmt(std::string fmt)
: format(fmt) { }
friend std::ostream& operator <<(std::ostream &, timefmt const &);
private:
std::string format;
};
std::ostream& operator <<(std::ostream& os, timefmt const& mt)
{
std::ostream::sentry s(os);
if (s)
{
std::time_t t = std::time(0);
std::tm const* tm = std::localtime(&t);
std::ostreambuf_iterator<char> out(os);
std::use_facet<std::time_put<char>>(os.getloc())
.put(out, os, os.fill(),
tm, &mt.format[0], &mt.format[0] + mt.format.size());
}
os.width(0);
return os;
}
int main()
{
std::cout << timefmt("%c");
}
Output: Fri Sep 6 20:33:31 2013
you could use C++ 11 time class:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
time_t now = chrono::system_clock::to_time_t(chrono::system_clock::now());
cout << put_time(localtime(&now), "%F %T") << endl;
return 0;
}
out put:
2017-08-25 12:30:08
There's always the __TIMESTAMP__ preprocessor macro.
#include <iostream>
using namespace std
void printBuildDateTime () {
cout << __TIMESTAMP__ << endl;
}
int main() {
printBuildDateTime();
}
example: Sun Apr 13 11:28:08 2014
std::ctime
Why was ctime only mentioned in the comments so far?
#include <ctime>
#include <iostream>
int main()
{
std::time_t result = std::time(nullptr);
std::cout << std::ctime(&result);
}
Output
Tue Dec 27 17:21:29 2011
You can use the following code to get the current system date and time in C++ :
#include <iostream>
#include <time.h> //It may be #include <ctime> or any other header file depending upon
// compiler or IDE you're using
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
// convert now to string form
string dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
return 0;
}
PS: Visit this site for more information.
You can also directly use ctime():
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
printf ( "Current local time and date: %s", ctime (&rawtime) );
return 0;
}
I found this link pretty useful for my implementation:
C++ Date and Time
Here's the code I use in my implementation, to get a clear "YYYYMMDD HHMMSS" output format. The param in is for switching between UTC and local time. You can easily modify my code to suite your need.
#include <iostream>
#include <ctime>
using namespace std;
/**
* This function gets the current date time
* #param useLocalTime true if want to use local time, default to false (UTC)
* #return current datetime in the format of "YYYYMMDD HHMMSS"
*/
string getCurrentDateTime(bool useLocalTime) {
stringstream currentDateTime;
// current date/time based on current system
time_t ttNow = time(0);
tm * ptmNow;
if (useLocalTime)
ptmNow = localtime(&ttNow);
else
ptmNow = gmtime(&ttNow);
currentDateTime << 1900 + ptmNow->tm_year;
//month
if (ptmNow->tm_mon < 9)
//Fill in the leading 0 if less than 10
currentDateTime << "0" << 1 + ptmNow->tm_mon;
else
currentDateTime << (1 + ptmNow->tm_mon);
//day
if (ptmNow->tm_mday < 10)
currentDateTime << "0" << ptmNow->tm_mday << " ";
else
currentDateTime << ptmNow->tm_mday << " ";
//hour
if (ptmNow->tm_hour < 10)
currentDateTime << "0" << ptmNow->tm_hour;
else
currentDateTime << ptmNow->tm_hour;
//min
if (ptmNow->tm_min < 10)
currentDateTime << "0" << ptmNow->tm_min;
else
currentDateTime << ptmNow->tm_min;
//sec
if (ptmNow->tm_sec < 10)
currentDateTime << "0" << ptmNow->tm_sec;
else
currentDateTime << ptmNow->tm_sec;
return currentDateTime.str();
}
Output (UTC, EST):
20161123 000454
20161122 190454
This works with G++ I'm not sure if this helps you.
Program output:
The current time is 11:43:41 am
The current date is 6-18-2015 June Wednesday
Day of month is 17 and the Month of year is 6,
also the day of year is 167 & our Weekday is 3.
The current year is 2015.
Code :
#include <ctime>
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
using namespace std;
const std::string currentTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%H:%M:%S %P", &tstruct);
return buf;
}
const std::string currentDate() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%B %A ", &tstruct);
return buf;
}
int main() {
cout << "\033[2J\033[1;1H";
std:cout << "The current time is " << currentTime() << std::endl;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << "The current date is " << now->tm_mon + 1 << '-'
<< (now->tm_mday + 1) << '-'
<< (now->tm_year + 1900)
<< " " << currentDate() << endl;
cout << "Day of month is " << (now->tm_mday)
<< " and the Month of year is " << (now->tm_mon)+1 << "," << endl;
cout << "also the day of year is " << (now->tm_yday)
<< " & our Weekday is " << (now->tm_wday) << "." << endl;
cout << "The current year is " << (now->tm_year)+1900 << "."
<< endl;
return 0;
}
This compiled for me on Linux (RHEL) and Windows (x64) targeting g++ and OpenMP:
#include <ctime>
#include <iostream>
#include <string>
#include <locale>
////////////////////////////////////////////////////////////////////////////////
//
// Reports a time-stamped update to the console; format is:
// Name: Update: Year-Month-Day_of_Month Hour:Minute:Second
//
////////////////////////////////////////////////////////////////////////////////
//
// [string] strName : name of the update object
// [string] strUpdate: update descripton
//
////////////////////////////////////////////////////////////////////////////////
void ReportTimeStamp(string strName, string strUpdate)
{
try
{
#ifdef _WIN64
// Current time
const time_t tStart = time(0);
// Current time structure
struct tm tmStart;
localtime_s(&tmStart, &tStart);
// Report
cout << strName << ": " << strUpdate << ": " << (1900 + tmStart.tm_year) << "-" << tmStart.tm_mon << "-" << tmStart.tm_mday << " " << tmStart.tm_hour << ":" << tmStart.tm_min << ":" << tmStart.tm_sec << "\n\n";
#else
// Current time
const time_t tStart = time(0);
// Current time structure
struct tm* tmStart;
tmStart = localtime(&tStart);
// Report
cout << strName << ": " << strUpdate << ": " << (1900 + tmStart->tm_year) << "-" << tmStart->tm_mon << "-" << tmStart->tm_mday << " " << tmStart->tm_hour << ":" << tmStart->tm_min << ":" << tmStart->tm_sec << "\n\n";
#endif
}
catch (exception ex)
{
cout << "ERROR [ReportTimeStamp] Exception Code: " << ex.what() << "\n";
}
return;
}
The ffead-cpp provides multiple utility classes for various tasks, one such class is the Date class which provides a lot of features right from Date operations to date arithmetic, there's also a Timer class provided for timing operations. You can have a look at the same.
http://www.cplusplus.com/reference/ctime/strftime/
This built-in seems to offer a reasonable set of options.
localtime_s() version:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t current_time;
struct tm local_time;
time ( &current_time );
localtime_s(&local_time, &current_time);
int Year = local_time.tm_year + 1900;
int Month = local_time.tm_mon + 1;
int Day = local_time.tm_mday;
int Hour = local_time.tm_hour;
int Min = local_time.tm_min;
int Sec = local_time.tm_sec;
return 0;
}
#include <iostream>
#include <chrono>
#include <string>
#pragma warning(disable: 4996)
// Ver: C++ 17
// IDE: Visual Studio
int main() {
using namespace std;
using namespace chrono;
time_point tp = system_clock::now();
time_t tt = system_clock::to_time_t(tp);
cout << "Current time: " << ctime(&tt) << endl;
return 0;
}
Here is the non-deprecated modern C++ solution for getting a timestamp as a std::string for use with e.g. filenames:
std::string get_file_timestamp()
{
const auto now = std::chrono::system_clock::now();
const auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream output_stream;
struct tm time_info;
const auto errno_value = localtime_s(&time_info, &in_time_t);
if(errno_value != 0)
{
throw std::runtime_error("localtime_s() failed: " + std::to_string(errno_value));
}
output_stream << std::put_time(&time_info, "%Y-%m-%d.%H_%M_%S");
return output_stream.str();
}
You could use boost and chrono library:
#include <iostream>
#include <chrono>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::posix_time::to_iso_extended_string;
using boost::posix_time::from_time_t;
using std::chrono::system_clock;
int main()
{
auto now = system_clock::now();
std::cout << to_iso_extended_string(from_time_t(system_clock::to_time_t(now)));
}
#include <Windows.h>
void main()
{
//Following is a structure to store date / time
SYSTEMTIME SystemTime, LocalTime;
//To get the local time
int loctime = GetLocalTime(&LocalTime);
//To get the system time
int systime = GetSystemTime(&SystemTime)
}
I needed a way to insert current date-time at every update of a list.
This seems to work well, simply.
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
int main()
{ //initialize variables
time_t now;
//blah..blah
/*each time I want the updated stamp*/
now=time(0);cout<<ctime(&now)<<"blah_blah";
}