0

I have searched all over Google without success for a way to convert my list of year,month,day and time to unix time. I need a simple way to find out if one time is smaller or bigger than another day. I have found this solution. I have this suggestion and want to know if there are any smarter or better way. It seems to work. I need the decimal seconds.

    using namespace System;

int MyYear = 2017;
int MyMonth = 5; 
int MyDay = 12;
int MyHour = 8; 
int MyMinute = 33;
double MySecond =47.2332;

double whole, fractional;
fractional = std::modf(MySecond, &whole);

System::DateTimeOffset dto(MyYear, MyMonth, MyDay,
    MyHour, MyMinute,static_cast<int>(whole),System::TimeSpan::Zero);

std::cout << "dto: Unix Seconds: " << dto.ToUnixTimeSeconds() << endl;
double MyUnixTime = static_cast<int>(dto.ToUnixTimeSeconds()) + fractional;
cout.precision(20);
std::cout << "MyUnixTime: " << MyUnixTime  << endl;
User1953
  • 171
  • 1
  • 7
  • How many days are in a year? How many hours are in a day? How many minutes are in an hour how many minutes are in an hour. How many seconds are in a minute? You should be able to calculate the time since 1970 pretty easily (a small complication will be leap years and leap seconds).. but, it's simple arithmetic. – Jesper Juhl Dec 25 '19 at 18:20
  • 1
    The typical Unix timestamp doesn't handle fractions of a second though so you're probably better off with [the `chrono` library](https://en.cppreference.com/w/cpp/chrono) and possibly extensions from [Howard Hinnant's Date library](https://github.com/HowardHinnant/date) (coming soon to a C++ Standard near you!) ([example](https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes#time_point_to_components)) – user4581301 Dec 25 '19 at 18:23
  • https://en.cppreference.com/w/cpp/chrono/time_point/time_since_epoch ? – Andrey Semenov Dec 25 '19 at 20:35

0 Answers0