Skip to contentSkip to conversation

Add new timeval_to_sec function

PATCH In review Varun Chandramohan 3 participants 5 messages Aug 20, 2007 – Aug 21, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/4 Add new timeval_to_sec function

A new function for converting timeval to time_t is added in time.h. Its a common function used in different places.

---
 include/linux/time.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
include/linux/time.h +12 -0
index 6a5f503..1faf65c 100644
@@ -149,6 +149,18 @@ static inline s64 timeval_to_ns(const st
 }
 
 /**
+ * timeval_to_sec - Convert timeval to seconds
+ * @tv:         pointer to the timeval variable to be converted
+ *
+ * Returns the seconds representation of timeval parameter.
+ * Note : Here we round up the value. We dont need accuracy.
+ */
+static inline time_t timeval_to_sec(const struct timeval *tv)
+{
+	return (tv->tv_sec + (tv->tv_usec ? 1 : 0));
+}
+
+/**
  * ns_to_timespec - Convert nanoseconds to timespec
  * @nsec:	the nanoseconds value to be converted
  *
Signature · 1 line
1.4.3.4
#1 lore raw
Quoted 17 lines · On Mon, 20 Aug 2007 13:45:36 +0530 Varun Chandramohan…

A new function for converting timeval to time_t is added in time.h. Its a common function used in different places.

---
 include/linux/time.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
include/linux/time.h +1 -0
index 6a5f503..1faf65c 100644
@@ -149,6 +149,18 @@ static inline s64 timeval_to_ns(const st
 }
 
 /**
+ * timeval_to_sec - Convert timeval to seconds

include/linux/time.h static inline s64 timeval_to_ns(const st · line 162

Quoted 10 lines
include/linux/time.h +10 -0
+ * @tv:         pointer to the timeval variable to be converted
+ *
+ * Returns the seconds representation of timeval parameter.
+ * Note : Here we round up the value. We dont need accuracy.
+ */
+static inline time_t timeval_to_sec(const struct timeval *tv)
+{
+	return (tv->tv_sec + (tv->tv_usec ? 1 : 0));
+}
+

Why roundup? Unless there is a requirement in the standard, please just use the timeval seconds. In which case the inline is unneeded.

Signature · 1 line
Stephen Hemminger <[email protected]>
#2 lore raw
Quoted 19 lines · Stephen Hemminger
Quoted 15 lines · On Mon, 20 Aug 2007 13:45:36 +0530 Varun Chandramohan…

A new function for converting timeval to time_t is added in time.h. Its a common function used in different places.

---
 include/linux/time.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
include/linux/time.h +0 -0
index 6a5f503..1faf65c 100644
@@ -149,6 +149,18 @@ static inline s64 timeval_to_ns(const st
 }
 
Quoted 19 lines

include/linux/time.h static inline s64 timeval_to_ns(const st

Quoted 13 lines
include/linux/time.h +11 -0
 /**
+ * timeval_to_sec - Convert timeval to seconds
+ * @tv:         pointer to the timeval variable to be converted
+ *
+ * Returns the seconds representation of timeval parameter.
+ * Note : Here we round up the value. We dont need accuracy.
+ */
+static inline time_t timeval_to_sec(const struct timeval *tv)
+{
+	return (tv->tv_sec + (tv->tv_usec ? 1 : 0));
+}
+
 

Why roundup? Unless there is a requirement in the standard, please just use the timeval seconds. In which case the inline is unneeded.

Thanks for the reply stephen. As you might be aware that this discussion took place sometime ago when i posted my first patch set. Initially it was like this:

return (tv->tv_sec + (tv->tv_usec + 500000)/1000000); Then i got some comments from patrick and oliver. They wanted me to round it up.

So what about rounding up with

return (tv->tv_sec + (tv->tv_usec + 999999)/1000000);

Then on second revision the above was changed to

return tv->tv_sec + (tv->tv_usec ? 1 : 0);

as it would be much faster. Since the timeval is meant for stats purpose we decided not really bother about accuracy. My initial patch actually took only sec value into account, but i was adviced to round up usec to give a better o/p. Is that ok??? Or you still think we should consider only secs?

Regards,
Varun

#3 lore raw

What if the name of the function was more descriptive of what the
function actually does? Maybe timeval_ceil_sec()?

#4 lore raw
Quoted 3 lines · Bob Beers

What if the name of the function was more descriptive of what the
function actually does? Maybe timeval_ceil_sec()?

Looks ok, it can be done if everyone is ok with it.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html