!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:
Xiang Xiao 2026-05-11 17:51:35 +08:00 committed by Xiang Xiao
parent e9d1141f7c
commit f9f59bd0f8
32 changed files with 144 additions and 139 deletions

View file

@ -567,7 +567,7 @@ static int32_t ntp_nsecpart(int64_t time)
{
/* Get fraction part converted to nanoseconds. */
return (((int64_t)((uint64_t)time << 32) >> 32) * NSEC_PER_SEC) >> 32;
return (((time << 32) >> 32) * NSEC_PER_SEC) >> 32;
}
/****************************************************************************
@ -589,7 +589,7 @@ static uint64_t timespec2ntp(FAR const struct timespec *ts)
/* Set seconds part. */
ntp_time += (uint64_t)(ts->tv_sec) << 32;
ntp_time += ts->tv_sec << 32;
return ntp_time;
}
@ -647,8 +647,8 @@ static void ntpc_calculate_offset(FAR int64_t *offset, FAR int64_t *delay,
* http://nicolas.aimon.fr/2014/12/05/timesync/
*/
*offset = (int64_t)((remote_recvtime / 2 - local_xmittime / 2) +
(remote_xmittime / 2 - local_recvtime / 2));
*offset = (remote_recvtime / 2 - local_xmittime / 2) +
(remote_xmittime / 2 - local_recvtime / 2);
/* Calculate roundtrip delay. */
@ -707,13 +707,13 @@ static void ntpc_settime(int64_t offset, FAR struct timespec *start_realtime,
diffms_real = curr_realtime.tv_sec - start_realtime->tv_sec;
diffms_real *= 1000;
diffms_real += (int64_t)(curr_realtime.tv_nsec -
start_realtime->tv_nsec) / (1000 * 1000);
diffms_real += (curr_realtime.tv_nsec -
start_realtime->tv_nsec) / (1000 * 1000);
diffms_mono = curr_monotonic.tv_sec - start_monotonic->tv_sec;
diffms_mono *= 1000;
diffms_mono += (int64_t)(curr_monotonic.tv_nsec -
start_monotonic->tv_nsec) / (1000 * 1000);
diffms_mono += (curr_monotonic.tv_nsec -
start_monotonic->tv_nsec) / (1000 * 1000);
/* Detect if real-time has been altered by other task. */
@ -1489,14 +1489,14 @@ static int ntpc_daemon(int argc, FAR char **argv)
if (offset1 > 0 && offset2 > 0)
{
offset = ((uint64_t)offset1 + (uint64_t)offset2) / 2;
offset = (offset1 + offset2) / 2;
}
else if (offset1 < 0 && offset2 < 0)
{
offset1 = -offset1;
offset2 = -offset2;
offset = ((uint64_t)offset1 + (uint64_t)offset2) / 2;
offset = (offset1 + offset2) / 2;
offset = -offset;
}

View file

@ -425,7 +425,7 @@ static int ptp_adjtime(FAR struct ptp_state_s *state, int64_t delta_ns,
struct timeval delta;
delta.tv_sec = delta_ns / NSEC_PER_SEC;
delta_ns -= (int64_t)delta.tv_sec * NSEC_PER_SEC;
delta_ns -= delta.tv_sec * NSEC_PER_SEC;
delta.tv_usec = delta_ns / NSEC_PER_USEC;
return adjtime(&delta, NULL);
}
@ -1030,11 +1030,11 @@ static int ptp_update_local_clock(FAR struct ptp_state_s *state,
const int64_t adj_limit_ns = CONFIG_NETUTILS_PTPD_SETTIME_THRESHOLD_MS
* (int64_t)NSEC_PER_MSEC;
ptpinfo("Local time: %lld.%09ld, remote time %lld.%09ld\n",
(long long)local_timestamp->tv_sec,
(long)local_timestamp->tv_nsec,
(long long)remote_timestamp->tv_sec,
(long)remote_timestamp->tv_nsec);
ptpinfo("Local time: %jd.%09ld, remote time %jd.%09ld\n",
(intmax_t)local_timestamp->tv_sec,
local_timestamp->tv_nsec,
(intmax_t)remote_timestamp->tv_sec,
remote_timestamp->tv_nsec);
delta_ns = timespec_delta_ns(remote_timestamp, local_timestamp);
delta_ns += state->path_delay_ns;
@ -1062,8 +1062,8 @@ static int ptp_update_local_clock(FAR struct ptp_state_s *state,
if (ret == OK)
{
ptpinfo("Jumped to timestamp %lld.%09ld s\n",
(long long)new_time.tv_sec, (long)new_time.tv_nsec);
ptpinfo("Jumped to timestamp %jd.%09ld s\n",
(intmax_t)new_time.tv_sec, new_time.tv_nsec);
}
else
{
@ -1241,7 +1241,7 @@ static void ptp_add_correction_time(FAR const uint8_t *correction,
| (((uint64_t)correction[4]) << 8)
| (((uint64_t)correction[5]) << 0);
ptpinfo("correction before: %lld.%09ld\n", (long long)ts->tv_sec,
ptpinfo("correction before: %jd.%09ld\n", (intmax_t)ts->tv_sec,
ts->tv_nsec);
ts->tv_sec += correction_time / NSEC_PER_SEC;
@ -1252,7 +1252,7 @@ static void ptp_add_correction_time(FAR const uint8_t *correction,
ts->tv_sec += 1;
}
ptpinfo("correction after: %lld.%09ld\n", (long long)ts->tv_sec,
ptpinfo("correction after: %jd.%09ld\n", (intmax_t)ts->tv_sec,
ts->tv_nsec);
}