PaulStoffregen / Time

Time library for Arduino
http://playground.arduino.cc/code/time
1.24k stars 664 forks source link

Macros for converting elapsed time to a time_t #154

Open zabojpetr opened 3 years ago

zabojpetr commented 3 years ago

Description

Macros for converting elapsed time to a time_t ends with error: 'parameter' was not declared in this scope

Steps To Reproduce Problem

Call any of Macros for converting elapsed time to a time_t

Hardware & Software

Board: ESP32 Arduino IDE version: 1.8.13 Version info & package name (from Tools > Boards > Board Manager): esp32 1.0.4 Operating system & version Windows 10 (20H2)

Arduino Sketch

// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)
#include <TimeLib.h>

void setup() {
  // put your setup code here, to run once:
  time_t oneDay = daysToTime_t(1);
}

void loop() {
  // put your main code here, to run repeatedly:
}

Errors or Incorrect Output

~/sketch_jan10a/sketch_jan10a.ino: In function 'void setup()':
~/libraries/Time/TimeLib.h:94:27: error: 'D' was not declared in this scope
 #define daysToTime_t    ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
                           ^
~/sketch_jan10a/sketch_jan10a.ino:5:19: note: in expansion of macro 'daysToTime_t'
   time_t oneDay = daysToTime_t(1);
                   ^
exit status 1

Fix

I am quite new in C/C++ so maybe my fix has some problems, but for me it works.

Original

#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN)  
#define hoursToTime_t   ((H)) ( (H) * SECS_PER_HOUR)  
#define daysToTime_t    ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
#define weeksToTime_t   ((W)) ( (W) * SECS_PER_WEEK)   

Fixed

#define minutesToTime_t(M) ( (M) * SECS_PER_MIN)  
#define hoursToTime_t(H) ( (H) * SECS_PER_HOUR)  
#define daysToTime_t(D) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011
#define weeksToTime_t(W) ( (W) * SECS_PER_WEEK)   
jpasqua commented 2 years ago

I am experiencing the same issue.

Config: Arduino IDE: 1.8.13 Boards: Uno, ESP8266, ESP32 Compiler __VERSION__ for ESP8266: 4.8.2

Sketch:

#include <TimeLib.h>
void setup() {
  uint32_t theTime = minutesToTime_t(5);
}
void loop() { }

Errors:

In file included from Dev/Arduino/sketch_sep21a/sketch_sep21a.ino:1:0:
Dev/Arduino/sketch_sep21a/sketch_sep21a.ino: In function 'void setup()':
Dev/Arduino/libraries/Time/TimeLib.h:92:27: error: 'M' was not declared in this scope
 #define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN)
                           ^
Dev/Arduino/sketch_sep21a/sketch_sep21a.ino:4:22: note: in expansion of macro 'minutesToTime_t'
   uint32_t theTime = minutesToTime_t(5);

Tests: Removing the space before the parameter list and the extra set of parens resolves the issue for me: #define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) becomes: #define minutesToTime_t(M) ( (M) * SECS_PER_MIN)

Additional Info: Compiling the following program on a Mac with Apple clang version 13.0.0 (clang-1300.0.29.3) results in the same errors:

#define mFix(M) ( (M) * 60)  
#define mOrig ((M)) ( (M) * 60)  

int main(int argc, char** argv) {
    int m1 = mFix(5);
    int m2 = mOrig(5);
    return 0;
}

Compiling results in:

testmacro.cpp:6:11: error: use of undeclared identifier 'M'
        int m2 = mOrig(5);
                 ^
testmacro.cpp:2:17: note: expanded from macro 'mOrig'
#define mOrig ((M)) ( (M) * 60)  
                ^
testmacro.cpp:6:11: error: use of undeclared identifier 'M'
testmacro.cpp:2:24: note: expanded from macro 'mOrig'
#define mOrig ((M)) ( (M) * 60)