Skip to contentSkip to conversation

misc: ibmasm: Replace timeval with timespec64

PATCH Superseded Amitoj Kaur Chawla 2 participants 2 messages Oct 23, 2015 – Feb 8, 2016 Char and misc drivers

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 misc: ibmasm: Replace timeval with timespec64

This patch replaces timeval with timespec64 as 32 bit 'struct timeval' will not give current time beyond 2038.

The patch changes the code to use ktime_get_real_ts64() which returns a 'struct timespec64' instead of do_gettimeofday() which returns a 'struct timeval'

This patch also alters the format strings in sprintf() for now.tv_sec and now.tv_nsec to incorporate 'long long' on 32 bit architectures and leading zeroes respectively.

---
Changes in v2:
-change format string of now.tv_sec to '%llu' -change format string of now.tv_nsec to '%.08lu'
Changes in v3:
-Replace tv_usec with tv_nsec, error made in v2 -Build tested

 drivers/misc/ibmasm/ibmasm.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/misc/ibmasm/ibmasm.h +5 -3
index 9b08344..82380ae 100644
@@ -34,6 +34,7 @@
 #include <linux/kref.h>
 #include <linux/device.h>
 #include <linux/input.h>
+#include <linux/time64.h>
 
 /* Driver identification */
 #define DRIVER_NAME	"ibmasm"
@@ -53,9 +54,10 @@ extern int ibmasm_debug;
 
 static inline char *get_timestamp(char *buf)
 {
-	struct timeval now;
-	do_gettimeofday(&now);
-	sprintf(buf, "%lu.%lu", now.tv_sec, now.tv_usec);
+	struct timespec64 now;
+	ktime_get_real_ts64(&now);
+	sprintf(buf, "%llu.%.08lu", (long long)now.tv_sec, 
+					now.tv_nsec / NSEC_PER_USEC);
 	return buf;
 }
 
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 40 lines · Amitoj Kaur Chawla

This patch replaces timeval with timespec64 as 32 bit 'struct timeval' will not give current time beyond 2038.

The patch changes the code to use ktime_get_real_ts64() which returns a 'struct timespec64' instead of do_gettimeofday() which returns a 'struct timeval'

This patch also alters the format strings in sprintf() for now.tv_sec and now.tv_nsec to incorporate 'long long' on 32 bit architectures and leading zeroes respectively.

---
Changes in v2:
-change format string of now.tv_sec to '%llu' -change format string of now.tv_nsec to '%.08lu'
Changes in v3:
-Replace tv_usec with tv_nsec, error made in v2 -Build tested

 drivers/misc/ibmasm/ibmasm.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/misc/ibmasm/ibmasm.h +1 -0
index 9b08344..82380ae 100644
@@ -34,6 +34,7 @@
 #include <linux/kref.h>
 #include <linux/device.h>
 #include <linux/input.h>
+#include <linux/time64.h>
 
 /* Driver identification */
 #define DRIVER_NAME	"ibmasm"
@@ -53,9 +54,10 @@ extern int ibmasm_debug;
 
 static inline char *get_timestamp(char *buf)
 {

drivers/misc/ibmasm/ibmasm.h extern int ibmasm_debug; · line 63

Quoted 10 lines
drivers/misc/ibmasm/ibmasm.h +4 -3
-	struct timeval now;
-	do_gettimeofday(&now);
-	sprintf(buf, "%lu.%lu", now.tv_sec, now.tv_usec);
+	struct timespec64 now;
+	ktime_get_real_ts64(&now);
+	sprintf(buf, "%llu.%.08lu", (long long)now.tv_sec, 
+					now.tv_nsec / NSEC_PER_USEC);
 	return buf;
 }
 

Always run your patches through checkpatch so that a maintainer doesn't have to be grumpy and tell you to fix up the obvious checkpatch issues in it...

thanks,

greg k-h