Converting Time_T to Int

Converting time_t to int

You should cast it to a long int instead of int.

long int t = static_cast<long int> (time(NULL));

An int might not be enough to hold the time, for example, on my platform, time_t is a typedef of __int64.

Convert time_t To Integer

If able, use sleep() to pause.

Use time() and localtime() to determine nap time.

#include <unistd.h>
#include <time.h>
#include <stdio.h>

int main() {
while (1) {
time_t Now;
if (time(&Now) == -1) {
return -1;
}
struct tm tm;
tm = *localtime(&Now);
tm.tm_mday++;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
time_t Then;
Then = mktime(&tm);
if (Then == -1) {
return -1;
}
if (Then <= Now) {
return -1; // Time moving backwards - Hmmmm?
}
unsigned int NapTime = (unsigned int) (Then - Now);
printf("Going to sleep %u\n", NapTime );
fflush(stdout); // Let's empty the buffers before nap time
if (0 != sleep(NapTime)) {
return -1; // trouble sleeping
}
// Done sleeping!
printf("Midnight\n");
fflush(stdout);
sleep(10);
}
}

How to correctly cast time_t to long int?

time(NULL) is not a cast but a function call which returns time_t. Since time_t is not exactly the same type as long int, you see the warning.

Furthermore, static_cast<T>(value) requires the parenthesis, that is why your first version does not work.

Convert long int to const time_t

In general, you can't as there need not be any reasonable connection between std::time_t and an integer like long.

On your specific system, std::time_t is a long long, so you can just do

std::time_t temp = tmit;

and then use temp's address. Note that this need not be portable across compilers or compiler versions (though I would not expect the latter to break).

It is worth checking whether whatever is saved in tmit gets interpreted by functions like ctime in a sensible way, as you did not tell us where that came from.

Depending on how this tmit is produced, it might also be a good idea to use an std::time_t tmit instead of long tmit from the get go and thus eliminate this conversion question entirely.

If you don't have to use the old C-style time facilities, check out the C++11 <chrono> header.

Converting time_t to double and back

... convert time_t value to double and then back ...

In what cases I might lose data in converting?

In some cases, nothing. time_t is some real type. C11 §7.27.1 3

Integer and real floating types are collectively called real types. C11 §6.2.5 17

So if time_t was the same as a double, no loss is expected in conversion.

time_t is usually represented as 32 or 64 bit integer. Typical double can encode all 53-bit unsigned values (or 54 bit signed integer values) exactly and so when time_t is a 32-bit integer, no conversion loss should occur. When time_t is a 64-bit integer, conversion to double begin to incurring rounding with values outside 253 in magnitude.

The 2nd conversion back step should never occur any loss if the double value was from an original time_t, but can incur undefined behavior if an arbitrary double was attempted to be changed to an integer.

Less common, time_t could be a float, long double, long long, etc. and similar issues apply.

Converting time_t to int64_t

There are 3 cases where you have to worry about casting from one integer type to another:

  1. You cast from a large integer to a small one. I'm reasonably confident that sizeof(int64_t) >= sizeof(time_t) so that shouldn't be a problem.
  2. You cast an unsigned type to a signed type, and value is outside the range of the signed type. Again unlikely since 1349388030 is less than 2^31.
  3. You cast a signed type to an unsigned type, and the value is negative. Doesn't apply here.

I don't think the problem is in your casting.

How do I set a C++ time_t object to the epoch?

What is wrong with using 0? (time_t)0 represents the epoch itself (if you want to find the actual epoch date/time, pass (time_t)0 to gmtime() or localtime()).

time_t myfunc(...., time_t defVal = 0 );

Or, you could use (time_t)-1 instead, which is not a valid time, as time() returns (time_t)-1 on error, and time_t represents a positive number of seconds since the epoch.

time_t myfunc(...., time_t defVal = (time_t)-1 );

Either way provides the user with something that is easily compared, if they don't provide their own default value.



Related Topics



Leave a reply



Submit