LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-21-2013, 06:43 AM   #1
huan1989
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Rep: Reputation: Disabled
Question How to get date in kernel space


Hi, everybody. I have a problem when get time from linux kernel. I writing a driver module on CentOS 5.4, kernel 2.6.18. I want to read day, month, year, hour, minute, second of system, then would printk to screen monitor. Now, i had read hour, minute, second of system folow this code:
Code:
do_gettimeofday(&tv);
get_time = tv.tv_sec;
sec = get_time % 60;
tmp1 = get_time / 60;
min = tmp1 % 60;
tmp2 = tmp1 / 60;
hr = tmp2 % 24;
hr = hr + 7; /* convert to GMT+7 */
po_data.opcode = 0x55;
po_data.cmd = 0x01;
po_data.reverse = 0x00;
po_data.data[0] = (unsigned char)hr; 
po_data.data[1] = (unsigned char)min; 
printk(KERN_ALERT "%d:%d \n",hr,min);
But, i don't get day, month, year of system. I use the command
Code:
tmp = getdate(0);
(KERN_ALERT "%d-%d \n",tmp->tm_mon,tmp->tm_mday);
and get error: error: implicit declaration of function getdate. I had include time.h or sys/time.h but get error: sys/time.h: No such file or directory.
How to get day, month and year from driver ? Can it ? if not, why ?
thanks and regards.
 
Old 10-21-2013, 06:56 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,500

Rep: Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431Reputation: 2431
just search for it:
http://stackoverflow.com/questions/5...x-kernel-space
 
Old 10-21-2013, 09:06 PM   #3
huan1989
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I can get time of day (hour, minute, second) by using do_gettimeofday function. But, i can't get day, month and year of system.
 
Old 10-21-2013, 11:37 PM   #4
huan1989
LQ Newbie
 
Registered: Oct 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
i use code in time.c to get time of system. It's working.
Code:
unsigned long get_cmos_time(void)
{
	unsigned int year, mon, day, hour, min, sec;
	int i;

	/* The Linux interpretation of the CMOS clock register contents:
	 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
	 * RTC registers show the second which has precisely just started.
	 * Let's hope other operating systems interpret the RTC the same way.
	 */
	/* read RTC exactly on falling edge of update flag */
	for (i = 0 ; i < 1000000 ; i++)	/* may take up to 1 second... */
		if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
			break;
	for (i = 0 ; i < 1000000 ; i++)	/* must try at least 2.228 ms */
		if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
			break;
	do { /* Isn't this overkill ? UIP above should guarantee consistency */
		sec = CMOS_READ(RTC_SECONDS);
		min = CMOS_READ(RTC_MINUTES);
		hour = CMOS_READ(RTC_HOURS);
		day = CMOS_READ(RTC_DAY_OF_MONTH);
		mon = CMOS_READ(RTC_MONTH);
		year = CMOS_READ(RTC_YEAR);
	} while (sec != CMOS_READ(RTC_SECONDS));
	if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
	  {
	    BCD_TO_BIN(sec);
	    BCD_TO_BIN(min);
	    BCD_TO_BIN(hour);
	    BCD_TO_BIN(day);
	    BCD_TO_BIN(mon);
	    BCD_TO_BIN(year);
	  }
	if ((year += 1900) < 1970)
		year += 100;
	return mktime(year, mon, day, hour, min, sec);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel Panic when netlink message is sent from User space to kernel space itrilok Programming 1 11-28-2012 11:56 AM
[SOLVED] date; how to display as i.e Oct 1 *note the extra space* greenpool Linux - Newbie 1 10-01-2011 01:41 AM
[SOLVED] date space in between not tab ted_chou12 Linux - Newbie 2 03-29-2011 08:05 PM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 10:59 AM
date value space prolbem jimmyjiang Red Hat 3 03-05-2008 01:24 AM


All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration