OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Mar 23, 2019 7:02 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Calculate Epoch (UNIX) time
PostPosted: Fri Feb 15, 2019 8:15 pm 
Offline
Member
Member

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 88
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:
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.


Top
 Profile  
 
 Post subject: Re: Calculate Epoch (UNIX) time
PostPosted: Fri Feb 15, 2019 11:10 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 236
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


Top
 Profile  
 
 Post subject: Re: Calculate Epoch (UNIX) time
PostPosted: Sat Feb 16, 2019 1:16 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 538
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: A microkernel-based OS that is capable of running a Wayland desktop


Top
 Profile  
 
 Post subject: Re: Calculate Epoch (UNIX) time
PostPosted: Sat Feb 16, 2019 9:34 am 
Offline

Joined: Mon Nov 26, 2018 9:14 am
Posts: 6
It is a little bit more complex than this. Take a look at time/mktime.c in glibc source code.


Top
 Profile  
 
 Post subject: Re: Calculate Epoch (UNIX) time
PostPosted: Sat Feb 16, 2019 10:20 am 
Offline
Member
Member

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 88
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group