1,085 questions
9
votes
2
answers
698
views
Using Chrono, how can I get millisecond precision?
C++, using <chrono>, I would like to get a value formatted as <seconds>.<milliseconds>. I am using system_clock and get the current seconds as:
auto secs = duration_cast<seconds&...
1
vote
1
answer
118
views
VS2022 C++: Referencing static lib project with date/date.h breaks chrono
I've come across a strange issue related to Howard Hinnant's date.h library that causes all of <chrono> to break, at least according to VS. It appears to be related to using date/date.h in a ...
3
votes
3
answers
161
views
Why chrono::timezone and format are slower than localtime_s and stringstream?
I'm generating a string representation of the current time in the local time zone for my logging system. I have an "old" version, and I wanted to see if I could improve its performance.
Old ...
5
votes
1
answer
209
views
How can I parse and format RFC-9557 date times with time zones in C++?
RFC-9557 allows tagging a time stamp with time zone information such as:
2022-07-08T00:14:07+02:00[Europe/Paris]
I would like to be able to parse this into a C++20 chrono data structure and format it ...
0
votes
1
answer
238
views
Workaround for missing std::chrono::from_stream
C++20 introduces std::chrono::from_stream() to parse date/time from a string and store it in a std::chrono::time_point. For example:
std::stringstream ss("2018-12-09T00:00:00+0130");
std::...
3
votes
1
answer
208
views
How can I format a `std::chrono::year_month_day` with libfmt?
I am using gcc-13.3 with c++23 enabled.
I have the following code which uses std::format to format a std::chrono::year_month_day to a string:
#include <chrono>
#include <format>
#include &...
7
votes
1
answer
118
views
How to stream out a C++ `zoned_time` constructed with a user-written `time_zone`
In this answer
there is a user-written time zone called OffsetZone that
can be put into a zoned_time.
#include <chrono>
class OffsetZone
{
std::chrono::minutes offset_;
public:
...
6
votes
1
answer
102
views
user-written time zones in C++ with fixed offset
I have a UTC offset that is not known until run-time, and is not necessarily an
integral number of hours. The IANA time zone
database isn't a good match for this.
What is the simplest user-written ...
30
votes
1
answer
1k
views
How can I find the Nth weekday of the month using chrono?
There are lots of Stack Overflow Q&A's solving this problem in other languages, but not so much in C++.
How do I find the Nth weekday of the month using <chrono>? For
example, what is the ...
8
votes
1
answer
143
views
How do I create a Julian calendar that will interoperate with chrono?
I would like to convert back and forth between the Julian calendar and
<chrono>'s civil calendar. What is the best way to do that?
11
votes
1
answer
755
views
Given the year, how do I compute when Easter is, using chrono
How do I write:
auto // either sys_days or year_month_day
easter(std::chrono::year y);
I'm aware of the SO question Function to return date of Easter for the given year but I would like to know how ...
22
votes
1
answer
1k
views
Find next weekday using chrono
Given a year, month and day, how do I find the next weekday on or after that
date? For example, If I have 2025-04-28, how can I find the Friday
following 2025-04-28, which is 2025-05-02?
What about ...
4
votes
2
answers
124
views
steady_clock overflow when compared to min?
The following code seems to overflow. Why?
#include <chrono>
#include <iostream>
int main() {
auto now = std::chrono::steady_clock::now();
auto low = std::chrono::steady_clock::...
2
votes
1
answer
73
views
Is there a way to parse a std::chrono::duration with a string literal as a field in an XML file using boost::property_tree?
I would like to be able to write my config file as:
<?xml version="1.0" encoding="UTF-8"?>
<Config>
<DurationValue>20ns</DurationValue>
</Config>
...
2
votes
1
answer
87
views
What is the highest resolution which can be printed or formatted for a std::chrono::time_point?
I am writing some logic to serialize a std::chrono::time_point object to a JSON string.
Here is the code I have so far:
std::string get_json(const std::chrono::time_point<std::chrono::system_clock&...
4
votes
0
answers
141
views
Chrono: timestamp after 2038
I'm having some issues using chrono after 2038.
My system is a 32Bit system with the following configuration
Kernel: 5.15.118
GCC: 13.2.0
GLIBC: 2.38
I have compiled my program with the flags ...
13
votes
2
answers
533
views
Is it "legitimate" to define an std::chrono Clock which doesn't really offer a now() function?
I'm writing a C++ library, which works with data that has some time values in it - values that do not originate in the system the library runs on. I want to put such values in std::chrono::time_point'...
-3
votes
3
answers
229
views
How should I idiomatically represent time intervals (not just durations) in C++?
I like C++11's std::chrono facilities, which let me work with:
time points
clocks
durations (= differences between time points)
but I occasionally need to work with time intervals, i.e. not just the ...
1
vote
1
answer
184
views
Is it possible to use std::chrono to create a datetime type which is not linked to a particular clock or implementation of a "now" function?
I'm trying to figure out how to use std::chrono to create and manipulate a datetime type.
In particular, I need some kind of type to store a datetime type. The input and output of my program will be ...
6
votes
1
answer
445
views
std::chrono::tzdb: cannot locate zone: America/New_York
A random unit test started failing, and I can't remember touching anything pertinent. Here's a minimal reproducible (on my machine) example:
#include <chrono>
#include <string>
#include &...
5
votes
3
answers
271
views
specify precision using std::formatter with std::chrono::sys_time
I have this simple code
#include <chrono>
#include <format>
#include <iostream>
int main()
{
auto time = std::chrono::system_clock::now();
std::cout << std::format(&...
1
vote
1
answer
120
views
Precision of chrono duration with double does not appear in format printing
In the following code:
using seconds = std::chrono::duration<long double, std::ratio<1, 1>>;
using namespace std::literals;
int main() {
seconds s = 10ms + 10us + 10ns;
std::print(...
1
vote
1
answer
42
views
Is there a way to read an infinite time duration using XML boost::property_tree?
I'm trying to use boost::property_tree to read in an infinite time duration from an XML config file for certain configurations (non-infinite for others). This code works for non-infinite durations.
My ...
2
votes
1
answer
220
views
Why can I not wait for 10^12 picoseconds using std::chrono and long long as a representation?
The following code fails to wait for picoseconds_longlong(1'000'000'000'000). Why?
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
using ...
-1
votes
1
answer
130
views
C++ - How to prevent function reordering in MSVC
I have created simple function and tried to measure execution time of it.
Unfortunately because of function reordering measured time is always 0.
My sample code:
#include <iostream>
#include <...
0
votes
2
answers
139
views
Which C++ time mechanism to use?
I'm trying to write a BW generator program that sends some fixed number of reads/writes every second and then sleeps for the rest of the time. Pseudocode:
for <number of iterations>
...
3
votes
3
answers
204
views
C++ chrono: store and retrieve date and time in 64 bit format for 2038 rollover
2038 is coming...I have to think date time in 64 bit! I'm trying to understand how to do this in C++, with chrono library, but I'm failing miserably so far.
I will have to communicate a date time ...
1
vote
1
answer
118
views
std::format specifier with multiple custom format specifiers
I want to print a row of time values in the format %H%M%S and align each "cell" to specific multiple of width using std::format. My current approach is this:
tps_headings += std::format(&...
1
vote
1
answer
122
views
Easiest way to convert std::chrono::microseconds to %H%M%S
I have a std::chrono::microseconds duration that denotes the amount of microseconds that have passed since the last midnight. What's the easiest way to print it in the time format %H%M%S in c++20?
...
1
vote
1
answer
79
views
`std::chrono::parse()`ing std::string in Eastern time
I'm trying to convert std::strings such as "2024-11-19 09:30:00.037000" **that I know to always be in Eastern time) to std::chrono objects and vice versa, but I can't get anything to work ...
2
votes
1
answer
264
views
How to set a system_clock time_point with year, month, day
I want to convert a UTC year, month, day to a std::chrono::time_point (and also display it).
#include <chrono>
using namespace std::chrono;
void DisplayDate(int y, int m, int d) {//e.g. 2024, ...
0
votes
1
answer
216
views
Add 1 day to a `year_month_day` std::chrono object?
Using C++20, given (year, month, day) as integers, I want to find the next day, also as (year, month, day). There is a data type in std::chrono, year_month_day, that seems appropriate. But I cannot ...
3
votes
1
answer
332
views
How to get std::chrono::year_month_day of today?
For reference, I'm in GMT+1. I set my system clock to 0:13 am today (nov 15th 2024). Trying the canonical code from cppreference:
const std::chrono::time_point<std::chrono::system_clock> now = ...
0
votes
1
answer
62
views
Convert time_point -> Rep and back again (Rep -> time_point)
How to update date in a datepicker element. I have a int64_t attribute m_myDate that contains the date.
TimeSpan is a specialization of std::chrono::duration.
DateTime a specialization of std::chrono:...
2
votes
2
answers
208
views
c++ std::chrono implicit conversion standard method
What is the standard/typical method when implicitly converting between time units using std::chrono?
E.g. from ms to ns
// #1
constexpr uint64_t foo_ns = std::chrono::milliseconds(500) / std::chrono::...
1
vote
1
answer
85
views
Is there a simple way to make the units of a std::chrono::duration configurable?
I'm attempting to allow setting the units that a std::chrono::duration is reported in by modifying an xml config file parsed by boost::property_tree. My current, non-compiling solution attempts to do ...
1
vote
1
answer
170
views
Can std::chrono's time_point silently overflow?
#include <iostream>
#include <chrono>
int main()
{
constexpr std::chrono::duration<float> kLastWarningMsgDuration{ 10 };
// default time point (zero)
std::chrono::...
2
votes
1
answer
114
views
Why can't I get C++ date formatting to be different in different locales?
This program should, I think, produce different results if in a different locale:
#include <chrono>
#include <iostream>
#include <locale>
using namespace std;
int main(int, char *...
3
votes
2
answers
217
views
Why does the order of std::chrono::floor matter here?
This works:
year_month_day day() {
return year_month_day(floor<days>(
zoned_time{
zone,
system_clock::now()
}.get_local_time()));
}
...
2
votes
1
answer
84
views
how to create accurate c++ timers loops for RTSP captures
So I'm trying to capture a stream and write it to 30 seconds chunks. I don't know what the cameras fps will be so need to factor in that I want to only capture 15 fps.
my problem is that the following ...
1
vote
1
answer
119
views
How to get value of std::formatter<std::chrono::zoned_time> to use repeatedly?
How to get value of std::formatter<std::chrono::zoned_time> to use repeatedly?
I need to be able to format time with the same format string repeatedly. I can't seem to figure out how to keep the ...
2
votes
1
answer
137
views
error while converting zoned time into get_local_time using Howard Hinnant's date library
#include "date/tz.h"
#include "date/date.h"
#include <iostream>
#include <string>
int main()
{
using namespace date;
using namespace std;
using namespace chrono;
auto ...
1
vote
1
answer
140
views
chrono usage wirh timezone
I have a duration as string e.g. "13:00:00" and a time_point parsed to a utc_time from an UTC time string using std::chrono::parse function.
Now given this duration and a timezone e.g. "...
4
votes
1
answer
201
views
Why can't chrono::parse parse a POSIX date and time string?
I'm trying to use the <chrono> library to parse a POSIX date and time string, no matter what the global locale is, based on this code:
#include <iostream>
#include <locale>
#include &...
6
votes
2
answers
701
views
std::chrono now() induces very significant drift with Ubuntu 24.04 under WSL2
I've got an algorithm I've used for years to emulate a real-time clock in a non-real-time environment. It's always worked exceptionally well until used on Ubuntu 24.04 in WSL2 in Windows 11. The logic ...
3
votes
1
answer
200
views
Creating alias by 'using' keyword for std::chrono is throwing compiler errors
I am trying to use alias for std::chrono and std::chrono::duration_cast<std::chrono::seconds> which are throwing compilation errors. I included <chrono> header file. Below is the code:
#...
1
vote
2
answers
142
views
Selecting a uniform distribution type from the standard library
Summary: Given a numeric type T, is there a concise way to declare a variable as either std::uniform_int_distribution<T> or std::uniform_real_distribution<T> depending upon whether T is ...
3
votes
3
answers
213
views
Syntactic sugar for timing a block of code in C++
Methods to time a block of code in C++ have been well documented, e.g.,
here and here.
In the answers to the questions linked, we usually need to write some lines of code at the start and end of the ...
2
votes
1
answer
137
views
c++20 gps_clock initilize from string
I want to set the variable of type gps_clock from a string, e.g. "2024-08-09T22:20:50".
My linux distribution uses gcc13. In this version the methods of std::chrono::from_stream and std::...
4
votes
1
answer
187
views
std::chrono::parse into zoned time
I need to parse a timestamp with timezone into a c++ timepoint.
My environment is linux and gcc 14.1 and -std=c++23.
My definitions are:
namespace MyTime {
using clock_t = std::chrono::...