Calculate Epoch (UNIX) time

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Calculate Epoch (UNIX) time

Post by deleted8917 »

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.

Code: Select all

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;
        ++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.
Thanks.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Calculate Epoch (UNIX) time

Post by bzt »

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.

Cheers,
bzt
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Calculate Epoch (UNIX) time

Post by Korona »

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.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
fpissarra
Posts: 8
Joined: Mon Nov 26, 2018 9:14 am

Re: Calculate Epoch (UNIX) time

Post by fpissarra »

It is a little bit more complex than this. Take a look at time/mktime.c in glibc source code.
deleted8917
Member
Member
Posts: 119
Joined: Wed Dec 12, 2018 12:16 pm

Re: Calculate Epoch (UNIX) time

Post by deleted8917 »

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.
Post Reply