error accessing struct tm data from avr.libc time

Having trouble to acces data from <time.h> tm struct. What is wrong, what do I miss in this simple code fragment?

I use: Arduino 1.6.9 board Arduino Nano ATMega 328 prozessor

#include <time.h>

void setup() {

Serial.begin(115200);
Serial.println(" ");
set_system_time(3681359818 - NTP_OFFSET); // >>> sample from NTP request just for testing
set_zone(1 * ONE_HOUR);
set_position (47.737591, 9.16148185);

Serial.print("sun rise: ");
time_t sunRise = sun_rise(1);
Serial.println(sunRise); // result is: 1397800573

Serial.print("year: ");

Serial.println(tm.tm_year);

ERROR: time_h_usage_V1:21: error: expected primary-expression before '.' token

Maybe I didn't understand right handling of struct

Thank you for your patience

Hit Quote on this post (lower right corner) to see what the code tags in the read me before posting here look like.

#include <time.h>                                        

void setup() {

  Serial.begin(115200);
  Serial.println(" ");
  set_system_time(3681359818 - NTP_OFFSET);  // >>> sample from NTP request just for testing
  set_zone(1 * ONE_HOUR);                           
  set_position (47.737591, 9.16148185);         
  
  Serial.print("sun rise: ");
  time_t sunRise = sun_rise(1);
  Serial.println(sunRise);                                    // result is: 1397800573

 
  Serial.print("year: ");
  
  Serial.println(tm.tm_year);

If you want to use a tm struct then you have to declare one, same as any other variable.

int x;
tm myTime;

Then you have to fill it and then you can use it in some time.h functions or do this:

Serial.println( myTime.tm_year); // myTime is the variable and tm is the variable type (a struct)

It's a big do-everything standard C library that you can find more about through a net-search.
http://www.nongnu.org/avr-libc/user-manual/group__avr__time.html#ga26c7d1dbf93fa8c23c5effbacec91f8c

Hi GoForSmoke,
thank you for fast reply. However I'm still missing something. Pls have a look at my modified code

#include <time.h> // die lib einbinden

tm myYear;

void setup() {

...

Serial.println(myYear.tm_year);

No compiler errors anymore - great! However the printed result is 0 and not as expected.

Thank you for helping a newbee!

We need to see you full code...
For example - If you don't initialize myYear anywhere then it should be no surprise that values are zero for a global variable.

MHz:
Hi GoForSmoke,
thank you for fast reply. However I'm still missing something. Pls have a look at my modified code

#include <time.h> // die lib einbinden

tm myYear;

void setup() {

...

Serial.println(myYear.tm_year);

No compiler errors anymore - great! However the printed result is 0 and not as expected.

Thank you for helping a newbee!

A struct is a kind of variable. First you define it and then you initialize and THEN you can use it.

Is there a function in time.h to help you fill your tm structure named myYear?

as an extra hint, I would read this page for example

To clarifying my request:

I like to make use of the "extra" time functions in the AVR-libc library (not contained in TimeLib.h)
as described in: https://www.arduino.cc/en/Tutorial/UsingAVRlibraries
excerpt: "Because Arduino already links It links against AVR Libc (the AVR libraries),
it is easy to use them. To use a libc library only the header needs to be included..."

for instance I like to use the function "sun_rise (const time_t *timer)"
(obtaining a ntp timestamp already solved)

for a list of time.h lib's see:
avr-libc: Modules darin u.A. <time.h>

and within time.h see:
http://www.nongnu.org/avr-libc/user-manual/group__avr__time.html

you will find the modules for calculating time of sunrise at a spcific location:
sun_rise (const time_t *timer), time_t
sun_set (const time_t *timer)
set_zone (int32_t)
set_position (int32_t latitude int32_t longitude) etc

What a stone age man know how to do (-> Stonehenge) I can't without help from arduino community.

So I'm asking for an simple AVR-libc time.h example.
As a newbee I'm not so familar useing / decoding the doxygen documentation
nor are the examples helpfull to me

Thank you for any hint and your patience

Hope you have an RTC in there somewhere because Arduino is not a watch company.

Oh what a funny answer from a Tesla Member! Karma goes up to the sky :slight_smile:

Maybe GoForSmoke or someone else can - for dummys only - explain how to use and set the pointer in

time_t time ( time_t * timer)

this would be a great help since arduinos documentation about pointers

https://www.arduino.cc/en/Reference/Pointer

is sparse.

btw Im using Arduino 1.6.9, board Nano ATMega 328 processor

btw Im using Arduino 1.6.9, board Nano ATMega 328 processor

I think that was kinda the question from GoForSmoke. do you have a RTC attached to your system?

thank you J-M-L for jumping in. As a non native speaker GoForSmokes answer "...Arduino is not a watch company" makes no sense to me nor seems it related to my question.

I've already mentioned that I receive and process NTP time stamps (primarily from german time server at ptb). No problem with receiving time/date strings delivered by an DCF77 receiver, more over I own a gps receiver from which I get time and geographical data. No problem by adding a DS1307 RTC however how is this related to my question???

All I asked for was "...how to use and set the pointer in time_t time ( time_t * timer) " module/function/methode you name it.
It's up to you GoForSmoke or any other Arduino community member to answer this (simple and maybe silly newbie) question. Either status nor karma rate counts, it should just be a working solution.

Thank you all

Arduino clock is not so perfect. RTC is Real Time Clock, one way people use for being sure of the time.
Your way is good too but I think cost more over time.

A C tutorial on pointers, enough for a good start.

I could type a lot and not say any more. Pointers are C power tools.

MHz:
I've already mentioned that I receive and process NTP time stamps (primarily from german time server at ptb). No problem with receiving time/date strings delivered by an DCF77 receiver, more over I own a gps receiver from which I get time and geographical data. No problem by adding a DS1307 RTC however how is this related to my question???

All I asked for was "...how to use and set the pointer in time_t time ( time_t * timer) " module/function/methode you name it.

I think there is a confusion on what to do to get what you want to achieve.

if you want to use the standard library, then because "Arduino is not a watch" :slight_smile: and thus it does not know what time it is, you need to first initialize the time in your arduino.

See this and that time GPS example

Once this is done, time functions from the Arduino Time library are available

so

  time_t t = now(); // Store the current time in time  variable t

then your pointer is just &t

It might be confusing why we need to pass a pointer to the time_t value, I would guess that in the early days of computing the time() function could not directly return a long int and thus the data was passed by reference...

J-M-L:
It might be confusing why we need to pass a pointer to the time_t value, I would guess that in the early days of computing the time() function could not directly return a long int and thus the data was passed by reference...

There were no long ints, so it was hidden as a struct. Same reason for size_t.

http://c-faq.com/lib/curtime.html

note the note.

KeithRB:
There were no long ints, so it was hidden as a struct. Same reason for size_t.

Question 13.12

note the note.

Thanks for the reference!

Bingo!

Thank you for patience and support

Great!

happy coding!