-1

I've a a problem with time numbers,

Lets supose that i've this time: 05-03-2016 09:45:55.064371, I've a function that converts this to miliseconds (Using Epoch (reference_date), using ctime and chrono libraries)->1457167555064, now what I want to find is the full minute with miliseconds before and after this time, so in this case what I want is to find 05-03-2016 09:45:00.000000 and 05-03-2016 09:46:00.000000

I'm open to lisent another way to find if a date is inside a minute.

Thank you!

david.t_92
  • 1,971
  • 1
  • 11
  • 15
  • what is the problem? Just round the number of milliseconds to the next multiple of 60000, this the next minute, then subtract 60000, this is the minute before – 463035818_is_not_an_ai Oct 27 '17 at 10:18
  • @tobi303: might be a little more complicated if [Leap_second](https://en.wikipedia.org/wiki/Leap_second) should be taken into account. – Jarod42 Oct 27 '17 at 10:22
  • @Jarod42 hm yes thats a problem when the date is given in number of milliseconds, but if the time is known as `hh::mm::ss::....` I dont see the problem (not claiming there is none, but imho question needs clarification) – 463035818_is_not_an_ai Oct 27 '17 at 10:27
  • What format is the time to start with? What is its C++ type? I think showing some code would clarify the problem. You could do as tobi305 suggested or maybe just discard the seconds and milliseconds in the original time to get the time before (or equal to) and then add one minute to get the time after. – snow_abstraction Oct 27 '17 at 15:32

1 Answers1

4

The most convenient tool for this is the new std::chrono::floor and std::chrono::ceil in C++17. If you don't have C++17, you can get a preview of these in Howard Hinnant's free, open-source datetime library:

#include "date/date.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    sys_time<milliseconds> tp{1457167555064ms};
    sys_time<milliseconds> t0 = floor<minutes>(tp);
    sys_time<milliseconds> t1 = ceil<minutes>(tp);
    std::cout << t0 << '\n';
    std::cout << tp << '\n';
    std::cout << t1 << '\n';
}

Output:

2016-03-05 08:45:00.000
2016-03-05 08:45:55.064
2016-03-05 08:46:00.000

These times are all UTC. You appear to have a local time at UTC +01:00. There is also a timezone library at this same GitHub site that you can use to convert between UTC and local time, or between any two IANA timezones.

Above, sys_time<milliseconds> is simply a type alias for time_point<system_clock, milliseconds>.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577