Shop OBEX P1 Docs P2 Docs Learn Events
Hours calculation between different dates on a DS1302 — Parallax Forums

Hours calculation between different dates on a DS1302

Sutton MurraySutton Murray Posts: 88
edited 2006-10-30 07:07 in BASIC Stamp
I want to build an hour meter for a diesel engine. My thoughts was to make use of the DS1302 IC. I am getting stuck with calculating the amount of hours between the different dates. I thought it would be as easy as one will calaculate this on a normal calculator. But i realized that things aren't working as i was thinking.
Can any one maybe help me with an example?

Comments

  • Larry~Larry~ Posts: 242
    edited 2006-10-29 18:54
    Go here http://www.emesystems.com/ and lookup julian this will put the year, month, day, hour, min, sec into one number which allows add/sub or any other math
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-10-30 00:33
    Here is a summary of what Larry referred to. There are 8760 hours in a year, 8784 in a leap year. The following formula gives the ordinal hour of the year, an integer from 0 to 8760 (or 8784).

    YY VAR Byte    ' year as integer, not BCD
    MM VAR Byte   ' month
    DD VAR Byte   ' day
    HH VAR Byte   ' hour
    JDH VAR Word    ' hour of year (for 2001 to 2099)
    JDH=(MM-1*30+(MM/9+MM/2)-(MM max 3/3*(YY//4 max 1 +1))+ DD-1) * 24 + HH  ' hours into year
    



    If you need to calculate across year boundaries just add in the total hours from th past year, and continue on from 8760 upward.

    It becomes a more difficult if you have to do it down to the minute, because there are 525,600 minutes in a non-leap year, and that requires a double precision (32 bit) calculation. But it is the most convenient way to do many time calculations, otherwise it a a tangle of mixed base of 60/24/356,366.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Sutton MurraySutton Murray Posts: 88
    edited 2006-10-30 07:07
    Larry thanks to the link reference.

    Tracey thank you for the sample, i have looked at your web site (your examples). I can/have a lot to learn. Great examples i will be going through this.

    I should be coming right with the examples.
    Thanks to you and your Web site.
Sign In or Register to comment.