Finding out if given time falls into a time range.

I made it in the past but wasn't able to find a programmatic approach how to get close to solution;
I have a standart Uno board and using Ds3231 module in order to comlete the task.but that being said,I fell into real problem.How can i find out a specific time falls into time range.

10:00 am - 18:00pm --> This one comes easy if you have time 11:00,just check if it is bigger than 10:00 am and smaller than 18:00 pm.On the other hand,

18:00-4:00am this one really break my nerves! How can approach this one and determine ,say 3:00 am is within the range?

Thanks in adcance

Convert ALL times to seconds after midnight.

Paul

You could use epoch time, second since a given time, have a look at: https://forum.arduino.cc/index.php?topic=442631.0

for one example.

Paul_KD7HB:
Convert ALL times to seconds after midnight.

Paul

Thank you for quick reply Paul however your answer doesn't give me slightest clue.In fact,all I am focused on is that I need to represent time uniquely so that it will always return true for any given time range.

Sugarpasa:
18:00-4:00am this one really break my nerves! How can approach this one and determine ,say 3:00 am is within the range?

if your end time is greater than your start time for the range, just negate the check. Or put it into a function (all variables are assumed to be seconds from midnight.)

bool timeCheck( uint32_t now, uint32_t start, uint32_t end ) {
  bool inRange = false;
  if ( start <= now and now <= end ) inRange = true;
  if ( start > end ) inRange = !inRange;
  return inRange;
}

Sugarpasa:
Thank you for quick reply Paul however your answer doesn't give me slightest clue.In fact,all I am focused on is that I need to represent time uniquely so that it will always return true for any given time range.

Are you not able to compute the number of seconds in an hour times the hours in your time, plus the number of seconds in a minute time the minute value in your time plus the number of seconds in your time?
Do that for each time you are working with then compare the resulting values.
Paul

blh64:
if your end time is greater than your start time for the range, just negate the check. Or put it into a function (all variables are assumed to be seconds from midnight.)

bool timeCheck( uint32_t now, uint32_t start, uint32_t end ) {

bool inRange = false;
 if ( start <= now and now <= end ) inRange = true;
 if ( start > end ) inRange = !inRange;
 return inRange;
}

This piece of code What I originally came up with and then had problem with the case 22:00pm-3:00am range.I don’t use date information when comparing values.I have only hour and minute values in my hand.Thank you mate.

bool timeCheck( uint32_t now, uint32_t start, uint32_t end ) {
  bool inRange = false;
  if(start<end){
  if ( start <= now and now <= end ) inRange = true;
  return inRange;
  } else{
 if ( start >= now and now >= end ) inRange = true;
  return inRange;
  }
}

I am not much experienced programmer.I found such solution to my problem.I inspired from your piece of code Blh64.Thank you all guys.hugging you.ohh Totally forgot corana :smiley:

Which does exactly what my code does... You certainly do not need multiple return statements within your function, but hey, it's your code.

blh64:
Which does exactly what my code does… You certainly do not need multiple return statements within your function, but hey, it’s your code.

In fact one more if statement matters.If you look at it closely,you’ll see or expression performed.
I reliazed just now that I made a mistake in my previous post.All in all.I am in your debt.Stay safe

bool timeCheck( uint32_t now, uint32_t start, uint32_t end ) {
bool inRange = false;
if(start<end){
if ( start <= now and now <= end ) inRange = true;
return inRange;
} else if(start>end){
if ( start <= now or now <= end ) inRange = true;
return inRange;
}
}

That code doesn’t compile. And, it’s ugly.

bool timeCheck( uint32_t now, uint32_t start, uint32_t end ) {

  if (start > end) {
    uint32_t temp = start;
    start = end;
    end = temp;
  }

  if ( start <= now && now <= end ) {
    return true;
  } else {
    return false;
  }
}

It is much easier to use the modulo operator.

This function checks whether a given time, in minutes past midnight, is within a start and end time, both also in minutes past midnight. The range may span midnight.

// within_interval function determines whether the current time in minutes past midnight
// is within the timed interval start and end, also in minutes past midnight
// ***range may span midnight***

#define MIN_PER_DAY 1440
byte within_interval(int start, int end, int now) {
  int duration = (end - start + MIN_PER_DAY) % MIN_PER_DAY;
  int time_on = (now - start + MIN_PER_DAY) % MIN_PER_DAY;
  if (time_on < duration) return 1;  //within interval
  return 0;  //not within interval
}

It is trivial to extend the precision to seconds past midnight (86400 of them) but the variables then have to be declared long integers.

jremington:
It is much easier to use the modulo operator.

This function checks whether a given time, in minutes past midnight, is within a start and end time, both also in minutes past midnight. The range may span midnight.

// within_interval function determines whether the current time in minutes past midnight

// is within the timed interval start and end, also in minutes past midnight
// range may span midnight

#define MIN_PER_DAY 1440
byte within_interval(int start, int end, int now) {
 int duration = (end - start + MIN_PER_DAY) % MIN_PER_DAY;
 int time_on = (now - start + MIN_PER_DAY) % MIN_PER_DAY;
 if (time_on < duration) return 1;  //within interval
 return 0;  //not within interval
}




It is trivial to extend the precision to seconds past midnight (86400 of them) but the variables then have to be declared long integers.

Awesome.Thank you sharing