• AVR Freaks

Hot!How to calculate the difference between two times <time.h>

Page: 12 > Showing page 1 of 2
Author
Neraildes
Super Member
  • Total Posts : 208
  • Reward points : 0
  • Joined: 2017/10/11 07:13:25
  • Location: 0
  • Status: offline
2019/01/17 02:43:02 (permalink)
0

How to calculate the difference between two times <time.h>

Good morning people,

I need to do a function that returns the difference between two times, inclusive can be greater than 24 hours.
I was seeing the difftime function <time.h>. Can someone help me to develop the idea.
 
my hardware loosens the time in the following format:

data = "yy / mm / dd"
time = "hh: mm: ss"
post edited by Neraildes - 2019/01/17 03:04:41
#1

38 Replies Related Threads

    GERSEI
    New Member
    • Total Posts : 28
    • Reward points : 0
    • Joined: 2015/04/02 06:54:35
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 03:17:13 (permalink)
    0
    Hi, do you mean time difference between external events (then I would use interrupt service routine based ont INTx or IOCx to set flags) or internal events (then you could just set flags when they happen) ? In both cases, you may use a TMR1 (16 bits) with a very slow clock and an interrupt every 1s; the first event flag starts the timer and the second event flag stops it;  a software counter (long integer) is incremented on each interrupt of the Timer ; on the second flag, the time difference is given by the value of the counter in seconds .
    Hope this helps ??
    #2
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 03:31:07 (permalink)
    0
    GERSEI
    Hi, do you mean time difference between external events (then I would use interrupt service routine based ont INTx or IOCx to set flags) or internal events (then you could just set flags when they happen) ? In both cases, you may use a TMR1 (16 bits) with a very slow clock and an interrupt every 1s; the first event flag starts the timer and the second event flag stops it;  a software counter (long integer) is incremented on each interrupt of the Timer ; on the second flag, the time difference is given by the value of the counter in seconds .
    Hope this helps ??




    My time comes from a DS1307 chip.
    #3
    crosland
    Super Member
    • Total Posts : 1580
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 03:55:20 (permalink)
    +2 (2)
    Convert to epoch time (google it) and do a simple subtraction.
    #4
    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 04:59:43 (permalink)
    +2 (2)
    Hi,
    Time from a DS1307, and also some other similar RTC chips, is in BCD format, Binary Coded Decimal,
    with one decimal digit in each 4 bit nibble, with 2 decimal digits in each 8 bit byte.
    This is reasonably easy to convert into text, by picking out each 4 bit digit, and put into a character byte.
     
    Doing arithmetic on BCD values, will need a specialiced software library,
    but it will be even more complicated with Time values, with some values rolling around in 12 or 24, hours
    and some values roll over in 60 seconds or minutes. 
    Doing arithmetic like time difference on text string representation is even more cumbersome.
     
    In <time.h> there are some conversion functions.
    I think it should be possible to convert digits from the DS1307 into a: struct tm
    and then use this as argument to mktime:  

    time_t mktime( struct tm *time );
    This should produce a result of type time_t, which is a integer variable counting seconds.
    Then it may be possible to use difftime function, or do integer arithmetic.
     
    For overview description, see wikipedia https://en.wikipedia.org/wiki/C_date_and_time_functions
    or C library documentation.
     
        Mysil
    #5
    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 09:13:56 (permalink)
    +1 (1)
    Hi,
    Something like this:
    /*
     *    Time calculations
     */
    #include <stdio.h>
    //#define  _CLOCKS_PER_SEC_ 1
    #include <time.h>

    #if defined DS1307
    /* For external DS1307  RTC device the structure might be something like this: */
    // Union to access rtcc registers
    typedef union tagRTCC
    {    struct
        {
            uint8_t        sec;
            uint8_t        min;
            uint8_t        hr;
            uint8_t        wkd;
            uint8_t        day;
            uint8_t        mth;
            uint8_t        yr;
        };
        struct
        {    uint16_t    prt00;
            uint16_t    prt01;
            uint16_t    prt10;
            uint16_t    prt11;
        };
        struct
        {    uint32_t    time;
            uint32_t    date;
        };
        uint8_t     buffer[8];
    } RTCC_t;

      RTCC_t        _time;                    /* This is a struct to contain BCD values from RTCC */

    #else
      #include "../../Board_Support_Package/Rtcc_driver.h"
      extern RTCC_t    _time;                    /* This is a struct containing BCD values from my RTCC */
    #endif
    static struct tm time_s;                /* Structure with decimal date and time. */

    time_t daytime (void)
    {    time_t current_time;
                                        /* Convert BCD values from RTC to decimal calendar values.
                                         * This require RTC to be configured for 24 hour mode.
                                         * AM/PM mode will need additional logic. */
         time_s.tm_year  = (_time.yr >> 4) * 10;
        time_s.tm_year +=  _time.yr & 0x0F;
        time_s.tm_year += 100;                /* Counting years from 1900 */
        time_s.tm_mon  = (_time.mth >> 4 & 1) * 10 + (_time.mth & 0x0F);
        time_s.tm_mday = (_time.day >> 4 & 3) * 10 + (_time.day & 0x0F);
        time_s.tm_hour = (_time.hr  >> 4)     * 10 + (_time.hr  & 0x0F);
        time_s.tm_min  = (_time.min >> 4)     * 10 + (_time.min & 0x0F);
        time_s.tm_sec  = (_time.sec >> 4 & 7) * 10 + (_time.sec & 0x0F);
        current_time = mktime(&time_s);
        return current_time;
    }

     
    #6
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:04:37 (permalink)
    0
    Mysil
    Hi,
    Something like this:
    /*
     *    Time calculations
     */
    #include <stdio.h>
    //#define  _CLOCKS_PER_SEC_ 1
    #include <time.h>

    #if defined DS1307
    /* For external DS1307  RTC device the structure might be something like this: */
    // Union to access rtcc registers
    typedef union tagRTCC
    {    struct
        {
            uint8_t        sec;
            uint8_t        min;
            uint8_t        hr;
            uint8_t        wkd;
            uint8_t        day;
            uint8_t        mth;
            uint8_t        yr;
        };
        struct
        {    uint16_t    prt00;
            uint16_t    prt01;
            uint16_t    prt10;
            uint16_t    prt11;
        };
        struct
        {    uint32_t    time;
            uint32_t    date;
        };
        uint8_t     buffer[8];
    } RTCC_t;

      RTCC_t        _time;                    /* This is a struct to contain BCD values from RTCC */

    #else
      #include "../../Board_Support_Package/Rtcc_driver.h"
      extern RTCC_t    _time;                    /* This is a struct containing BCD values from my RTCC */
    #endif
    static struct tm time_s;                /* Structure with decimal date and time. */

    time_t daytime (void)
    {    time_t current_time;
                                        /* Convert BCD values from RTC to decimal calendar values.
                                         * This require RTC to be configured for 24 hour mode.
                                         * AM/PM mode will need additional logic. */
         time_s.tm_year  = (_time.yr >> 4) * 10;
        time_s.tm_year +=  _time.yr & 0x0F;
        time_s.tm_year += 100;                /* Counting years from 1900 */
        time_s.tm_mon  = (_time.mth >> 4 & 1) * 10 + (_time.mth & 0x0F);
        time_s.tm_mday = (_time.day >> 4 & 3) * 10 + (_time.day & 0x0F);
        time_s.tm_hour = (_time.hr  >> 4)     * 10 + (_time.hr  & 0x0F);
        time_s.tm_min  = (_time.min >> 4)     * 10 + (_time.min & 0x0F);
        time_s.tm_sec  = (_time.sec >> 4 & 7) * 10 + (_time.sec & 0x0F);
        current_time = mktime(&time_s);
        return current_time;
    }

     




    Hi, Mysil,

    My compiler does not recognize the type uint8_t, so I tried to include the file:

    #include <types.h>

    also did not work.
    So I tried to define a type this way:

    typedef uint8_t unsigned int

    also did not work.
    What should I do?
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 17389
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:12:30 (permalink)
    +2 (2)
    What compiler are you using?
    Does work means what?
    Try including stdint.h
    uint8_t is unsigned char. ( 8 bits) not an int.
    #8
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:13:43 (permalink)
    0
    What I want to do is a function that calculates the difference between time1 and time2.
    #9
    jack@kksound
    code tags!
    • Total Posts : 3188
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:18:08 (permalink)
    +1 (1)
    #include <types.h>

    also did not work.
    So I tried to define a type this way:

    typedef uint8_t unsigned int
     
    Try
    #include <stdint.h>

     
     
    #10
    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:22:04 (permalink)
    +1 (1)
    Sorry,
    It is:
    #include <stdint.h> 

    that have type definitions for C standard integer type definitions.
    uint8_t is unsigned 8 bit integer, aka char, so the typedef in there is:
    typedef unsigned char uint8_t;
     
        Mysil
    post edited by Mysil - 2019/01/17 10:25:57
    #11
    malaugh
    Super Member
    • Total Posts : 396
    • Reward points : 0
    • Joined: 2011/03/31 14:04:42
    • Location: San Diego
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:22:50 (permalink)
    +1 (1)
    As the previous poster said, convert your time to a time_t structure.  It s not clear form your description that exact form your time data takes, so converting is something you may need to look into.
     
    The time_t structure is defined as:

    int    tm_sec   seconds [0,61]
    int tm_min minutes [0,59]
    int tm_hour hour [0,23]
    int tm_mday day of month [1,31]
    int tm_mon month of year [0,11]
    int tm_year years since 1900
    int tm_wday day of week [0,6] (Sunday = 0)
    int tm_yday day of year [0,365]
    int tm_isdst daylight savings flag
    [/code]You just need to fill out the element with the values.  Something like below.  
     
    time_t tm;
    char data[] = "yy / mm / dd";
    char time[] = "hh: mm: ss";
     
    tm.tm_sec = atoi(&data[7]);
    tm.tm_min = atoi(&data[3]);
    tm.tm_hour = atoi(data);
    tm.tm_mday = atoi(&time[7]);
    tm.tm_mon = atoi(&time[3]) - 1;
    tm.tm_year = atoi(time) + 100;
    tm.tm_wday = 0;
    tm.tm_yday = 0;
    tm.tm_isdst = 0;
     
    [/code]
    Now you can use all the functions described in time.h, including difftime.
      
     
    #12
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 10:34:16 (permalink)
    0
    Thank you. I'll implement ...
    #13
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 11:06:52 (permalink)
    0
    I'm implementing DEVC ++. Look how it was! Where it says "Code here" I must load the two time variables and then calculate the difference. Being a layperson, I'd like to know how I do it.
     
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <time.h>



    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    typedef union tagRTCC
    { struct
        {
            uint8_t sec;
            uint8_t min;
            uint8_t hr;
            uint8_t wkd;
            uint8_t day;
            uint8_t mth;
            uint8_t yr;
        };
        struct
        { uint16_t prt00;
            uint16_t prt01;
            uint16_t prt10;
            uint16_t prt11;
        };
        struct
        { uint32_t time;
            uint32_t date;
        };
        uint8_t buffer[8];
    } RTCC_t;

      RTCC_t _time;
      

    static struct tm time_s; /* Structure with decimal date and time. */
      
    //=====================================================================================
    int main(int argc, char *argv[]) {
     
     
     // Code Here
     
     
     return 0;
    }
    //=====================================================================================





    time_t daytime (void)
    { time_t current_time;
                                        /* Convert BCD values from RTC to decimal calendar values.
                                         * This require RTC to be configured for 24 hour mode.
                                         * AM/PM mode will need additional logic. */
         time_s.tm_year = (_time.yr >> 4) * 10;
        time_s.tm_year += _time.yr & 0x0F;
        time_s.tm_year += 100; /* Counting years from 1900 */
        time_s.tm_mon = (_time.mth >> 4 & 1) * 10 + (_time.mth & 0x0F);
        time_s.tm_mday = (_time.day >> 4 & 3) * 10 + (_time.day & 0x0F);
        time_s.tm_hour = (_time.hr >> 4) * 10 + (_time.hr & 0x0F);
        time_s.tm_min = (_time.min >> 4) * 10 + (_time.min & 0x0F);
        time_s.tm_sec = (_time.sec >> 4 & 7) * 10 + (_time.sec & 0x0F);
        current_time = mktime(&time_s);
        return current_time;
    }

     
     HOW I SHOULD CHARGE THE TIME DATA, I NEVER WORK WITH 'UNION'.
    post edited by Neraildes - 2019/01/17 11:42:31
    #14
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 12:14:02 (permalink)
    +1 (1)
    Do you have any sort of C textbook?
    "union" is a basic concept that will be explained in any C textbook.
    That union just allows you to access your data either as a simple array of bytes, or as a structure of named fields.
     

    Nearly there...
    #15
    Jim Nickerson
    User 452
    • Total Posts : 5969
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 12:36:10 (permalink)
    +1 (1)
    Is your profile accurate with regard to experience ?

    Attached Image(s)

    #16
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 12:37:23 (permalink)
    0
    ok, I will understand the concept of union and tomorrow I come back to continue developing the routine that calculates the time difference. Thank you all.
    #17
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/17 12:57:26 (permalink)
    0
    JANickerson
    Is your profile accurate with regard to experience ?





    I have a certain experience with C, I work with structures, macros, pointers, pointers, dynamic allocation ... But some resources I used little and I have little practice. But you can let me quickly learn the concept of UNION.
    #18
    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/18 05:17:42 (permalink)
    0
    Hi,
    A union is a way to access a structure or array, one piece at a time, as a array, or all together.
    The union in example in message #6 is intended to reproduce layout of registers in DS1307.
    You may fill in bytes as they arrive from DS1307, into _time.buffer[...]
    and then pick it apart as shown in example.
    Or you may call a function to retrieve time registers from DS1307, using address of the buffer array as pointer:
     
        GetDS1307( &_time.buffer[0] );  

    and fill in 8 bytes into the array inside the function.
    void GetDS1307( time_array[8] )
    {    /* Fill time_array[0 ... 7] with values from RTC device. */
        ...
    }

     
    You probably have been using union and structures many times when accessing PIC SFR control registers.
     
    There is a trick when writing a program using functions into a single c source file:
    Place source code of any functions to be called Before the main program function.
    /*
     *  Description, #includes, type definitions and declarations Here.
     */
    //=========================================================
    void GetDS1307(uint8_t array[8] )
    {
        /* Fill array[0 ... 7] with values from RTC device. */
        /* Or fill array[0] to array[7] with syntetic test values.
        ...
    }
    //=========================================================
    time_t daytime (void)
    {   time_t current_time;
        GetDS1307( &_time.buffer[0] );
        ...
        // code as shown above
        ...
        return current_time;
    }
    //=========================================================
    int main(int argc, char *argv[])
    {
        static time_t StartTime, CurrentTime, ElapsedTime;

        // Code Here
        StartTime = daytime();    /* Epoc time */
        while (1)
        {
            CurrentTime = daytime();    /* Epoc time */
            ElapsedTime = CurrentTime - StartTime;
            // More code here
        }
        return 0;
    }

     
        Mysil
    #19
    Neraildes
    Super Member
    • Total Posts : 208
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: How to calculate the difference between two times <time.h> 2019/01/19 09:43:16 (permalink)
    0
     
    qhb
    Do you have any sort of C textbook?
    "union" is a basic concept that will be explained in any C textbook.
    That union just allows you to access your data either as a simple array of bytes, or as a structure of named fields.
     




    Thanks qhb, thanks to you I learned something very important in C. Thanks for the tip. Look at the code I made for setar or read bit, and also store char and int.

    Thank you very much
     
     
     
     
     
    typedef union num{
         union{
            char cnum;
            int inum;
         };
         struct{
         unsigned int b0 : 1;
         unsigned int b1 : 1;
         unsigned int b2 : 1;
         unsigned int b3 : 1;
         unsigned int b4 : 1;
         unsigned int b5 : 1;
         unsigned int b6 : 1;
         unsigned int b7 : 1;
         };
         
    } t_test;
       


    int main(void)
      {
      t_test var;
     
      var.b3=1;
       
      printf("%d",var);
      
      return 0;
      }

    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5