Skip to content

Instantly share code, notes, and snippets.

@MikaelSmith
Created August 26, 2016 17:50
Show Gist options
  • Save MikaelSmith/a0daa7656d39f328a166e1fcc4090287 to your computer and use it in GitHub Desktop.
Save MikaelSmith/a0daa7656d39f328a166e1fcc4090287 to your computer and use it in GitHub Desktop.
Boost date_time posix_time breaking
#include <limits>
#include <iostream>
#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
int main(int argc, char** argv)
{
for (int i = 0; i < 63; ++i) {
int64_t integer = 1ll << i;
{
auto time = nanoseconds(integer);
std::cout << "Test 2^" << i << std::endl;
std::cout << "As nanoseconds" << std::endl;
std::cout << integer << " == " << time.total_nanoseconds() << std::endl;
std::cout << (integer / 1000) << " == " << time.total_microseconds() << std::endl;
std::cout << (integer / 1000000) << " == " << time.total_milliseconds() << std::endl;
std::cout << (integer / 1000000000) << " == " << time.total_seconds() << std::endl;
std::cout << (integer / 60000000000) << " == " << time.total_seconds() / 60 << std::endl;
std::cout << (integer / 3600000000000) << " == " << time.total_seconds() / 3600 << std::endl;
}
{
auto time = microseconds(integer);
std::cout << "As microseconds" << std::endl;
std::cout << (integer * 1000) << " == " << time.total_nanoseconds() << std::endl;
std::cout << integer << " == " << time.total_microseconds() << std::endl;
std::cout << (integer / 1000) << " == " << time.total_milliseconds() << std::endl;
std::cout << (integer / 1000000) << " == " << time.total_seconds() << std::endl;
std::cout << (integer / 60000000) << " == " << time.total_seconds() / 60 << std::endl;
std::cout << (integer / 3600000000) << " == " << time.total_seconds() / 3600 << std::endl;
}
{
auto time = milliseconds(integer);
std::cout << "As milliseconds" << std::endl;
std::cout << (integer * 1000000) << " == " << time.total_nanoseconds() << std::endl;
std::cout << (integer * 1000) << " == " << time.total_microseconds() << std::endl;
std::cout << (integer) << " == " << time.total_milliseconds() << std::endl;
std::cout << (integer / 1000) << " == " << time.total_seconds() << std::endl;
std::cout << (integer / 60000) << " == " << time.total_seconds() / 60 << std::endl;
std::cout << (integer / 3600000) << " == " << time.total_seconds() / 3600 << std::endl;
}
{
auto time = seconds(integer);
std::cout << "As seconds" << std::endl;
std::cout << (integer * 1000000000) << " == " << time.total_nanoseconds() << std::endl;
std::cout << (integer * 1000000) << " == " << time.total_microseconds() << std::endl;
std::cout << (integer * 1000) << " == " << time.total_milliseconds() << std::endl;
std::cout << (integer) << " == " << time.total_seconds() << std::endl;
std::cout << (integer / 60) << " == " << time.total_seconds() / 60 << std::endl;
std::cout << (integer / 3600) << " == " << time.total_seconds() / 3600 << std::endl;
}
{
auto time = minutes(integer);
std::cout << "As minutes" << std::endl;
std::cout << (integer * 60000000000) << " == " << time.total_nanoseconds() << std::endl;
std::cout << (integer * 60000000) << " == " << time.total_microseconds() << std::endl;
std::cout << (integer * 60000) << " == " << time.total_milliseconds() << std::endl;
std::cout << (integer * 60) << " == " << time.total_seconds() << std::endl;
std::cout << (integer) << " == " << time.total_seconds() / 60 << std::endl;
std::cout << (integer / 60) << " == " << time.total_seconds() / 3600 << std::endl;
}
std::cout << std::endl;
}
return 0;
}
@MikaelSmith
Copy link
Author

MikaelSmith commented Aug 26, 2016

The boost representation seems to start breaking down around 2^26 minutes:

Test 2^26
As nanoseconds
67108864 == 67108864
67108 == 67108
67 == 67
0 == 0
0 == 0
0 == 0
As microseconds
67108864000 == 67108864000
67108864 == 67108864
67108 == 67108
67 == 67
1 == 1
0 == 0
As milliseconds
67108864000000 == 67108864000000
67108864000 == 67108864000
67108864 == 67108864
67108 == 67108
1118 == 1118
18 == 18
As seconds
67108864000000000 == 67108864000000000
67108864000000 == 67108864000000
67108864000 == 67108864000
67108864 == 67108864
1118481 == 1118481
18641 == 18641
As minutes
4026531840000000000 == 4026531840000000000
4026531840000000 == 4026531840000000
4026531840000 == 4026531840000
4026531840 == -268435456
67108864 == -4473924
1118481 == -74565

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment