aboutsummaryrefslogtreecommitdiff
path: root/manual/examples
diff options
context:
space:
mode:
Diffstat (limited to 'manual/examples')
-rw-r--r--manual/examples/strftim.c30
-rw-r--r--manual/examples/timespec_subtract.c36
-rw-r--r--manual/examples/timeval_subtract.c44
3 files changed, 52 insertions, 58 deletions
diff --git a/manual/examples/strftim.c b/manual/examples/strftim.c
index 304981d..6e1e047 100644
--- a/manual/examples/strftim.c
+++ b/manual/examples/strftim.c
@@ -18,30 +18,32 @@
#include <time.h>
#include <stdio.h>
-#define SIZE 256
-
int
main (void)
{
- char buffer[SIZE];
- time_t curtime;
- struct tm *loctime;
+ /* This buffer is big enough that the strftime calls
+ below cannot possibly exhaust it. */
+ char buf[256];
/* Get the current time. */
- curtime = time (NULL);
+ time_t curtime = time (NULL);
/* Convert it to local time representation. */
- loctime = localtime (&curtime);
+ struct tm *lt = localtime (&curtime);
+ if (!lt)
+ return 1;
- /* Print out the date and time in the standard format. */
- fputs (asctime (loctime), stdout);
+ /* Print the date and time in a simple format
+ that is independent of locale. */
+ strftime (buf, sizeof buf, "%Y-%m-%d %H:%M:%S", lt);
+ puts (buf);
/*@group*/
- /* Print it out in a nice format. */
- strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
- fputs (buffer, stdout);
- strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
- fputs (buffer, stdout);
+ /* Print it in a nicer English format. */
+ strftime (buf, sizeof buf, "Today is %A, %B %d.", lt);
+ puts (buf);
+ strftime (buf, sizeof buf, "The time is %I:%M %p.", lt);
+ puts (buf);
return 0;
}
diff --git a/manual/examples/timespec_subtract.c b/manual/examples/timespec_subtract.c
new file mode 100644
index 0000000..380d173
--- /dev/null
+++ b/manual/examples/timespec_subtract.c
@@ -0,0 +1,36 @@
+/* struct timespec subtraction.
+ Copyright (C) 1991-2024 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <https://www.gnu.org/licenses/>.
+*/
+#include <stdckdint.h>
+#include <time.h>
+
+/* Put into *R the difference between X and Y.
+ Return true if overflow occurs, false otherwise. */
+
+bool
+timespec_subtract (struct timespec *r,
+ struct timespec x, struct timespec y)
+{
+ /* Compute nanoseconds, setting @var{borrow} to 1, 0, or -1
+ for propagation into seconds. */
+ long int nsec_diff = x.tv_nsec - y.tv_nsec;
+ int borrow = (nsec_diff < 0) - ! (nsec_diff < 1000000000);
+ r->tv_nsec = nsec_diff + 1000000000 * borrow;
+
+ /* Compute seconds, returning true if this overflows. */
+ bool v = ckd_sub (&r->tv_sec, x.tv_sec, y.tv_sec);
+ return v ^ ckd_sub (&r->tv_sec, r->tv_sec, borrow);
+}
diff --git a/manual/examples/timeval_subtract.c b/manual/examples/timeval_subtract.c
deleted file mode 100644
index 20c2463..0000000
--- a/manual/examples/timeval_subtract.c
+++ /dev/null
@@ -1,44 +0,0 @@
-/* struct timeval subtraction.
- Copyright (C) 1991-2024 Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, see <https://www.gnu.org/licenses/>.
-*/
-
-/* Subtract the `struct timeval' values X and Y,
- storing the result in RESULT.
- Return 1 if the difference is negative, otherwise 0. */
-
-int
-timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
-{
- /* Perform the carry for the later subtraction by updating @var{y}. */
- if (x->tv_usec < y->tv_usec) {
- int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
- y->tv_usec -= 1000000 * nsec;
- y->tv_sec += nsec;
- }
- if (x->tv_usec - y->tv_usec > 1000000) {
- int nsec = (x->tv_usec - y->tv_usec) / 1000000;
- y->tv_usec += 1000000 * nsec;
- y->tv_sec -= nsec;
- }
-
- /* Compute the time remaining to wait.
- @code{tv_usec} is certainly positive. */
- result->tv_sec = x->tv_sec - y->tv_sec;
- result->tv_usec = x->tv_usec - y->tv_usec;
-
- /* Return 1 if result is negative. */
- return x->tv_sec < y->tv_sec;
-}