How to compare hours?

i would like to control hours and minutes but not very sure on how to do this ...

i wrote this :

// si l' heure est plus grande que 1 heure et 30 minutes et que l' heure est plus petite que 7 heures et 30 minutes : on active le relais .
		if (( RTC.heure() >= 1 && RTC.minute() > 30) && (RTC.heure() <= 7 && RTC.minute() < 29 )) {
			RHC = 1;
		}
		else {
			RHC = 0;
		}

But what happen for example if this is 2 hours and 00 minutes ?
RHC will be equal to 0 or 1 ?

Can you not work out the answer to your own question? Have you tested it?

Your logical expression contains 4 terms, and all 4 are combined with &&. The braces make no difference in this expression. For the whole expression to be true, all 4 terms must be true.

it would give 0
if you are not sure what the result of a statement would be write a simple program to test it, e.g.

int main(void)
{
  int RHC, heure = 2, minute = 0;
  if (( heure >= 1 && minute > 30) && (heure <= 7 && minute < 29 )) {
    RHC = 1;
  }
  else {
    RHC = 0;
  }
  printf("%d ", RHC);
}

try converting the time to minutes and then test, e.g.

        if (( heure*60+minute > 60+30) && (heure*60+minute < 7*60+29 )) {

resut 1

Anyone know if the Arduino IDE compiler replaces 7*60+29 with 449, while compiling? I know modern compilers do that. Just to ease up code writing and actually to make code more readable, too.

Isn't it far easier to derive a timestamp (minutes since start of day) and compare to that?

int timestamp = hrs*60 + min;

if (timestamp> 1*60 && timestamp<1*60+30) {
    Do something;
}

Read on ZEN of python...
Simple is better than complex
If the implementation is hard to explain, it's a bad idea.

if testing multiple times implement a function, e.g.

// return true if time is between limits hour1:minute1 and hour2:minute2
int timeTest(int hour, int minute, int hour1, int minute1, int hour2, int minute2) {
    int timestamp = hour*60 + minute;
    return ( timestamp > hour1*60+30 && timestamp < hour2*60+minute2 );
}

int main(void)
{
    int RHC, heure=2, minute=0;
    printf("%d ", timeTest(heure, minute, 1, 30, 7, 29));
    printf("%d ", timeTest(8, 0, 1, 30, 7, 29));
    printf("%d ", timeTest(0, 0, 1, 30, 7, 29));
}

a run gives

1 0 0

what happens if one is comparing times on different days?

using MicroPython on an ESP32 executing

import time
time1 = time.mktime((2022, 10, 24, 2, 0, 0, 1, 256, 0))
print('time1 ' + str(time.localtime(time1)))
time2 = time.mktime((2022, 10, 24, 1, 30, 0, 1, 256, 0))
print('time2 ' + str(time.localtime(time2)))
time3 = time.mktime((2022, 10, 24, 7, 29, 0, 1, 256, 0))
print('time3 ' + str(time.localtime(time3)))
print('time1 > time2 and time1 < time3 ' + str(time1 > time2 and time1 < time3))

time1 = time.mktime((2022, 10, 24, 0, 0, 0, 1, 256, 0))
print('time1 ' + str(time.localtime(time1)))
print('time1 > time2 and time1 < time3 ' + str(time1 > time2 and time1 < time3))

gives

time1 (2022, 10, 24, 2, 0, 0, 0, 297)
time2 (2022, 10, 24, 1, 30, 0, 0, 297)
time3 (2022, 10, 24, 7, 29, 0, 0, 297)
time1 > time2 and time1 < time3 True
time1 (2022, 10, 24, 0, 0, 0, 0, 297)
time1 > time2 and time1 < time3 False
>>>

Hello all , and thanks for thex explanation .

i need to calculate 4 time in one day , so i think i will use a function .

i know start and stop time ( 2 times in a day ) .
start time by night = HC_START_TIME_N
start time by day = HC_START_TIME_D
end time by night = HC_END_TIME_N
end time by day = HC_END_TIME_D

and now implement a function calculate if RHC = 1 or 0

if   ( HC_START_TIME_D < timestamp < HC_END_TIME_D ) or ( HC_START_TIME_N < timestamp < HC_END_TIME_N )
RHC = 1;
else RHC =0 ;

is this work :

int timeTest() {
	int timestamp = RTC.hour()*60 + RTC.minute();
	return ( timestamp > HC_START_TIME_N && timestamp < HC_END_TIME_N OR  timestamp > HC_START_TIME_D && timestamp < HC_END_TIME_D)
}

i can ' t do real test on arduino board , it is actually in use ...

ok , i change the function for :

int timeTest() {
	unsigned int timestamp = RTC.heure()*60 + RTC.minute();
	bool boool=0;
	if (timestamp > HC_START_TIME_N && timestamp < HC_END_TIME_N || timestamp > HC_START_TIME_D && timestamp < HC_END_TIME_D)
		boool=1;
	 return boool;
}

before declare constant :

const unsigned int 
HC_START_TIME_N = 1*60 + 30, // 1 H 30 min
HC_END_TIME_N = 7*60 +30, // 7 H 30 min
HC_START_TIME_D = 12*60, // 12 h 00 min
HC_END_TIME_D = 14*60; // 14 h 00 min

and finally use it in another function :

void relaisHeureCreuse () {
	static unsigned long chrono = 0;
	static int RHC = -1; // jamais lu
	static bool oldRHC = false;
// heures creuses de 1 h 30 a 7 H 30 et de 12 a 14 Heures
	if ( (RHC == -1) || (millis() - chrono >= TIMER_V) ) { // si jamais lue ou lue depuis plus de 30 secondes

		
		if (timeTest()) { // si l' heure est plus grande que 1 heure et 30 minutes et que l' heure est plus petite que 7 heures et 30 minutes : on active le relais .
			RHC = 1;
			if (DEBUG) Serial.println (F("relaisHeureCreuse => etat actif"));
		}
		else {
			RHC = 0;
			if (DEBUG) Serial.println (F("relaisHeureCreuse => etat passif"));
		}

		if ((oldRHC != RHC) && (RHC != -1)) {
			tableau_Etat_Relais[RHC_PIN_RELAY] = !(tableau_Etat_Relais[RHC_PIN_RELAY]);
			digitalWrite(tableau_Pin_Relais[RHC_PIN_RELAY], tableau_Etat_Relais[RHC_PIN_RELAY]);
			oldRHC = RHC;
			if (DEBUG) Serial.println (F("relaisHeureCreuse => changement etat"));
		}
		chrono = millis();
	}
}

If you want to do it this way, test the full hours and then test the minutes only for their specific hours:

if (( RTC.heure() > 1 && RTC.heure() < 7) ||   // Full hours
     (RTC.heure() == 1 && RTC.minute() > 30) ||
     (RTC.heure() == 7 && RTC.minute() < 29 )) {

If you have a DS3231, it could be as simple as that...
// clock.setAlarm1(0, 1, 10, 30, DS3231_MATCH_H_M_S);

hi , this is working with the simplertc librairie ? #include "simpleRTC.h"

Hi , i would like the relay is ON at 1h 30 and OFF at 7 H 29 .
and after , ON at 12 h 00 , and OFF at 14 H 00 .

https://github.com/NorthernWidget/DS3231

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.