nuttx-apps/examples/adjtime/adjtime_main.c
Xiang Xiao f9f59bd0f8 !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>
2026-05-22 13:38:25 +08:00

170 lines
4.7 KiB
C

/****************************************************************************
* apps/examples/adjtime/adjtime_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pwm_help
****************************************************************************/
static void adjtime_help(void)
{
printf("Usage: adjtime [OPTIONS]\n");
printf(" [-s delta->tv_sec] sets delta in seconds.\n");
printf(" [-u delta->tv_usec] sets delta in micro seconds.\n");
printf(" [-h] shows this message and exits\n");
printf("Both delta->tv_sec and delta->tv_usec are set to 0 by default\n");
}
/****************************************************************************
* Name: arg_string
****************************************************************************/
static int arg_string(FAR char **arg, FAR char **value)
{
FAR char *ptr = *arg;
if (ptr[2] == '\0')
{
*value = arg[1];
return 2;
}
else
{
*value = &ptr[2];
return 1;
}
}
/****************************************************************************
* Name: arg_decimal
****************************************************************************/
static int arg_decimal(FAR char **arg, FAR unsigned long *value)
{
FAR char *string;
int ret;
ret = arg_string(arg, &string);
*value = strtoul(string, NULL, 10);
return ret;
}
/****************************************************************************
* Name: parse_args
****************************************************************************/
static void parse_args(FAR struct timeval *delta, int argc,
FAR char **argv)
{
FAR char *ptr;
unsigned long value;
int index;
int nargs;
for (index = 1; index < argc; )
{
ptr = argv[index];
if (ptr[0] != '-')
{
printf("Invalid options format: %s\n", ptr);
exit(0);
}
switch (ptr[1])
{
case 's':
nargs = arg_decimal(&argv[index], &value);
delta->tv_sec = value;
index += nargs;
break;
case 'u':
nargs = arg_decimal(&argv[index], &value);
delta->tv_usec = value;
index += nargs;
break;
case 'h':
adjtime_help();
exit(0);
default:
printf("Unsupported option: %s\n", ptr);
adjtime_help();
exit(1);
}
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* adjtime_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct timeval delta;
struct timeval olddelta;
int ret;
/* Set delta between real time and system tick. */
delta.tv_sec = 0;
delta.tv_usec = 0;
parse_args(&delta, argc, argv);
printf("Delta time is %jd seconds and %ld micro seconds.\n",
(intmax_t)delta.tv_sec, delta.tv_usec);
/* Call adjtime function. */
ret = adjtime(&delta, &olddelta);
if (ret < 0)
{
printf("ERROR: adjtime() failed: %d\n", ret);
}
else
{
printf("Returned olddelta is %jd seconds and %ld micro seconds.\n",
(intmax_t)olddelta.tv_sec, olddelta.tv_usec);
}
return ret;
}