Calculate Epoch (UNIX) time
Page 1 of 1

Author:  hextakatt [ Fri Feb 15, 2019 8:15 pm ]
Post subject:  Calculate Epoch (UNIX) time

Well, as the title says, I want to calculate the epoch (UNIX time).
As you all know, (or so I believe) epoch time is the seconds since 1/1/1970.
It's easy to do, just multiply 31557600 (the number of seconds a year has) by the years between 1970 and the current year is that you're reading this. (in this case 2019).
I writted the algorithm in Python, and it works.
Now I port it to C, reading the year from CMOS, well you know.
void epocht(void)
    Hardcoded, 'cause CMOS does not have an standard
    register to century...
    Anyways, I'll be dead when this century ends.
    int century = 20;
    uint8_t year = get_rtcdate(9);
    int fnyr;
    /* Concatenates century and year in one integer */
    fnyr = intcat(century, year);
    println(nl"Year: ");
    println(itoa(fnyr, buf, 10));
    //fnyr = atoi(buf);
    /* 1 year = 31557600 seconds */
    int yrsec = 31557600;
    Epoch time starts at 1/1/1970 (DD/MM/YYYY)
    How many years since 1970 and the actual year?
    int yrtn = 1970 - fnyr;
    char buf9[255];
    unsigned int i = 0;
    while (i <= abs(yrtn))
        Formula 31557600 * years since 1970
        yrsec *= i;
    println(nl"Epoch time: ");
    println(itoa(yrsec, buf9, 10));

This code is supposed to give the epoch time between 1/1/1970 and 1/1/actualyear. It just calculate the first day and month of an year, but later I fix that.
Well, the problem is that prints nothing, and another problem is that uint8_t year = get_rtcdate(9); returns 25, instead of the actual year.
It is weird because it was working before, I don't know what happened.

Author:  bzt [ Fri Feb 15, 2019 11:10 pm ]
Post subject:  Re: Calculate Epoch (UNIX) time

The CMOS can store the date in binary and BCD formats too (hint: 25 = 0x19). And what about leap years and leap seconds? And months, days, timezones? Hmm?
It takes a bit more than multipling by a precalculated seconds per year.


Author:  Korona [ Sat Feb 16, 2019 1:16 am ]
Post subject:  Re: Calculate Epoch (UNIX) time

I do not really understand the point of your while loop. Can't you just multiply the number of seconds per year with the number of years since 1970? As for coding style, i would recommend to write x*100+y instead of having an intcat() function :D.

As bzt said, it seems likely that you ran into BCD encoding.

UNIX timestamps usually do not take leap seconds into account, i.e., if you call a function like clock_gettime(CLOCK_REALTIME) and friends, you get a value that differs from the actual number of seconds since 01/01/1970 by the number of leap seconds since then. The tradeoff here is that it is much easier to convert the resulting number to a human-readable form (by static computations). On the other hand, the existence of leap seconds is essentially determined by a consortium based on observation of the earth's trajectory. On UNIX, the timestamps of leap seconds are usually stored in TZinfo files, i.e., those pointed to by /etc/localtime (although, they are not needed to convert UNIX time to local time).

Of course, the UNIX timestamps (as returned by clock_gettime() and friends) do take leap years and differences in the number of days per month into account.

Author:  fpissarra [ Sat Feb 16, 2019 9:34 am ]
Post subject:  Re: Calculate Epoch (UNIX) time

It is a little bit more complex than this. Take a look at time/mktime.c in glibc source code.

Author:  hextakatt [ Sat Feb 16, 2019 10:20 am ]
Post subject:  Re: Calculate Epoch (UNIX) time

fpissarra wrote:
It is a little bit more complex than this. Take a look at time/mktime.c in glibc source code.

Oh, I see. Better I port the GCC C standard library.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group