Time difference with time.h

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

I have a problem using time.h for the ATMEGA328PB, finding the time difference of 2 sets of times in seconds.
Both sets come from an external RTC.

First, I have a start time:  uint8_t TMR_start_year , uint8_t TMR_start_month, ....uint8_t TMR_start_second,
and second there is the stop time:  uint8_t TMR_stop_year ...uint8_t TMR_stop_second.  The uint8_t TMR...year is the number above 2000.

 

I've already read this post ...https://www.avrfreaks.net/forum/...  , but I really can't figure out how to use the difftime() function with my sets of times.

 

Many thanks for some coding help!

This topic has a solution.
Last Edited: Sun. Oct 10, 2021 - 11:19 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Fill in a struct tm with the values from your RTC.

Then you can convert the struct tm into a time_t with mktime()

 

Rinse and repeat for your 2 sets of times.    difftime() will give you the difference in seconds.

 

Note that you don't need to fill in every field of the struct tm

e.g. the day of the week can be ignored.   Just make sure that you have sec, min, hour, day, month, year fields.

 

David.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Many thanks David.

It works fine!

Bexx

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am impressed !

Most people just throw up their hands in horror.

 

I was feeling guilty that I had not posted example calls.   It is good to know that someone reads the time.h function documentation.

 

And you can use the same approach for any RTC chip and any target MCU or Operating System.

You will find it useful for calculating oestrus and parturition dates.

 

David.