C++ timedelta with strftime? - c++

I want to do a function to get the current time with a certain format. C++ is not my main language but im trying to do this:
current_datetime(timezone='-03:00', offset=timedelta(seconds=120))
def current_datetime(fmt='%Y-%m-%dT%H:%M:%S', timezone='Z', offset=None):
offset = offset or timedelta(0)
return (datetime.today() + offset).strftime(fmt) + timezone
My best so far searching internet was this, but is missing the offset part:
#include <iostream>
#include <ctime>
std::string current_datetime(std::string timezone="Z", int offset=1)
{
std::time_t t = std::time(nullptr);
char mbstr[50];
std::strftime(mbstr, sizeof(mbstr), "%Y-%m-%dT%H:%M:%S", std::localtime(&t));
std::string formated_date(mbstr);
formated_date += std::string(timezone);
return formated_date;
}
int main()
{
std::cout << current_datetime() << std::endl; //2021-10-26T21:34:48Z
std::cout << current_datetime("-05:00") << std::endl; //2021-10-26T21:34:48-05:00
return 0;
}
The idea is to get a string that is a "start date" and one as "end date" that is X seconds in the future. Im stuck with the offset/delta part

Just add the offset to the seconds since epoch.
std::time_t t = std::time(nullptr) + offset;
You would also make offset of type std::time_t, as it represents a distance in time in seconds.

Related

Getting Current Date inside a C++ process running 24*7

I have a backend process running 24*7 mostly built using C++ and I need to validate if an input date (in format YYYYMMDD) belongs in a set of next 5 business days. The input date is not a clear indicator of the current date so I am using the following function to get the current date and then calculating the next 5 business days from it.
const std::string& CurrentDateStr() {
static const std::string sDate = []() {
time_t currTime = time(NULL);
struct tm timeinfo;
localtime_r(&currTime, &timeinfo);
char buffer[16]="";
strftime(buffer, sizeof(buffer), "%Y%m%d", &timeinfo);
return std::string(buffer);
} ();
return sDate;
}
This function returns me the correct current date if the process was started today but if the process continues running till tomorrow then it will return me yesterday's date as current date due to which calculation of next 5 business days from current date goes for a toss.
Is this expected ? Is there some workaround for it or is there a better way to implement the requirement using standard C++
Your issue is the static variable. You should read up on that, because you're going to encounter it a lot. This is what the comments were trying to get you to do. You can fix your issue by just removing it:
const std::string& CurrentDateStr() {
time_t currTime = time(NULL);
struct tm timeinfo;
localtime_r(&currTime, &timeinfo);
char buffer[16]="";
strftime(buffer, sizeof(buffer), "%Y%m%d", &timeinfo);
return std::string(buffer);
}
For a more modern solution, as suggested in the comments as well, read up on chrono. Especially system_clock::now().
one way to do it using chrono:
#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>
int main()
{
while (true)
{
theTime currentTime = time(nullptr);
tm* date = gmtime(&currentTime);
// Print the date and time
std::cout << "Current date and time: " << date->theDay << "/" << date->theMon + 1 << "/" << date->theYear + 1900;
std::cout << " " << date->theHour << ":" << date->theMmin << ":" << date->theSec << std::endl;
// Wait for 1 minute
std::this_thread::sleep_for(std::chrono::minutes(1));
}
}
OR Use the sleep method.
#include <iostream>
#include <ctime>
#include <unistd.h>
int main()
{
while (true)
{
time_t currentTime = time(nullptr);
tm* date = gmtime(&currentTime);
std::cout << "Current date and time: " << date->tm_mday << "/" << date->tm_mon + 1 << "/" << date->tm_year + 1900;
std::cout << " " << date->tm_hour << ":" << date->tm_min << std::endl;
// Wait for 1 minute (60 seconds)
sleep(60);
}
}

How to correctly read and increment dates in c++ using localtime and mktime?

I am struggling to execute a simple task. I want to take a date parameter from the command line argument and increment it several times by 1 day. The basic program should:
read the command line argument as the starting date, and
loop several times, incrementing that date by 1 day every time.
I convert the char* command line argument to a struct tm, then convert the struct tm to a time_t and add 60 * 60 * 24 = 1 day to it. I convert it back to struct tm to print it.
Here is the code:
#include <iostream>
#include <cstdlib>
#include <string>
#include "time.h"
int main(int argc, char *argv[])
{
char* start_date;
tm tm_start_date = {}; // solution: needs initialization
start_date = argv[1];
strptime(start_date, "%Y-%m-%d", &tm_start_date); // YYYY-MM-DD
char ch_stmt_date[11] = "";
time_t t_stmt_date = 0;
tm tm_stmt_date = {}; // solution: needs initialization;
tm_stmt_date = tm_start_date;
// time_t -> tm localtime_r(time_t, tm)
// tm -> time_t mktime(tm) returns time_t
std::cout << "start_date: " << start_date << " / tm_start_date: " << std::to_string(1900 + tm_start_date.tm_year) + std::to_string(tm_start_date.tm_mon + 1) +
std::to_string(tm_start_date.tm_mday) << std::endl;
// increment by 1 day per iteration
for (int i=0; i<5; i++)
{
// tm -> t_time
t_stmt_date = mktime(&tm_stmt_date);
std::cout << "t_stmt_date: " << t_stmt_date << std::endl;
// + 1 day
t_stmt_date += 60*60*24;
std::cout << "t_stmt_date: " << t_stmt_date << std::endl;
// time_t -> tm
localtime_r(&t_stmt_date, &tm_stmt_date);
strftime (ch_stmt_date, 11, "%Y-%m-%d", &tm_stmt_date);
std::cout << "ch_stmt_date: " << ch_stmt_date << std::endl;
}
return EXIT_SUCCESS;
}
The start date is correctly read and parsed into the struct tm.
However, subsequently I get one of two behaviors of the program:
Either I get a -1 on the first call of t_stmt_date = mktime(&tm_stmt_date); and a value of t_stmt_date: 86399 (1970-01-02) in the output. The rest of the loop then works correctly and iterates 5 times, incrementing 1970-01-02 by 1 day.
Or, the same code using the same command line parameter parses a nonsensical value on the first call of t_stmt_date = mktime(&tm_stmt_date); in the loop which is not a valid date, which, however is also correctly incremented by 60*60*24 on each of the 5 loops.
At this point I am desperate to understand the issue. I am working on Ubuntu 20.04 using gcc.
Thanks for your help.
Edit: Initializing the struct tm did the trick!
[NOTE]
You explicitly mention "using localtime and mktime" in the question's title, but I wasn't sure though after reading the rest of the text if that was mandatory, or you just needed to get a task done.
If you cannot use other libraries, just let me know and I'll remove this answer.
You could use std::chrono and Howard Hinnant's date library (C++11 onwards, header-only).
Or, should you be able to use a C++20 compiler, you would only need std::chrono.
[Demo]
#include <chrono>
#include <iostream> // cout
#include <sstream> // istringstream
#include <string>
#include "date/date.h"
int main()
{
namespace ch = std::chrono;
namespace dt = date;
const std::string start_date{"2022-01-31"}; // date
std::istringstream iss{ start_date }; // to string stream
dt::sys_days start_day{}; // to a time point with a day duration
dt::from_stream(iss, "%Y-%m-%d", start_day);
for (auto day{start_day}, end_day{start_day + dt::days{3}};
day < end_day;
day += dt::days{1}) // with which we can do date arithmetic
{
std::cout << dt::format("%Y-%m-%d\n", day);
}
}
// Outputs:
//
// 2022-01-31
// 2022-02-01
// 2022-02-02

Get std::chrono timestamp as string in %H.%M.%S format

I'm working on a logging system for my console, and I'm trying to get a timestamp for when a entry was added to the log. I tried doing this;
time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char time_buffer[16] = { 0 };
std::strftime(time_buffer, sizeof(time_buffer), "%H.%M.%S", std::localtime(&now));
TextWrapped(time_buffer);
This almost works. The issue is that this works more as a clock then as a timestamp as it will not stay at the time of when the entry was logged, but increment... I think that I might have to use std::chrono::time_point, but I didn't really understand how to use it.
In C++20 there are a number of nice functions in std::chrono to do this:
#include <iostream>
#include <chrono>
#include <format>
int main()
{
// get the current time
auto now = std::chrono::system_clock::now();
// floor till the start of day
auto start_of_day = std::chrono::floor<std::chrono::days>(now);
// round time till nearest seconds
auto time_since_start_of_day = std::chrono::round<std::chrono::seconds>(now - start_of_day);
// convert to hour minute second type
std::chrono::hh_mm_ss hms { time_since_start_of_day };
std::cout << time_since_start_of_day << "\n"; // will output number of seconds since start of day
std::cout << hms << "\n"; // will output formatted time in hh:mm::ss
auto string = std::format("{}", hms);
std::cout << string << "\n"; // will output formatted time in hh:mm::ss
return 0;
}
So I figured it out.
The issue is that since my code snipped is in my render function, it will always update. My fix was to move the snippet above into my "add_log()" function, then write those timestamps into a new array, then in my for loop I would just grab from both arrays.
Kinda like so;
for (int i = 0; i < items.Size; i++) {
TextWrapped(items[i]);
TextWrapped(timestamps[i]);
}
It's a hacky solution, but it works.

Epoch time to date/time format in boost c++

In Linux, i am reading epoch time from "/proc/stat" as btime and i want to convert to readable date and time format with c++ boost.
I have tried below things and date is working properly.
time_t btime_ = 1505790902; //This is epoch time read from "/proc/stat" file.
std::wstring currentDate_ = L"";
boost::gregorian::date current_date_ =
boost::posix_time::from_time_t(btime_).date();
std::wstring year_ = boost::lexical_cast<std::wstring>
(current_date_.year());
std::wstring day_ = boost::lexical_cast<std::wstring>
(current_date_.day());
Here i am getting correct year and day. BUT How can i get time( HH::MM:SS) from above epoch time ? Let me give hint - i can try.
Thanks in Advance.
Just:
Live On Coliru
#include <ctime>
#include <boost/date_time/posix_time/posix_time_io.hpp>
int main() {
std::time_t btime_ = 1505790902; //This is epoch time read from "/proc/stat" file.
std::cout << boost::posix_time::from_time_t(btime_) << "\n";
std::cout.imbue(std::locale(std::cout.getloc(), new boost::posix_time::time_facet("%H:%M:%S")));
std::cout << boost::posix_time::from_time_t(btime_) << "\n";
}
Prints
2017-Sep-19 03:15:02
03:15:02
UPDATE
To the comment:
Live On Coliru
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>
namespace pt = boost::posix_time;
namespace g = boost::gregorian;
using local_adj = boost::date_time::c_local_adjustor<pt::ptime>;
int main() {
std::cout.imbue(std::locale(std::cout.getloc(), new pt::time_facet("%H:%M:%S")));
std::time_t btime_ = 1505790902; // This is epoch time read from "/proc/stat" file.
pt::ptime const timestamp = pt::from_time_t(btime_);
std::cout << timestamp << "\n";
// This local adjustor depends on the machine TZ settings
std::cout << local_adj::utc_to_local(timestamp) << " local time\n";
}
Prints
+ TZ=CEST
+ ./a.out
03:15:02
03:15:02 local time
+ TZ=MST
+ ./a.out
03:15:02
20:15:02 local time
You can use a time_facet. Here's an example that prints UTC date/time:
std::string PrintDateTime()
{
std::stringstream str;
boost::posix_time::time_facet *facet = new boost::posix_time::time_facet("%d.%m.%Y-%H:%M:%S-UTC");
str.imbue(std::locale(str.getloc(), facet));
str << boost::posix_time::second_clock::universal_time(); //your time point goes here
return str.str();
}
Notice that you don't need to worry about the memory management of facet. It's taken care of already from within boost.

How to get datetime in ISO 8601 format on Windows?

What's a standard way to get a date time in ISO8601 format on Windows using C++? Specifically, I would like it to be formatted as:
2017-02-22T10:00:00.123-05:00
2017-02-22T10:00:00.123 >>> -05:00 <<< # how to print the offset?
I was looking into combining the output of GetLocalTime and GetTimeZoneInformation, but this looks esoteric. There are similar questions on SO, however, I've not found a single one that prints UTC offset in a desired format. Is there a better approach?
The format specifier %z gives you the timezone offset as described in the documentation (e.g. MSDN on strftime) but lefts out the ':'. You can use it like this to get the ':' into your string:
struct tm tmNow;
time_t now = time(NULL); // Get the current time
_localtime64_s(&tmNow, &now);
char bufferTime[26];
char bufferTimezoneOffset[6];
size_t tsizTime = strftime(bufferTime, 26, "%Y-%m-%dT%H:%M:%S", &tmNow); // The current time formatted "2017-02-22T10:00:00"
size_t tsizOffset = strftime(bufferTimezoneOffset, 6, "%z", &tmNow); // The timezone offset -0500
strncpy_s(&bufferTime[tsizTime], 26, bufferTimezoneOffset, 3); // Adds the hour part of the timezone offset
bufferTime[tsizTime + 3] = ':'; // insert ':'
strncpy_s(&bufferTime[tsizTime + 4], 26, &bufferTimezoneOffset[3], 3); // Adds the minutes part of the timezone offset
puts(bufferTime); // Your output: "2017-02-22T10:00:00-05:00"
I left out the milliseconds, as they are not part of the localtime as far as I know.
Maybe something like this. We call GetLocalTime and GetTimeZoneInformation then pass it to the function which returns formatted string.
This is written quickly, not tested besides observing the fact it returns correct result on my machine now. It operates on the fact that SYSTEMTIME has a member Bias where UTC = Localtime + Bias and Bias is set in minutes. So get hours by dividing by 60 and taking absolute value of that. Then we get the minutes in similar way and set the sign depending on if Bias > 0
#include <Windows.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
std::string format_system_time(const SYSTEMTIME& sys_time, const TIME_ZONE_INFORMATION& time_zone)
{
std::ostringstream formatted_date_time;
formatted_date_time << std::setfill('0');
formatted_date_time << sys_time.wYear << "-" << std::setw(2) << sys_time.wMonth << "-" <<
std::setw(2) << sys_time.wDay << "T" << std::setw(2) << sys_time.wHour << ":" <<
std::setw(2) << sys_time.wMinute << ":" << std::setw(2) << sys_time.wSecond << "." <<
std::setw(3) << sys_time.wMilliseconds;
//UTC = localtime + bias; bias is in minutes
int utc_offset_hours = time_zone.Bias / 60;
int utc_offset_minutes = std::abs(time_zone.Bias - (utc_offset_hours * 60));
char offset_sign = time_zone.Bias > 0 ? '-' : '+';
formatted_date_time << offset_sign << std::setw(2) << std::abs(utc_offset_hours) << ":" << utc_offset_minutes;
return formatted_date_time.str();
}
int main(int argc, char* argv[])
{
SYSTEMTIME date_and_time;
GetLocalTime(&date_and_time);
TIME_ZONE_INFORMATION time_zone;
GetTimeZoneInformation(&time_zone);
auto& formatted_date_time = format_system_time(date_and_time, time_zone);
return 0;
}
I don't think there is a drop-in solution for c++ on Windows. The closest you can get is InternetTimeFromSystemTime but it is only documented to support RFC1123.
You probably have to code it yourself with GetLocalTime + GetTimeZoneInformation + wsprintf (or GetTimeZoneInformationForYear if you are not dealing with the current time).
Using Howard Hinnant's free, open-source timezone library, which works on VS-2013 and later, but does require some installation:
#include "tz.h"
#include <iostream>
int
main()
{
using namespace std;
using namespace std::chrono;
using namespace date;
auto zt = make_zoned(current_zone(), floor<milliseconds>(system_clock::now()));
cout << format("%FT%T%Ez\n", zt);
}
This just output for me:
2017-02-22T17:29:03.859-05:00