If statement issue

Can someone explain to me why this if statement won't evaluate past the first conditional, the month() function?

if (month() == 3 && day() == (14 - x) && hour() >= 2)

It seems to ignore the day and the hour parts.
Using teensy 3.2

Thanks

I have no clue. I can't see the rest of your sketch.

Also, if month() is not equal to 3 then the rest of the conditional will be ignored.

The && operator is short-circuiting, which means that it will not evaluate its second operand if the first operand is false. What that means is that if month() is not 3, the rest of the expressions are not evaluated.

The issue is when the month is 3, the day and the hour and are not evaluated and the DSTchk var is set to 1 before the the correct date and time, ie. the date is before the first sunday.

#include <TimeLib.h>                    //Time lib
#include "sdios.h"                      //Std IO lib


void setup() {
  Serial.begin(9600);
  setSyncProvider(getTeensy3Time); 
}

void loop() {
  int i;
  i = Sunday_Offset();
  Serial.println(GetTimeNow());
  Serial.print("offset ");
  Serial.println(i);
  delay(500);
  Serial.print("dst ");
  Serial.println(dst());
  delay(500);

}
/* Functions */

//Sunday_Offset() **********************************************************
int Sunday_Offset() {
  int y = year() - 2000;
  int x = (y + y / 4 + 2) % 7;
  return x;
}
//..........................................................................

// DST() *******************************************************************
int dst(void) {
  int DSTchk;

  ////************ calc sunday offset 
  
  int x = Sunday_Offset();
  
  //// *********** Test DST: BEGINS on 2nd Sunday of March @ 2:00 AM *********
  if (month() == 3 && day() == (14 - x) && hour() >= 2)
  {
    DSTchk = 1;                                // Daylight Savings Time is TRUE (add one hour)
  }
//  if (month() == 3 && day() > (14 - x) || month() > 3)
//  {
//    DSTchk = 1;
//  }
  //// ************* Test DST: ENDS on 1st Sunday of Nov @ 2:00 AM ************
   if (month() == 11 && day() == (7 - x) && hour() >= 2)
       {
        DSTchk = 0;                                // daylight savings time is FALSE (Standard time)
       }
   if (month() == 11 && day() > (7 - x) || month() > 11 || month() < 3)
       {
        DSTchk = 0;
       }
   if (DSTchk == 1)                                 // Test DST and add one hour if = 1 (TRUE)
       {
        return 1;
       }
   else {
        return 0;
       }
}

//********** GetTimeNow **************************
// Gets current date & time from the System and returns it as a string "09/09/2020,11:09:00,"

String GetTimeNow() {
  String time_now;
  String Hour = hour();
  if (Hour.toInt() < 10) Hour = "0" + Hour;
  String Min = minute();
  if (Min.toInt() < 10) Min = "0" + Min;
  String Sec = second();
  if (Sec.toInt() < 10) Sec = "0" + Sec;
  String Mo = month();
  if (Mo.toInt() < 10) Mo = "0" + Mo;
  String Day = day();
  if (Day.toInt() < 10) Day = "0" + Day;
  String Yr = year();

  time_now = time_now + Mo + "/" + Day + "/" + Yr + "," + Hour + ":" + Min + ":" + Sec + ",";
  return time_now;
}

//********** getTeensy3time ********************************
time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

This is my test version for testing.

In your dst() function did you print month(), day(), and hour() to confirm? Most assuredly if statements work properly and you have a coding error, misunderstanding if what the library provides, or a hardware error.

So the code should work as written? The date and time are changing as I print the time to serial to watch for a change.

fluxanode1:
So the code should work as written? The date and time are changing as I print the time to serial to watch for a change.

I didn't say the code should work as written, I said if statements work so something else is wrong.

Here is a working DST module, native to AVR-GCC:

/*
 * (c)2012 Michael Duane Rice All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions
 * and the following disclaimer in the documentation and/or other materials
 * provided with the distribution. Neither the name of the copyright holders
 * nor the names of contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/* $Id: usa_dst.h 2344 2013-04-10 19:52:09Z swfltek $ */

/**
    Daylight Saving function for the USA. To utilize this function, you must
    \code #include <util/usa_dst.h> \endcode
    and
    \code set_dst(usa_dst); \endcode

    Given the time stamp and time zone parameters provided, the Daylight Saving function must
    return a value appropriate for the tm structures' tm_isdst element. That is...

    0 : If Daylight Saving is not in effect.

    -1 : If it cannot be determined if Daylight Saving is in effect.

    A positive integer : Represents the number of seconds a clock is advanced for Daylight Saving.
    This will typically be ONE_HOUR.

    Daylight Saving 'rules' are subject to frequent change. For production applications it is
    recommended to write your own DST function, which uses 'rules' obtained from, and modifiable by,
    the end user ( perhaps stored in EEPROM ).

*/

#ifndef NA_DST_H
#define NA_DST_H

#ifdef __cplusplus
extern          "C" {
#endif

#include <time.h>
#include <inttypes.h>

#ifndef DST_START_MONTH
#define DST_START_MONTH MARCH
#endif

#ifndef DST_END_MONTH
#define DST_END_MONTH NOVEMBER
#endif

#ifndef DST_START_WEEK
#define DST_START_WEEK 2
#endif

#ifndef DST_END_WEEK
#define DST_END_WEEK 1
#endif

    int             na_dst(const time_t * timer, int32_t * z) {
        time_t          t;
        struct tm       tmptr;
        uint8_t         month, week, hour, day_of_week, d;
        int             n;

        /* obtain the variables */
                        t = *timer + *z;
                        gmtime_r(&t, &tmptr);
                        month = tmptr.tm_mon;
                        day_of_week = tmptr.tm_wday;
                        week = week_of_month(&tmptr, 0);
                        hour = tmptr.tm_hour;

        if              ((month > DST_START_MONTH) && (month < DST_END_MONTH))
                            return ONE_HOUR;

        if              (month < DST_START_MONTH)
                            return 0;
        if              (month > DST_END_MONTH)
                            return 0;

        if              (month == DST_START_MONTH) {

            if (week < DST_START_WEEK)
                return 0;
            if (week > DST_START_WEEK)
                return ONE_HOUR;

            if (day_of_week > SUNDAY)
                return ONE_HOUR;
            if (hour >= 2)
                return ONE_HOUR;
            return 0;
        }
        if              (week > DST_END_WEEK)
                            return 0;
        if              (week < DST_END_WEEK)
                            return ONE_HOUR;
        if              (day_of_week > SUNDAY)
                            return 0;
        if              (hour >= 1)
                            return 0;
                        return ONE_HOUR;

    }

#ifdef __cplusplus
}
#endif

#endif

I took your code and stripped everything except your Sunday_Offset and dst functions. Added test stubs for the year, month, day, and hour functions.

With the following code dst() returns 1 as expected. If month, day, or hour is changed dst() returns 0 as expected.

This is an exercise in incremental testing.

const int test_year = 2021;
const int test_month = 3;
const int test_day = 14;
const int test_hour = 2;

int year() {return test_year;}
int month() {return test_month;}
int day() {return test_day;}
int hour() {return test_hour;}

void setup() {
  Serial.begin(9600);
}

void loop() {
  int i;
  i = Sunday_Offset();
  Serial.print("offset ");
  Serial.println(i);
  delay(500);
  Serial.print("dst ");
  Serial.println(dst());
  delay(500);

}
/* Functions */

//Sunday_Offset() **********************************************************
int Sunday_Offset() {
  int y = year() - 2000;
  int x = (y + y / 4 + 2) % 7;
  return x;
}
//..........................................................................

// DST() *******************************************************************
int dst(void) {
  int DSTchk;

  ////************ calc sunday offset

  int x = Sunday_Offset();

  //// *********** Test DST: BEGINS on 2nd Sunday of March @ 2:00 AM *********
  if (month() == 3 && day() == (14 - x) && hour() >= 2)
  {
    DSTchk = 1;                                // Daylight Savings Time is TRUE (add one hour)
  }
  //  if (month() == 3 && day() > (14 - x) || month() > 3)
  //  {
  //    DSTchk = 1;
  //  }
  //// ************* Test DST: ENDS on 1st Sunday of Nov @ 2:00 AM ************
  if (month() == 11 && day() == (7 - x) && hour() >= 2)
  {
    DSTchk = 0;                                // daylight savings time is FALSE (Standard time)
  }
  if (month() == 11 && day() > (7 - x) || month() > 11 || month() < 3)
  {
    DSTchk = 0;
  }
  if (DSTchk == 1)                                 // Test DST and add one hour if = 1 (TRUE)
  {
    return 1;
  }
  else {
    return 0;
  }
}

Thanks, I really appreciate the help!
This gives me something I can work with.

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