Time library with NANO 33 BLE

Hello,

I am trying to use the time library which I used before with Arduino Uno and MEGA, but it does not compile on the NANO 33 BLE sense, even with an empty code like this:

#include <TimeLib.h> //this is the version 1.6.0 

void setup() {
}

void loop() {
}

This is the error I get:

Arduino: 1.8.12 (Mac OS X), Board: "Arduino Nano 33 BLE"

In file included from /Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/String.h:33:0,
from /Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/Print.h:24,
from /Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/Stream.h:25,
from /Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/Client.h:22,
from /Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/ArduinoAPI.h:29,
from /Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/Arduino.h:42,
from /Users/user/Documents/Arduino/libraries/Time-master/DateStrings.cpp:12:
/Users/user/Documents/Arduino/libraries/Time-master/DateStrings.cpp: In function 'char* monthStr(uint8_t)':
/Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h:106:49: error: 'const void*' is not a pointer-to-object type
#define pgm_read_ptr(addr) ((const void )(addr))
^
/Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h:63:45: note: in definition of macro 'strcpy_P'
#define strcpy_P(dest, src) strcpy((dest), (src))
^~~
/Users/user/Documents/Arduino/libraries/Time-master/DateStrings.cpp:72:29: note: in expansion of macro 'pgm_read_ptr'
strcpy_P(buffer, (PGM_P)pgm_read_ptr(&(monthNames_P[month])));
^~~~~~~~~~~~
/Users/user/Documents/Arduino/libraries/Time-master/DateStrings.cpp: In function 'char dayStr(uint8_t)':
/Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h:106:49: error: 'const void' is not a pointer-to-object type
#define pgm_read_ptr(addr) (*(const void *)(addr))
^
/Users/user/Library/Arduino15/packages/arduino/hardware/mbed/1.1.3/cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h:63:45: note: in definition of macro 'strcpy_P'
#define strcpy_P(dest, src) strcpy((dest), (src))
^~~
/Users/user/Documents/Arduino/libraries/Time-master/DateStrings.cpp:86:28: note: in expansion of macro 'pgm_read_ptr'
strcpy_P(buffer, (PGM_P)pgm_read_ptr(&(dayNames_P[day])));
^~~~~~~~~~~~
exit status 1
Error compiling for board Arduino Nano 33 BLE.

Does anybody know how to solve it?

Thank you a lot in advance,
best

Lorenzo

The Arduino Nano 33 BLE uses mbedOS running on an ARM Cortex-M processor. Your library is likely not compatible. What kind of functions do you want to use?

You could have a look at the mbed documentation and find functions that will provide the same functionality.

Thank you for your reply,

I just need to adjust the time that I get from a GPS to my current time zone... nothing more than this.

I have created and tested an example that should get you started. I looked at the mbed documentation and searched the cpp reference for time_t.

mbedOS 5 time

https://en.cppreference.com/w/cpp/chrono/c/time_t

There are two versions in the example for setting the time, depending how you get the time from your gps module. Choose which one works best for you.
If you search a little bit and experiment, you can probably figure out how to set a time zone difference for the local time function. If you need some more help, please let me know.

void setup()
{
  // put your setup code here, to run once:
  Serial.begin( 9600 );
  while ( !Serial );

  //  set_time( 1585537264 );     // Unix time seconds since 1970, 1 January 00:00:00

  struct tm setTime;

  setTime.tm_mday = 30;           // day of month (0 - 31)
  setTime.tm_mon = 3 - 1;         // month are from (0 - 11)
  setTime.tm_year = 2020 - 1900;  // years since 1900
  setTime.tm_hour = 9;            // hour (0 - 23)
  setTime.tm_min = 45;            // minutes (0 - 59)
  setTime.tm_sec = 0;             // seconds (0 -   59)


  set_time( mktime( &setTime ) );

}

void loop()
{
  // put your main code here, to run repeatedly:
  time_t seconds = time( NULL );

  Serial.print( "Time: " );
  Serial.println( seconds );

  Serial.print( "UTC: " );
  Serial.print( asctime( gmtime( &seconds ) ) );

  Serial.print( "local: " );
  Serial.print( asctime( localtime( &seconds ) ) );

  delay( 1000 );
}
1 Like

Thank you very much!
I will try your code. Meantime, I also made a simple code:

void myTime()
{

int offset = 0;

  int Hour = gps.time.hour(); //from GPS using TinyGPS++ library
  int Minute = gps.time.minute(); //from GPS using TinyGPS++ library
  int Second = gps.time.second(); //from GPS using TinyGPS++ library
  int Day = gps.date.day(); //from GPS using TinyGPS++ library
  int Month = gps.date.month(); //from GPS using TinyGPS++ library
  int Year = gps.date.year(); //from GPS using TinyGPS++ library
  
  Hour = Hour + offset; 




    if (Hour > 24 || Hour == 24) {
      Hour = Hour - 24;
      Day = Day + 1;
      
      if ((Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) && Day > 31 ) {
      Day = 1;
      Month = Month + 1;
      }
    
      if ((Month == 4 || Month == 6 || Month == 9 || Month == 11) && Day > 30 ) {
      Day = 1;
      Month = Month + 1;
      }
    
      if ((Month == 2) && Day > 28 ) {
      Day = 1;
      Month = Month + 1;
      }
    
      if (Month > 12) {
      Month = 1;
      Year = Year + 1;
      }
    
    }


    if (Hour < 0) {
      Hour = 24 + Hour;
      Day = Day - 1;
      
      if (Day <= 0) {
      Month = Month - 1;
      
      if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) {
      Day = 31;
      }
    
      if (Month == 4 || Month == 6 || Month == 9 || Month == 7) {
      Day = 30;
      }
    
      if (Month == 2) {
      Day = 28;
      }
      }
    
      if (Month <= 0) {
      Day = 31;
      Month = 12;
      Year = Year - 1;
      }
    }
  
}

While it is fun to write some code to do your own time routines for learning, I would recommend to use libraries for that for real applications. Date and Time are more difficult because of the exception cases e.g. February 29th. Sometimes additional seconds are added to the world time. Using a library gives you a better chance of someone else really looking this.

For time calculation I think the best is to use UNIX time. Its a single value and you can add/subtract any amount of seconds and therefore hours, days or even years to it. When you are done you convert the time to the individual components for human reading. So, no need to handle overflows between hours, days, month and so on.