Avoiding the possible data loss error

 

What is the best way to avoid the "possible loss of data..." error from this line?

 

int secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent(); 

 
daileycon: What is the best way to avoid the "possible loss of data..." error from this line?

int secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent(); 
Typecasting!
// Either in the "function" style
int secs_remaining = int( Time[0] - TimeCurrent() ) + PeriodSeconds();
// or in the "C" style
int secs_remaining = (int)( Time[0] - TimeCurrent() ) + PeriodSeconds();
 
daileycon:

What is the best way to avoid the "possible loss of data..." error from this line?

 

int secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent(); 

long secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent();
 
Fernando Carreiro:
Typecasting!
// Either in the "function" style
int secs_remaining = ( int( Time[0] ) + PeriodSeconds() ) - int( TimeCurrent() );
// or in the "C" style
int secs_remaining = ( (int) Time[0] + PeriodSeconds() ) - (int) TimeCurrent();
Sorry, we posted at the same time. Different ways to skin the proverbial cat!
 
honest_knave: Sorry, we posted at the same time. Different ways to skin the proverbial cat!
Updated my original post to be a little more efficient!

EDIT: I have to wonder however, if one can have a negative datetime variable, namely is "Time[0] - TimeCurrent()" valid?

EDIT2: Maybe my original post is more correct than my updated "efficient" version:
// Either in the "function" style
int secs_remaining = ( int( Time[0] ) + PeriodSeconds() ) - int( TimeCurrent() );
// or in the "C" style
int secs_remaining = ( (int) Time[0] + PeriodSeconds() ) - (int) TimeCurrent();

 
Fernando Carreiro:

EDIT: I have to wonder however, if one can have a negative datetime variable, namely is "Time[0] - TimeCurrent()" valid?

Yes, a datetime variable can store a negative number (to all intents and purposes, datetime is a long) but if you try to print a negative datetime it will show "wrong datetime"

 
honest_knave: Yes, a datetime variable can store a negative number (to all intents and purposes, datetime is a long) but if you try to print a negative datetime it will show "wrong datetime"
Yes, but converting a negative "datetime" into an "int" my cause a loss. I've never actually tested that myself!

EDIT: Well, maybe not, because in this case it is just a time difference which perfectly fits an "int".
 
Fernando Carreiro:
Yes, but converting a negative "datetime" into an "int" my cause a loss. I've never actually tested that myself!

In theory, yes. Both ways, positive or negative. Around 68 years / Year 2038 will max out the int.

I just take the hit on the 4 bytes of extra memory and store a long.

 
honest_knave: In theory, yes. Both ways, positive or negative. Around 68 years / Year 2038 will max out the int. I just take the hit on the 4 bytes of extra memory and store a long.
Hopefully, this "extra" chatter will not confuse the OP!
 
Fernando Carreiro:
Hopefully, this "extra" chatter will not confuse the OP!

Meh, posts 2 and 3 will set them on the right path so they don't have to read very far.

The rest is relevant discussion for those who are interested (probably just you and me!) 

 
honest_knave: The rest is relevant discussion for those who are interested (probably just you and me!) 
This is why I typedef it and make the code self-documenting
#define SECONDS uint
#define HR2400 86400
SECONDS    time(datetime when=0){
      return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400;               }
datetime   date(datetime when=0){
      return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );      }
Reason: