How to convert seconds to hh:mm:ss.millisecond format c++? - c++

I need to convert seconds in to hh:mm:ss.milliseconds and I need that this format must be respected. I mean that hours, minutes and seconds must have 2 digits and milliseconds 3 digits.
For example, if seconds = 3907.1 I would to obtain 01:05:07.100
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <sstream>
#include <math.h>
using namespace std;
int main()
{
double sec_tot = 3907.1;
double hour = sec_tot/3600; // seconds in hours
double hour_int;
double hour_fra = modf(hour, &hour_int );//split integer and decimal part of hours
double minutes = hour_fra*60; // dacimal hours in minutes
double minutes_int;
double minutes_fra = modf(minutes, &minutes_int); // split integer and decimal part of minutes
double seconds = minutes_fra*60; // decimal minutes in seconds
stringstream ss;
ss << ("%02lf", hour_int) << ":" << ("%02lf", minutes_int) << ":" << ("%02lf", seconds);
string time_obs_def = ss.str();
cout << time_obs_def << endl;
return 0;
}
but the output is 1:5:7.1
Thank you.

Nowadays you should probably use the chrono duration std::chrono::milliseconds for such a task, but if you'd like make our own type to support formatting, something like this should do it:
#include <iomanip> // std::setw & std::setfill
#include <iostream>
// your own type
struct seconds_t {
double value;
};
// ostream operator for your type:
std::ostream& operator<<(std::ostream& os, const seconds_t& v) {
// convert to milliseconds
int ms = static_cast<int>(v.value * 1000.);
int h = ms / (1000 * 60 * 60);
ms -= h * (1000 * 60 * 60);
int m = ms / (1000 * 60);
ms -= m * (1000 * 60);
int s = ms / 1000;
ms -= s * 1000;
return os << std::setfill('0') << std::setw(2) << h << ':' << std::setw(2) << m
<< ':' << std::setw(2) << s << '.' << std::setw(3) << ms;
}
int main() {
seconds_t m{3907.1};
std::cout << m << "\n";
}

Coming in C++20:
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int
main()
{
double sec_tot = 3907.1;
cout << format("{:%T}\n", round<milliseconds>(duration<double>{sec_tot}));
}

printf style format specifiers do not work. You will need to use the stream manipulators to set the width and fill character.
ss << std:setw(2) << std::setfill('0') << hour_int;

This will also add a leading zero to enforce hh:mm:ss format.
If hh is 00, returns only mm:ss.
ms are left out from this example, but easy to add.
#include <iostream>
std::string convertSecondsToHHMMSS (int value) {
std::string result;
// compute h, m, s
std::string h = std::to_string(value / 3600);
std::string m = std::to_string((value % 3600) / 60);
std::string s = std::to_string(value % 60);
// add leading zero if needed
std::string hh = std::string(2 - h.length(), '0') + h;
std::string mm = std::string(2 - m.length(), '0') + m;
std::string ss = std::string(2 - s.length(), '0') + s;
// return mm:ss if hh is 00
if (hh.compare("00") != 0) {
result = hh + ':' + mm + ":" + ss;
}
else {
result = mm + ":" + ss;
}
return result;
}
int main() {
std::cout << convertSecondsToHHMMSS(3601) << "\n";
std::cout << convertSecondsToHHMMSS(1111) << "\n";
std::cout << convertSecondsToHHMMSS(60) << "\n";
std::cout << convertSecondsToHHMMSS(12) << "\n";
std::cout << convertSecondsToHHMMSS(0) << "\n";
}

Related

How can I output bit pattern of infinity and NaN in C++?(IEEE standard)

I'm reading Computer Systems: A Programmer’s Perspective, then I found the Special Values's definition and corresponding bit patterns.
Now, I wanna output their bits using C++. I use their macro to output bits, obviously is incorrect, because macro defined to Integer!
#define FP_NAN 0x0100
#define FP_NORMAL 0x0400
#define FP_INFINITE (FP_NAN | FP_NORMAL)
What should I do to correctly output bits in the image above? and, Why compiler defined those Integer macros rather than IEEE standard?
below is my code.
#include <iostream>
#include <cmath>
#include <bitset>
using namespace std;
union U {
float f;
int i;
};
int main() {
U u1, u2;
u1.f = FP_NAN;
u2.f = FP_INFINITE;
cout << bitset<32>(u1.i) << endl;
cout << bitset<32>(u2.i) << endl;
return 0;
}
output:
01000011100000000000000000000000
01000100101000000000000000000000
My computer environment:
win10
mingw64
I wrote a quick-and-dirty double bit-wise output program a while back. You could modify it to work for float.
It has ANSI escape sequences in it, which might not be suitable for your environment.
The key part is just using a byte memory pointer and examining the bit state directly, rather than trying to get std::bitset to play nice.
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using std::cout;
using std::fpclassify;
using std::memcpy;
using std::nan;
using std::numeric_limits;
using std::reverse;
using std::setw;
using std::size_t;
using std::string;
using std::stringstream;
using std::uint32_t;
using std::uint64_t;
namespace {
uint32_t low32_from(double d) {
char const* p = reinterpret_cast<char const*>(&d);
uint32_t result;
memcpy(&result, p, sizeof result);
return result;
}
uint32_t high32_from(double d) {
char const* p = reinterpret_cast<char const*>(&d);
p += 4;
uint32_t result;
memcpy(&result, p, sizeof result);
return result;
}
string hexstr(uint32_t value) {
char hex[] = "0123456789ABCDEF";
unsigned char buffer[4];
memcpy(buffer, &value, sizeof buffer);
auto p = &buffer[0];
stringstream ss;
char const* sep = "";
for (size_t i = 0; i < sizeof buffer; ++i) {
ss << sep << hex[(*p >> 4) & 0xF] << hex[*p & 0xF];
sep = " ";
++p;
}
return ss.str();
}
string bits(uint64_t v, size_t len) {
string s;
int group = 0;
while (len--) {
if (group == 4) { s.push_back('\''); group = 0; }
s.push_back(v & 1 ? '1' : '0');
v >>= 1;
++group;
}
reverse(s.begin(), s.end());
return s;
}
string doublebits(double d) {
auto dx = fpclassify(d);
unsigned char buffer[8];
memcpy(buffer, &d, sizeof buffer);
stringstream ss;
uint64_t s = (buffer[7] >> 7) & 0x1;
uint64_t e = ((buffer[7] & 0x7FU) << 4) | ((buffer[6] >> 4) & 0xFU);
uint64_t f = buffer[6] & 0xFU;
f = (f << 8) + (buffer[5] & 0xFFU);
f = (f << 8) + (buffer[4] & 0xFFU);
f = (f << 8) + (buffer[3] & 0xFFU);
f = (f << 8) + (buffer[2] & 0xFFU);
f = (f << 8) + (buffer[1] & 0xFFU);
f = (f << 8) + (buffer[0] & 0xFFU);
ss << "sign:\033[0;32m" << bits(s, 1) << "\033[0m ";
if (s) ss << "(-) ";
else ss << "(+) ";
ss << "exp:\033[0;33m" << bits(e, 11) << "\033[0m ";
ss << "(" << setw(5) << (static_cast<int>(e) - 1023) << ") ";
ss << "frac:";
// 'i' for implied 1 bit, '.' for not applicable (so things align correctly).
if (dx == FP_NORMAL) ss << "\033[0;34mi";
else ss << "\033[0;37m.\033[34m";
ss << bits(f, 52) << "\033[0m";
if (dx == FP_INFINITE) ss << " \033[35mInfinite\033[0m";
else if (dx == FP_NAN) ss << " \033[35mNot-A-Number\033[0m";
else if (dx == FP_NORMAL) ss << " \033[35mNormal\033[0m";
else if (dx == FP_SUBNORMAL) ss << " \033[35mDenormalized\033[0m";
else if (dx == FP_ZERO) ss << " \033[35mZero\033[0m";
ss << " " << d;
return ss.str();
}
} // anon
int main() {
auto lo = low32_from(1111.2222);
auto hi = high32_from(1111.2222);
cout << hexstr(lo) << "\n";
cout << hexstr(hi) << "\n";
cout << doublebits(1111.2222) << "\n";
cout << doublebits(1.0) << "\n";
cout << doublebits(-1.0) << "\n";
cout << doublebits(+0.0) << "\n";
cout << doublebits(-0.0) << "\n";
cout << doublebits(numeric_limits<double>::infinity()) << "\n";
cout << doublebits(-numeric_limits<double>::infinity()) << "\n";
cout << doublebits(nan("")) << "\n";
double x = 1.0;
while (x > 0.0) {
cout << doublebits(x) << "\n";
x = x / 2.0;
}
}
There are multiple problems with your code.
Problem #1:
FP_NAN and FP_INFINITE are not constants representing return values of std::fpclassify, which returns classification of given floating point number.
Problem 2:
Accessing inactive union member, i.e. not the latest assigned to, is UB. Most robust, well-known way to inspect memory representation of an object is to memcpy it into char buffer.
Taking it into account, you can write your code in following way:
#include <bitset>
#include <cmath> // nanf
#include <cstring> // memcpy
#include <iostream>
#include <limits>
#include <ranges>
template <typename T> // Template, because reusability
void print_bits(const T& t)
{
char buffer[sizeof(T)];
std::memcpy(buffer, &t, sizeof(T));
for (char c: buffer | std::views::reverse) //Endianness
{
std::cout << std::bitset<8>(c);
}
}
int main()
{
const double nan = std::nanf("");
const double inf = std::numeric_limits<float>::infinity();
print_bits(nan);
std::cout << '\n';
print_bits(inf);
std::cout << '\n';
}
0111111111111000000000000000000000000000000000000000000000000000
0111111111110000000000000000000000000000000000000000000000000000
http://coliru.stacked-crooked.com/a/0d6c30067c9e7e6a

NTP timestamp is not correctly parsed c++

I want to send an SNTP req and then parse the response, hence I started by simply creating a timestamp convert it to ntp and then try to read the seconds and the fractional part. The issue is that the seconds part is not correctly parsed in the first place, always shifted by a few seconds. This is what I have done:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream> // Needed to perform IO operations
#include <ctime>
constexpr auto SECONDS_SINCE_FIRST_EPOCH = (2208988800ULL);
constexpr auto NTP_SCALE_FRAC = (4294967296ULL);
using namespace std;
int main()
{
std::time_t _timeEpoch = std::time(nullptr);
unsigned long long tv_ntp, tv_usecs, tv_time;
tv_ntp = _timeEpoch + SECONDS_SINCE_FIRST_EPOCH;
tv_usecs = (NTP_SCALE_FRAC * _timeEpoch) / 1000000UL;
tv_time = (tv_ntp << 32) | tv_usecs;
std::cout << "GetUtcTimeMilliSeconds UTC sec: " << _timeEpoch << " 1900: " << tv_ntp << " fraction of second " << tv_usecs << " tv_time " << tv_time << std::endl;
uint32_t msw = (tv_time >> 32) & 0xFFFFFFFF;
uint32_t lsw = tv_time & 0xFFFFFFFF;
std::cout << "GetUtcTimeMilliSeconds msw " << msw << " lsw: " << lsw << std::endl;
return 0;
}
I expected msw to be equal to tv_ntp and lsw to tv_usecs but they are not
GetUtcTimeMilliSeconds UTC sec: 1572103823 1900: 3781092623 fraction of second 6752134505701 tv_time 16239671495839982821
GetUtcTimeMilliSeconds msw 3781093167 lsw: 445916389

Converting chrono::duration type to std::tm type

I am writing a program that takes input a date from the user, uses it to initialize a tm struct, then using chrono::time_points performs some chrono::duration operation, such as getting the age.
Here is my code:
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
using namespace std;
int main(){
//representing a date
tm *birthday = new tm{00, 30, 00, 07, 11, 97};
//convert to time t
time_t bt = mktime(birthday);
//convert time_t to time_point
chrono::system_clock::time_point t = chrono::system_clock::from_time_t(bt);
chrono::system_clock::time_point now = chrono::system_clock::now();
/*.. Testing
time_t nn = chrono::system_clock::to_time_t(now);
time_t tnn = chrono::system_clock::to_time_t(t);
*/
chrono::system_clock::duration lft = now - t;
//convert to timepoint
chrono::system_clock::time_point tlft{lft};
time_t lifetime = chrono::system_clock::to_time_t(tlft);
cout << put_time(localtime(&lifetime), "%F %T") << endl;
return 0;
}
And my output is something like this:
$> 1990-02-10 09:42:46
So, according to my understanding, it performs a plain mathematical subtraction on the ticks and using localtime, converts it to a date since EPOCH that is why it is giving me 1990. I want to know, is there any way, to convert the duration straight into struct tm, so that I should get something like 20 years?
Here is how you extract a duration in the unit of your choice:
std::chrono::duration<double> lft = now - t;
using year = std::chrono::duration<int, std::ratio<31557600>>;
auto nby = std::chrono::duration_cast<year>(lft);
std::cout << nby.count() << "\n";
With this in mind, I'll suggest an implementation of the taste of:
struct Age
{
using year = std::chrono::duration<int, std::ratio<31'557'600>>;
using month = std::chrono::duration<int, std::ratio< 2'592'000>>;
using day = std::chrono::duration<int, std::ratio< 86'400>>;
using hour = std::chrono::hours;
using minute = std::chrono::minutes;
using second = std::chrono::seconds;
Age(std::chrono::system_clock::time_point birth)
: _age(std::chrono::system_clock::now() - birth)
{}
template<class Duration>
auto extract()
{
const auto result = std::chrono::duration_cast<Duration>(_age);
_age -= result;
return result;
}
friend std::ostream& operator<<(std::ostream& os, Age age)
{
const auto years = age.extract<year>();
const auto monthes = age.extract<month>();
const auto days = age.extract<day>();
const auto hours = age.extract<hour>();
const auto minutes = age.extract<minute>();
const auto seconds = age.extract<second>();
return os << years.count()
<< ":" << std::setw(2) << std::setfill('0') << monthes.count()
<< ":" << std::setw(2) << std::setfill('0') << days.count()
<< " " << std::setw(2) << std::setfill('0') << hours.count()
<< ":" << std::setw(2) << std::setfill('0') << minutes.count()
<< ":" << std::setw(2) << std::setfill('0') << seconds.count()
;
}
private:
std::chrono::duration<double> _age;
};
Prints 20:01:10 12:43:40 with your example date (live demo).

What is an intelligent way to determine max size of a strftime char array?

How can I size the char array for strftime without trial and error? Using mktime, the timestamp size N in the example has to be greater 86, otherwise I get arbitrary dates back.
e.g.
N = 86 : 2013-07-13 02:41
N = 82 : 1979-05-18 13:23
How do I efficiently scale N without prior knowledge of the date? The check >0 does not help.
#include <iostream>
#include <cstring>
#include <ctime>
#define N 86
using namespace std;
int main(void)
{
time_t t;
struct tm ts;
char timestamp[N] ;
ts.tm_min = 41;
ts.tm_hour = 2;
ts.tm_mday = 13;
ts.tm_mon = 7 - 1;
ts.tm_year = 13 - 1900 + 2000;
t = mktime(&ts);
if (strftime(timestamp, sizeof(timestamp)-1, "%Y-%m-%d %H:%M", &ts) > 0)
cout << timestamp;
else {
cerr << "strftime failed." <<endl;
return 1;
}
return 0;
}
From the documentation for strftime:
If the length of the resulting C string, including the terminating null-character, doesn't exceed maxsize, the function returns the total number of characters copied to ptr (not including the terminating null-character). Otherwise, it returns zero, and the contents of the array pointed by ptr are indeterminate.
That means if you don't know the size and can dynamically allocate a string you can do something along the lines of:
int size = N; // Some starting size
char *timestamp = malloc(size);
// Your time stuff
int result = strftime(timestamp, size - 1, "%Y-%m-%d %H:%M", &ts);
// While there isn't enough room to store the result
while (result == 0)
{
free(timestamp); // Free old data
size *= 2; // Double the size (should be more than enough)
timestamp = malloc(size); // Allocate the new size. You can check for failed allocations here as well.
// Retry
result = strftime(timestamp, size - 1, "%Y-%m-%d %H:%M", &ts);
}
std::cout << timestamp;
Because you tagged this as C++, perhaps you might consider the following.
--> Note that there is no struggle with the string size here.
// function to create a timestamp style string
std::string yyDmmDdd_hhCmmGet(time_t tt)
{
std::stringstream ss;
// goal - something like: "%Y-%m-%d %H:%M"
{
struct tm mybdtod; // linux api: my broken down time of day
// the following is a relatively slow function
::localtime_r (&tt, &mybdtod);
// linux api - convert time_t to tm as local time
ss << std::setw(4) << (mybdtod.tm_year+1900)
<< "-"
<< std::setfill('0') << std::setw(2) << mybdtod.tm_mon+1
<< "-"
<< std::setfill('0') << std::setw(2) << mybdtod.tm_mday
<< " ";
ss << std::dec << std::setfill('0') << std::setw(2)
<< mybdtod.tm_hour
<< ":"
<< std::setfill('0') << std::setw(2)
<< mybdtod.tm_min;
}
return(ss.str());
}
int t186(void)
{
struct tm ts; // linux api: time struct
::memset(&ts, 0, sizeof(tm));
ts.tm_min = 41;
ts.tm_hour = 3-1;
ts.tm_mday = 13;
ts.tm_mon = 7 - 1;
ts.tm_year = 13 - 1900 + 2000;
time_t tt = mktime(&ts); // linux api: Convert tm struct to time_t
// format time_t to string
std::string s = yyDmmDdd_hhCmmGet(tt); // timestamp style
std::cout << "\n" << s
<< "\n s.size(): "
<< s.size() << " chars" << std::endl;
// now we know how many chars timestamp needs
// add 1 to size because ?strftime seems to need it?
char timestamp[s.size()+1];
(void)strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M", &ts);
// linux api: format time_t to string
std::cout << "\n" << timestamp << std::endl;
std::cout << " sizeof(timestamp): "
<< sizeof(timestamp) << " chars" << std::endl;
return(0);
}

Get time difference in milliseconds in c++

I'm trying to get the time difference between two time stamps. Thread is sleeping between the two time stamps. but when i get the difference, it doesn't give me the time which the thread was sleeping. My code is below.
#include <iostream>
#include <locale>
#include <sys/time.h>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int timeInMilli();
int main()
{
timeval t;
timeval t2;
gettimeofday(&t, NULL);
gettimeofday(&t2, NULL);
std::string buf(20, '\0');
std::strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
std::string hr = buf.substr(0, 2);
std::string min = buf.substr(3, 2);
std::string sec = buf.substr(6, 2);
/*std::cout << hr << '\n';
std::cout << min << '\n';
std::cout << std::atoi(sec.c_str()) << '\n';*/
int a = timeInMilli();
usleep(10);
int b = timeInMilli();
cout << b-a << endl;
}
int timeInMilli()
{
timeval t;
gettimeofday(&t, NULL);
string buf(20, '\0');
strftime(&buf[0], buf.size(), "%H:%M:%S:", localtime(&t.tv_sec));
string str_hr = buf.substr(0, 2);
string str_min = buf.substr(3, 2);
string str_sec = buf.substr(6, 2);
int hr = atoi(str_hr.c_str());
int min = atoi(str_min.c_str());
int sec = atoi(str_sec.c_str());
int milli = t.tv_usec/1000;
/*cout << hr << endl;
cout << min << endl;
cout << sec << endl;
cout << milli << endl;*/
int timeInMilli = (((hr * 60) + min) * 60 + sec) * 1000 + milli;
return timeInMilli;
}
usleep(10);
means that you pause for 10 µsec and not 10 msec since usleep works with microseconds. try with
usleep(10000);