How to get UTC time in micro-seconds with leap seconds adjusted?
I am using boost::universal_clock()
boost::posix_time::time_duration utc_time =
boost::posix_time::microsec_clock::universal_time() -
boost::posix_time::from_time_t(0);
int64_t total_microsec = utc_time.total_microseconds();
But the result of this is same as
std::chrono::time_point_cast<std::chrono::duration<int64_t, std::microseconds>>(
std::chrono::system_clock::now())
.time_since_epoch()
.count();
which seems that boost is not adjusting leap seconds. However when I use C gmtime() I get different result:
time_t mytime;
struct tm *ptm;
time(&mytime); // Get local time in time_t
ptm = gmtime(&mytime); // Find out UTC time
time_t utctime = mktime(ptm); // Get UTC time as time_t
How to achieve it using boost library?
Using Howard Hinnant's date/time library this is very easy:
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
auto now = floor<microseconds>(utc_clock::now());
cout << now << '\n';
cout << now.time_since_epoch() << '\n';
cout << clock_cast<system_clock>(now).time_since_epoch() << '\n';
}
This is a preview of the C++20 additions to <chrono>, but in namespace date.
utc_clock counts time since 1970-01-01 00:00:00 UTC including leap seconds. The clock_cast casts the time_point from this clock back to that of system_clock just for comparison purposes.
This program just output for me:
2019-08-10 00:19:04.388788
1565396371388788µs
1565396344388788µs
Some installation required.
Related
I have a bunch of tasks which are in the order of microseconds, the below code prints only until seconds (Thu Oct 21 12:48:20 2021) so comparing the values of start and finish always ends up giving 0. I want to be able to compare in the order of milliseconds and microseconds. Is there a function to help with this?
Also, is there a way to convert uint64_t current1 = std::chrono::system_clock::now().time_since_epoch().count(); to time_t to print out the current time based on the count()?
const auto p1 = std::chrono::system_clock::now();
std::time_t now = std::chrono::system_clock::to_time_t(p1);
std::cout << "now: " << std::ctime(&now);
I recommend skipping the C timing API entirely. It is error-prone and doesn't handle sub-second precision.
If UTC (as opposed to local time) is ok, then there is a header-only, open-source preview of C++20 that works with C++11/14/17:
#include "date/date.h"
#include <chrono>
#include <iostream>
int
main()
{
using date::operator<<;
const auto p1 = std::chrono::system_clock::now();
std::cout << "now: " << p1 << '\n';
}
Output:
now: 2021-10-21 20:28:15.754423
To port the above program to C++20 (which is already shipping in the latest Visual Studio), just drop the #include "date/date.h" and using date::operator<<;.
If you need local time, that can be also be had in C++20 (shipping in VS), but the open-source preview of C++20 is no longer header only. There exists one source file that needs to be compiled, and depending on your needs, might require a download of the IANA tz database.
#include "date/tz.h"
#include <chrono>
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
const auto p1 = system_clock::now();
std::cout << "now: " << zoned_time{current_zone(), p1} << '\n';
}
Output:
now: 2021-10-21 16:28:15.754423 EDT
The above syntax assumes C++17. For C++11/14 the template parameter for zoned_time needs to be specified: zoned_time<system_clock::duration>.
The above program ports to C++20 by dropping #include "date/tz.h" and using namespace date;.
In either program you can truncate to millisecond precision with:
const auto p1 = floor<milliseconds>(system_clock::now());
time_t is usually an integer specifying (whole) seconds.
You could get the millseconds by subtracting the whole-second time_t from now:
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
p1 - std::chrono::system_clock::from_time_t(now)).count();
or using operator%:
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>p1)
.time_since_epoch() % std::chrono::seconds(1);
std::cout << ms.count();
Example how you could do the formatting:
#include <chrono>
#include <iostream>
#include <iomanip>
int main() {
using Clock = std::chrono::system_clock;
using Precision = std::chrono::milliseconds;
auto time_point = Clock::now();
// extract std::time_t from time_point
std::time_t t = Clock::to_time_t(time_point);
// output the part supported by std::tm
std::cout << std::put_time(std::localtime(&t), "%FT%T."); // select format here
// get duration since epoch
auto dur = time_point.time_since_epoch();
// extract the sub second part from the duration since epoch
auto ss =
std::chrono::duration_cast<Precision>(dur) % std::chrono::seconds{1};
// output the millisecond part
std::cout << std::setfill('0') << std::setw(3) << ss.count();
}
A very simple question. I have been using date.h to get epoch in milliseconds and nanoseconds from a full datestamp.
istringstream temp_ss1{timestamp1};
sys_time<nanoseconds> tp1;
temp_ss1 >> parse("%Y/%m/%d,%T", tp1);
std::cout << tp1.time_since_epoch().count() << "ns\n";
What is the method to go from a millisecond epoch to a full datestamp with milliseconds?
Thank you very much for your help !
If you're not picky about the format, you can stream the sys_time<milliseconds> out. If you would like to control the format, you can use date::format with these formatting flags. For example:
std::cout << format("%Y/%m/%d,%T", tp1) << '\n';
The precision of the output will match the precision of the time_point.
Is there a method to convert. "long epoch = 1609330278454" is there a function which take a long and prints a date?
#include "date/date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
long epoch = 1609330278454;
cout << sys_time<milliseconds>{milliseconds{epoch}} << '\n';
}
Outputs:
2020-12-30 12:11:18.454
You can customize the format like so:
long epoch = 1609330278454;
sys_time<milliseconds> tp{milliseconds{epoch}};
cout << format("%Y/%m/%d,%T", tp) << '\n';
Output:
2020/12/30,12:11:18.454
So right now, the code im using is
using std::chrono::system_clock;
std::time_t tt = system_clock::to_time_t (system_clock::now());
struct std::tm * ptm = std::localtime(&tt);
std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n';
std::this_thread::sleep_for (std::chrono::seconds(7));
It is simple in that this is in a loop, and chrono sleep_for delays the system for however many seconds.
The problem is that it is in the HH:MM:SS format when I really need seconds.milliseconds to show the system clock transaction time. How would I do this? I really just need someone to explain the code, why is it making a struct? And what should I do to change the format? Thanks!
I've got two answers for you:
How to do this next year (in C++20), which isn't implemented today, and
How you can do it today with some minor syntax changes and an open-source 3rd party library.
First 1:
#include <chrono>
#include <iostream>
#include <thread>
int
main()
{
using namespace std::chrono;
auto tp = system_clock::now();
while (true)
{
zoned_time zt{current_zone(), floor<milliseconds>(system_clock::now())};
cout << "Current time: " << std::format("{:%T}", zt) << '\n';
tp += 7s;
std::this_thread::sleep_until (tp);
}
}
This creates a local time using the computer's currently set time zone, with a precision of milliseconds. And then just prints it out with the desired format (%T). I'm using sleep_until instead of sleep_for so that each iteration of the loop doesn't drift off of the desired 7s interval due to loop overhead.
Second 2:
Nobody has C++20 chrono yet, but you can approximate it today with Howard Hinnant's free open source date/time library:
#include "date/tz.h"
#include <chrono>
#include <iostream>
#include <thread>
int
main()
{
using namespace date;
using namespace std::chrono;
auto tp = system_clock::now();
while (true)
{
zoned_time zt{current_zone(), floor<milliseconds>(system_clock::now())};
cout << "Current time: " << format("%T", zt) << '\n';
tp += 7s;
std::this_thread::sleep_until (tp);
}
}
The difference is that the format statement is slightly different, and the library lives in namespace date instead of namespace std::chrono. And there's an extra header to include. And some installation is required to handle the time zones.
If you're happy with a UTC time stamp, instead of a local time stamp, then you can use a header-only version of the same library like this (no installation required):
#include "date/date.h"
#include <iostream>
#include <thread>
int
main()
{
auto tp = std::chrono::system_clock::now();
while (true)
{
using namespace date;
using namespace std::chrono;
std::cout << "Current time: "
<< format("%T", floor<milliseconds>(system_clock::now())) << '\n';
tp += 7s;
std::this_thread::sleep_until (tp);
}
}
This is what I am trying to do:
Get the local time (from the system);
Convert that time to the UTC format and associate it with some member variable of current object.
Later on, given the timezone of the user, I wish to convert it into the correct local time and display it to the user.
Looking up few things on SO and CppReference, I could come up with the following snippet:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
int main()
{
time_t lt = std::time(0);
//time(0) gives current time, but LTime shows the UTC time (not local time)
string LTime = std::ctime(<); //localtime() gives error
cout<<LTime;
//timestamp = mktime(&tm) - timezone;
//time_t timestamp = mktime(&tm) - _timezone;
//std::cout << "timestamp: " << std::put_time(timestamp, "%c %Z") << '\n';
return 0;
}
The example on cppreference.com illustrates how the value can be printed using put_time(); but how to store it in a variable?
How to convert the UTC time format to current timezone (given some timezone as the input)? I tried using the commented code above as per this link but it does not take any parameter.
You can use local time get the local time and gmt time for UTC
You can set the Time zone using the list Time zone wiki
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
std::time_t result = std::time(nullptr);
auto local = std::asctime(std::localtime(&result));
std::cout <<local;
std::cout << "UTC: " << std::put_time(std::gmtime(&result), "%c %Z") << '\n';
putenv("TZ=Asia/Singapore");
local = std::asctime(std::localtime(&result));
std::cout <<"Asia/Singapore Time "<<local;
}
Output
Thu Sep 14 21:59:37 2017
UTC: Fri Sep 15 01:59:37 2017 UTC
Asia/Singapore Time Fri Sep 15 09:59:37 2017
Program ended with exit code: 0
I'm looking for a way to retrieve the start of the current year as a unix timestamp.
For example if we're on 2017-10-16 the unix timestamp is 1523318400. I have to retrieve 1483228800 (2017-01-01) instead. And it must work for the next years too of course.
There are functions to add and subtract a number of months, days, minutes and seconds from a time_t, which can be used to calculate the the time_t for a point in the past, but it looks quite awkward to find the correct number of units to remove. (cpp reference : time_point). I also looked at original C function mktime. However, whilst creating a time_t and then a struct tm*, the issue is correctly generating a timezone correct version.
So my solution is something like this....
int getYear(time_t now)
{
struct tm * tnow = std::gmtime(&now);
return tnow->tm_year + 1900;
}
std::time_t calculateYear( int currentYear )
{
int epochYear = currentYear - 1970;
int leapYears = (epochYear + 1) / 4;
time_t result = epochYear * 24 * 60 * 60 * 365;
result += leapYears * 24 * 60 * 60;
return result;
}
The code is good for years between 1970 (first time_t value) and 2100, which is not a leap year from a 100 year rule.
The number of leap years is strange, as whilst 2012 is a leap year, it is 2013 which is the first year beginning to count it.
You could use Howard Hinnant's free, open-source C++11/14/17 date/time library. It would be as simple as this:
#include "date/date.h"
date::sys_seconds
start_of_year(date::sys_seconds t)
{
using namespace date;
return sys_days{year_month_day{floor<days>(t)}.year()/jan/1};
}
You could use it like this:
#include <iostream>
int
main()
{
using date::operator<<;
using namespace std::chrono_literals;
std::cout << start_of_year(date::sys_seconds{1523318400s}) << '\n';
}
This outputs:
1514764800s
Note that this is not the answer you said you wanted. However it is correct. You can debug this discrepancy with this library as well:
std::cout << date::sys_seconds{1523318400s} << '\n';
This outputs:
2018-04-10 00:00:00
instead of 2017-10-16. You can find the Unix Time stamp for 2017-10-16 with:
using namespace date::literals;
std::cout << date::sys_seconds{date::sys_days{2017_y/10/16}}.time_since_epoch() << '\n';
which outputs:
1508112000s
And:
std::cout << start_of_year(date::sys_seconds{1508112000s}).time_since_epoch() << '\n';
will output:
1483228800s
You can also use this library to find the current year:
date::year
current_year()
{
using namespace date;
using namespace std::chrono;
return year_month_day{floor<days>(system_clock::now())}.year();
}
And you could rewrite (or overload) start_of_year to take a date::year instead of (or in addition to) date::sys_seconds:
date::sys_seconds
start_of_year(date::year y)
{
using namespace date;
return sys_days{y/jan/1};
}
And now you can write:
int
main()
{
using date::operator<<;
std::cout << start_of_year(current_year()).time_since_epoch() << '\n';
}
which currently outputs:
1483228800s