Using time_t filttering

54 Views Asked by At

I'd like to understand better time_t since i've never dealt with it before.

I want to make a filter_by_time function that gets a time window (lets say, 24 hours) and sends me back a vector of objects with a lower time gap of the said time window.

So since the function is getting a integer (from what i googled i understand i might need long-int) how can i convert the current time and the time_t field of an object into a long int ?

I was thinking to check if the current time (now) minus object's time_t < 24 hours then i'd put object into the array

hope i could be clear enough with my ambitions

1

There are 1 best solutions below

0
UniversE On

Actually the time_t is almost always an integer counting the seconds since 00:00, Jan 1 1970 UTC (POSIX time).

However, this is not defined by the standard and might not be 100% reliable everywhere.

You can try to use from_time_t to convert your time_t to a time_point and use the constructors of time_point and duration to construct another time point from your integer. Then you can compare those time points.

This would be the most reliable way of solving the problem without assuming any particular format for time time_t.

But if it is acceptable for you to assume that the time_t is always POSIX time, you might also be able to just substract the integers as you have already suggested.