is it available to get time using only c code?

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
Mohamed007
Posts: 14
Joined: Fri Jul 08, 2016 8:44 pm

is it available to get time using only c code?

Post by Mohamed007 »

Hi, i was asking if there is a way to get time with just c code, with out assembly..if not, how to do it with assembly?
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: is it available to get time using only c code?

Post by alexfru »

Your question should be a bit more specific. If you're using a standard C compiler (which implies a standard C library), then there are time functions declared in <time.h>, look them up. If not, it depends on your hardware. If time can be obtained by reading memory at a specific address, then you may be able to do just that. If, however, communication with the timer/clock cannot be done via memory alone (say, you need to access x86 I/O ports), then you're probably out of luck.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: is it available to get time using only c code?

Post by Brendan »

Hi,
Mohamed007 wrote:Hi, i was asking if there is a way to get time with just c code, with out assembly..if not, how to do it with assembly?
It's impossible to get the time with just C code without any assembly being used directly or indirectly (e.g. in a library somewhere).

To get the time you'll need to determine where you're getting the time from - getting time from (e.g.) the RTC chip on 80x86 is very different from getting time from an NTP server.

There is example code to get time and date from the RTC chip on 80x86 (which is almost entirely in C, but uses assembly for IO port access) on the wiki. Of course you don't really know which time and date your getting (local time or UTC or something else) and this should only need to be done once (e.g. the OS should do "time_now = time_at_boot + time_since_boot" where you only determine "time_at_boot" once and "time_size_boot" can be maintained by a more precise timer like HPET).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: is it available to get time using only c code?

Post by Octacone »

I think that the best way of doing it would be accessing CMOS chip and getting time from there. But you need to use assembly for that as far as I know, your best bet is coding assembly from your C file.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Mohamed007
Posts: 14
Joined: Fri Jul 08, 2016 8:44 pm

Re: is it available to get time using only c code?

Post by Mohamed007 »

alexfru wrote:Your question should be a bit more specific. If you're using a standard C compiler (which implies a standard C library), then there are time functions declared in <time.h>, look them up. If not, it depends on your hardware. If time can be obtained by reading memory at a specific address, then you may be able to do just that. If, however, communication with the timer/clock cannot be done via memory alone (say, you need to access x86 I/O ports), then you're probably out of luck.
im using gcc and there is no <time.h> library available
Mohamed007
Posts: 14
Joined: Fri Jul 08, 2016 8:44 pm

Re: is it available to get time using only c code?

Post by Mohamed007 »

Brendan wrote:Hi,
Mohamed007 wrote:Hi, i was asking if there is a way to get time with just c code, with out assembly..if not, how to do it with assembly?
It's impossible to get the time with just C code without any assembly being used directly or indirectly (e.g. in a library somewhere).

To get the time you'll need to determine where you're getting the time from - getting time from (e.g.) the RTC chip on 80x86 is very different from getting time from an NTP server.

There is example code to get time and date from the RTC chip on 80x86 (which is almost entirely in C, but uses assembly for IO port access) on the wiki. Of course you don't really know which time and date your getting (local time or UTC or something else) and this should only need to be done once (e.g. the OS should do "time_now = time_at_boot + time_since_boot" where you only determine "time_at_boot" once and "time_size_boot" can be maintained by a more precise timer like HPET).


Cheers,

Brendan
i followed the code and coded out_byte and in_byte functions like that (the same way i used while dealing with keyboard ports and its working for the it)

Code: Select all

uint8 in_byte (uint16 _port)
{
    	uint8 rv;
    	__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    	return rv;
}

Code: Select all

void out_byte (uint16 _port, uint8 _data)
{
	__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
*uint8 and uint16 is a defined types by me*
after doing so, still cant prints out `hour` variable or any other variable of the code

what mistake i've done?
Post Reply