mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
!apps: drop redundant casts on tv_sec/tv_nsec and fix printf formats
Now that time_t is unconditionally 64-bit (signed int64_t) and the
struct timespec fields tv_sec / tv_nsec are wide enough on their own,
the explicit (uint64_t)/(int64_t)/(int) casts that used to guard the
multiplications and subtractions in *_us / *_ms / *_ns helpers are no
longer needed. Drop them to keep the timekeeping math readable.
In the same spirit, this commit also normalises the printf-style format
specifiers and casts used to print tv_sec / tv_nsec / tv_usec values.
The prior code was a mix of "%d"/"%u"/"%ld"/"%lu"/"%lld" with matching
(int)/(unsigned long)/(long long) casts; some formats truncated time_t
on 32-bit hosts, others mismatched signedness or width. Replace all
such cases with the portable POSIX-recommended forms:
- tv_sec (time_t, signed, impl-defined width) -> %jd + (intmax_t)
- tv_nsec (long, signed) -> %ld (no cast)
- tv_usec (suseconds_t / long) -> %ld (no cast)
Also drop two stale `(FAR const time_t *)&ts.tv_sec` casts that are
unnecessary now that ts.tv_sec is plain time_t.
Arithmetic-cleanup files (existing scope):
- benchmarks/cyclictest/cyclictest.c: timediff_us()
- benchmarks/sd_bench/sd_bench_main.c: get_time_delta_us()
- examples/oneshot/oneshot_main.c: maxus computation
- examples/watchdog/watchdog_main.c: current_time_ms (x2)
- industry/nxmodbus/nxmb_internal.h: nxmb_util_clock_ms()
- netutils/ntpclient/ntpclient.c: timespec2ntp()
- netutils/ptpd/ptpd.c: ptp_adjtime()
- system/dd/dd_main.c: elapsed accounting
- testing/drivers/drivertest/drivertest_posix_timer.c:
get_timestamp()
- testing/drivers/sd_stress/sd_stress_main.c:get_time_delta()
- testing/sched/getprime/getprime_main.c: elapsed accounting
- testing/sched/pthread_mutex_perf/pthread_mutex_perf.c:
timespec_avg()
Printf-format-fix files (new in this revision):
- examples/adjtime/adjtime_main.c
- examples/charger/charger_main.c
- examples/netpkt/netpkt_ethercat.c
- fsutils/mkfatfs/mkfatfs.c
- graphics/tiff/tiff_initialize.c
- netutils/ptpd/ptpd.c
- nshlib/nsh_timcmds.c
- system/coredump/coredump.c
- system/ptpd/ptpd_main.c
- testing/drivers/drivertest/drivertest_oneshot.c
- testing/mm/kasantest/kasantest.c
- testing/ostest/semtimed.c
- testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
- testing/sched/timerjitter/timerjitter.c
- testing/testsuites/kernel/time/cases/clock_test_clock01.c
- testing/testsuites/kernel/time/cases/clock_test_smoke.c
No behavioural change.
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
e9d1141f7c
commit
f9f59bd0f8
32 changed files with 144 additions and 139 deletions
|
|
@ -25,6 +25,7 @@
|
|||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
|
|
@ -149,8 +150,8 @@ int main(int argc, FAR char *argv[])
|
|||
|
||||
parse_args(&delta, argc, argv);
|
||||
|
||||
printf("Delta time is %ld seconds and %ld micro seconds.\n",
|
||||
(long)delta.tv_sec, delta.tv_usec);
|
||||
printf("Delta time is %jd seconds and %ld micro seconds.\n",
|
||||
(intmax_t)delta.tv_sec, delta.tv_usec);
|
||||
|
||||
/* Call adjtime function. */
|
||||
|
||||
|
|
@ -161,8 +162,8 @@ int main(int argc, FAR char *argv[])
|
|||
}
|
||||
else
|
||||
{
|
||||
printf("Returned olddelta is %ld seconds and %ld micro seconds.\n",
|
||||
(long)olddelta.tv_sec, olddelta.tv_usec);
|
||||
printf("Returned olddelta is %jd seconds and %ld micro seconds.\n",
|
||||
(intmax_t)olddelta.tv_sec, olddelta.tv_usec);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
|
|
@ -231,8 +231,8 @@ int main(int argc, FAR char *argv[])
|
|||
}
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
printf("%ju.%06ld: %d mV, %d mA\n",
|
||||
(uintmax_t)tv.tv_sec, tv.tv_usec, voltage, current);
|
||||
printf("%jd.%06ld: %d mV, %d mA\n",
|
||||
(intmax_t)tv.tv_sec, tv.tv_usec, voltage, current);
|
||||
|
||||
close(fd);
|
||||
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
|
@ -118,9 +119,9 @@ int main(int argc, FAR const char *argv[])
|
|||
|
||||
len = recvfrom(sockfd, recvbuff, 100, 0, NULL, NULL);
|
||||
clock_gettime(CLOCK_REALTIME, &recv_time);
|
||||
printf("Data recv: %d bytes, spent time %ld ns\n", len,
|
||||
(recv_time.tv_sec - send_time.tv_sec) * NSEC_PER_SEC +
|
||||
recv_time.tv_nsec - send_time.tv_nsec);
|
||||
printf("Data recv: %d bytes, spent time %jd ns\n", len,
|
||||
(intmax_t)(recv_time.tv_sec - send_time.tv_sec) *
|
||||
NSEC_PER_SEC + recv_time.tv_nsec - send_time.tv_nsec);
|
||||
usleep(1000);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -167,8 +167,8 @@ int main(int argc, FAR char *argv[])
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
maxus = (uint64_t)ts.tv_sec * USEC_PER_SEC +
|
||||
(uint64_t)ts.tv_nsec / NSEC_PER_USEC;
|
||||
maxus = ts.tv_sec * USEC_PER_SEC +
|
||||
ts.tv_nsec / NSEC_PER_USEC;
|
||||
|
||||
printf("Maximum delay is %" PRIu64 "\n", maxus);
|
||||
|
||||
|
|
|
|||
|
|
@ -308,8 +308,7 @@ int main(int argc, FAR char *argv[])
|
|||
/* Get current time to calculate the elapsed time */
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &tnow);
|
||||
current_time_ms = (uint64_t)((tnow.tv_sec * 1000)
|
||||
+ (tnow.tv_nsec / 1000000));
|
||||
current_time_ms = (tnow.tv_sec * 1000) + (tnow.tv_nsec / 1000000);
|
||||
}
|
||||
|
||||
/* Then stop pinging */
|
||||
|
|
@ -345,8 +344,7 @@ int main(int argc, FAR char *argv[])
|
|||
/* Get current time to calculate the elapsed time */
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &tnow);
|
||||
current_time_ms = (uint64_t)((tnow.tv_sec * 1000)
|
||||
+ (tnow.tv_nsec / 1000000));
|
||||
current_time_ms = (tnow.tv_sec * 1000) + (tnow.tv_nsec / 1000000);
|
||||
}
|
||||
|
||||
/* We should not get here */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue