LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 07-02-2009, 05:25 PM   #1
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Rep: Reputation: 15
Convert timeval to timespec


Hey all,

I have some code that was written by a partner of mine originally for user space and now I need to implement it as a module in kernel space. One of the functions in the code sets the system time. It originally used settimeofday with a timeval stuct passed in but the kernel space equivalent do_settimeofday takes a timepsec struct instead so I need to convert the timeval to a timespec. Has anyone done this before and knows a good way to do it?

Digging through the source I didn't see any function that directly converts it but I did find:
Code:
static inline s64 timeval_to_ns(const struct timeval *tv);
extern struct timespec ns_to_timespec(const s64 nsec);
You can probably guess where I'm going with that. What the hell is an s64 type?

Also from linux/time.h that might help:
Code:
struct timespec {
    time_t tv_sec;
    long   tv_nsec;
};

struct timeval {
    time_t      tv_sec;
    suseconds_t tv_usec;
};
 
Old 07-03-2009, 09:47 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
That's not so difficult! Or am I missing something?
How many ns in 1 us... 1000 so just multiply tv_usec by 1000 to get tv_nsec, and divide tv_nsec per 1000 to have the tv_usec, you can round it too...
 
Old 07-06-2009, 04:28 PM   #3
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jf.argentino View Post
That's not so difficult! Or am I missing something?
No you're right, it's not very difficult. It was more a question I was raising since I hadn't worked with timespecs at all before so I wanted to ensure the approach I was taking is correct. Currently I have it implemented as such:
Code:
struct timeval tv
struct timespec ts;
s64 nsec;

do_gettimeofday(&tv);

nsec = timeval_to_ns(&tv);
ts = ns_to_timespec(nsec);
Seems simple enough but there's certain things I want to make sure I have covered. For instance, does this handle the assignment of the tv_sec member variables or is it concerned with only the usec to nsec? ns_to_timespec returns a timespec struct, not just a long value that would be consistent with operating solely on the nsec memeber. Hopefully if the design of the function is consistent then it handles both.

I've yet to look past the header file into the source C file. I'll do that when I get the chance, hopefully in about 30 mins or so.
 
Old 07-06-2009, 06:31 PM   #4
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Original Poster
Rep: Reputation: 15
Checked out the source code and it definitely has me covered. timeval_to_ns is an inline and you can see in time.h it has a tv->tv_sec * NS_PER_TSEC evaluation. ns_to_timespec is in time.c and it is also apparent that it handles the tsec value as well. I'll update if something comes up when I'm testing.
 
  


Reply



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
Using struct timespec from time.h pd9 Linux - Kernel 0 07-26-2008 04:56 PM
Hi all from a new convert! martin_baker LinuxQuestions.org Member Intro 1 04-13-2008 09:27 AM
Trying to convert... Twister512 Slackware 7 11-10-2007 05:49 PM
Trying to Convert GATTACA Slackware 3 11-23-2003 12:40 PM
How to convert? ronobi Linux - Software 3 11-22-2003 02:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:26 PM.

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
Open Source Consulting | Domain Registration