Home AMX User Forum NetLinx Studio

How to calculate seconds elapsed since 00:00:00 GMT January 1, 1970?

Does anyone know how take the current date and time and calculate the number of seconds elapsed since 00:00:00 GMT January 1, 1970? The value is expressed in 4 bytes.

I also need to figure out the inverse, given 4 bytes (the number of seconds elapsed?) convert it to a time and date value.

If anyone has written such functions or can anyone point me in the right direction, I?d really appreciate it.

TIA

Comments

  • jeffacojeffaco Posts: 121
    I've got that ...

    My TimeSync module does that. I have those to support the NTP (network time protocol code) where the time comes back as seconds since January 1st, 1970. It's up on source forge (and is used in i!-TimeManager as well).

    You can fetch the sources to the TimeSync module up on SourceForge:

    http://sourceforge.net/projects/netlinx-modules/

    That will do EXACTLY what you want. Cut and paste the relevant routine from TimeSync.axs into a module of your choice.

    -- Jeff
  • Joe HebertJoe Hebert Posts: 2,159
    Thanks, Jeff. I'll check it out.
  • Joe HebertJoe Hebert Posts: 2,159
    Jeff,

    I must be missing something. I downloaded TimeSync-1.0.zip and extracted it, however, there is no TimeSync.axs in the zip. There is an include file named TimeSyncMod.axi. I looked through each file in the zip but don't see any source to do the time conversion. Any ideas?

    Thanks,
  • Joe HebertJoe Hebert Posts: 2,159
    I think I have it figured out for the most part. I just need to determine the best way to handle leap years. Thanks, anyhow.
  • jeffacojeffaco Posts: 121
    Hi, sorry for the lack of information.

    The .zip file you downloaded was a BINARY source file. I've learned that it's easier to distribute binary, as the source is more involved to build (you need bits of syslog - at least the .axi file, etc).

    Look here:

    http://cvs.sourceforge.net/viewcvs.py/netlinx-modules/NetLinx-Modules/TimeSync/

    This will take you to the TimeSync source code.

    Hope that helps. Note that my code handles leap years as well.

    -- Jeff
  • frthomasfrthomas Posts: 176
    Here's a little utility .axi defining a "DateTime" structure with associated functions working on it, one of them being the difference in seconds between two dates.
    STACK_VAR _DateTime now, y0;
    STACK_VAR LONG theResult;
    
    DateTime_Now(now);
    DateTime_SetDate(y0, 1970, 1, 1)
    DateTime_SetTime(y0, 0, 0, 0)
    theResult = DateTime_GetDiffSeconds(now, y0)
    

    Doing the reverse:
    //assume theSecs contains the seconds...
    STACK_VAR _DateTime theResult;
    
    DateTime_SetDate(theResult, 1970, 1, 1)
    DateTime_SetTime(theResult, 0, 0, 0)
    DateTime_SecondOffset(theResult, theSecs)
    
    // Now use theResult, f.e. using DateTime_GetDateTimeString()...
    

    It is not very well documented, but fire away if you have a question. One thing you need to do in the CALLING code is to define the functions you are actually using. The .axi figures out by itself which internal ones are needed if any. For example:
    #define __DATETIME_SecondOffset__
    
    In any case Netlinx will remind you some function is not defined. Alternatively uncomment all such defines in the .axi. I do this to keep the code size down, I am reaching 1MB and the limit of my old Netlinx card...

    Credit where credit is due: part of this code is derived from Jeff's code on sourceforge, in particular the stuff dealing with calculations of seconds since 1900...

    Hope this helps

    Fred
  • Joe HebertJoe Hebert Posts: 2,159
    Thanks for your help Jeff and Fred. :)
Sign In or Register to comment.