# OSDev.org

The Place to Start for Operating System Developers
 It is currently Mon May 25, 2020 11:54 am

 All times are UTC - 6 hours

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Calculate Epoch (UNIX) timePosted: Fri Feb 15, 2019 8:15 pm
 Member

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

 Post subject: Re: Calculate Epoch (UNIX) timePosted: Fri Feb 15, 2019 11:10 pm
 Member

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

 Post subject: Re: Calculate Epoch (UNIX) timePosted: Sat Feb 16, 2019 1:16 am
 Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 704
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 .

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].

Top

 Post subject: Re: Calculate Epoch (UNIX) timePosted: Sat Feb 16, 2019 9:34 am

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

 Post subject: Re: Calculate Epoch (UNIX) timePosted: Sat Feb 16, 2019 10:20 am
 Member

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Operating System Development    OS Development    OS Design & Theory    Announcements, Test Requests, & Job openings Everything Else    General Programming    General Ramblings    Auto-Delete Forum OSDev.org    OSDev Wiki    About this site