Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F165557691
D44905.id137503.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
7 KB
Referenced Files
None
Subscribers
None
D44905.id137503.diff
View Options
diff --git a/bin/date/date.1 b/bin/date/date.1
--- a/bin/date/date.1
+++ b/bin/date/date.1
@@ -29,7 +29,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd May 19, 2023
+.Dd April 22, 2024
.Dt DATE 1
.Os
.Sh NAME
@@ -141,17 +141,19 @@
.Cm date ,
.Cm hours ,
.Cm minutes ,
+.Cm seconds ,
and
-.Cm seconds .
+.Cm ns No Pq for nanoseconds .
The date and time is formatted to the specified precision.
When
.Ar FMT
is
.Cm hours
-(or the more precise
-.Cm minutes
+.Po or the more precise
+.Cm minutes ,
+.Cm seconds ,
or
-.Cm seconds ) ,
+.Cm ns Pc ,
the
.St -iso8601
format includes the timezone.
@@ -325,7 +327,9 @@
The format string may contain any of the conversion specifications
described in the
.Xr strftime 3
-manual page, as well as any arbitrary text.
+manual page and
+.Ql %N
+for nanoseconds, as well as any arbitrary text.
A newline
.Pq Ql \en
character is always output after the characters specified by
@@ -581,6 +585,12 @@
.Fl I
flag is compatible with
.St -iso8601 .
+.Pp
+The
+.Ql %N
+conversion specification for nanoseconds is a non-standard extension
+It is compatible with GNU date's
+.Ql %N .
.Sh HISTORY
A
.Nm
@@ -599,3 +609,8 @@
.Fl I
flag was added in
.Fx 12.0 .
+.Pp
+The
+.Ql %N
+conversion specification was added in
+.Fx 15.0 .
diff --git a/bin/date/date.c b/bin/date/date.c
--- a/bin/date/date.c
+++ b/bin/date/date.c
@@ -50,7 +50,7 @@
#define TM_YEAR_BASE 1900
#endif
-static time_t tval;
+static struct timespec tspec;
static void badformat(void);
static void iso8601_usage(const char *) __dead2;
@@ -58,6 +58,8 @@
static void printdate(const char *);
static void printisodate(struct tm *);
static void setthetime(const char *, const char *, int);
+static size_t extendedstrftime(char * __restrict, size_t,
+ const char * __restrict, const struct tm * __restrict);
static void usage(void) __dead2;
static const struct iso8601_fmt {
@@ -68,6 +70,7 @@
{ "hours", "T%H" },
{ "minutes", ":%M" },
{ "seconds", ":%S" },
+ { "ns", ",%N" },
};
static const struct iso8601_fmt *iso8601_selected;
@@ -126,11 +129,12 @@
break;
case 'r': /* user specified seconds */
rflag = 1;
- tval = strtoq(optarg, &tmp, 0);
+ tspec.tv_sec = strtoq(optarg, &tmp, 0);
if (*tmp != 0) {
- if (stat(optarg, &sb) == 0)
- tval = sb.st_mtim.tv_sec;
- else
+ if (stat(optarg, &sb) == 0) {
+ tspec.tv_sec = sb.st_mtim.tv_sec;
+ tspec.tv_nsec = sb.st_mtim.tv_nsec;
+ } else
usage();
}
break;
@@ -149,8 +153,8 @@
argc -= optind;
argv += optind;
- if (!rflag && time(&tval) == -1)
- err(1, "time");
+ if (!rflag && clock_gettime(CLOCK_REALTIME, &tspec) == -1)
+ err(1, "clock_gettime");
format = "%+";
@@ -179,7 +183,7 @@
if (outzone != NULL && setenv("TZ", outzone, 1) != 0)
err(1, "setenv(TZ)");
- lt = localtime(&tval);
+ lt = localtime(&tspec.tv_sec);
if (lt == NULL)
errx(1, "invalid time");
badv = vary_apply(v, lt);
@@ -202,7 +206,7 @@
setlocale(LC_TIME, "C");
- (void)strftime(buf, sizeof(buf), format, lt);
+ (void)extendedstrftime(buf, sizeof(buf), format, lt);
printdate(buf);
}
@@ -219,16 +223,16 @@
printisodate(struct tm *lt)
{
const struct iso8601_fmt *it;
- char fmtbuf[32], buf[32], tzbuf[8];
+ char fmtbuf[64], buf[64], tzbuf[8];
fmtbuf[0] = 0;
for (it = iso8601_fmts; it <= iso8601_selected; it++)
strlcat(fmtbuf, it->format_string, sizeof(fmtbuf));
- (void)strftime(buf, sizeof(buf), fmtbuf, lt);
+ (void)extendedstrftime(buf, sizeof(buf), fmtbuf, lt);
if (iso8601_selected > iso8601_fmts) {
- (void)strftime(tzbuf, sizeof(tzbuf), "%z", lt);
+ (void)extendedstrftime(tzbuf, sizeof(tzbuf), "%z", lt);
memmove(&tzbuf[4], &tzbuf[3], 3);
tzbuf[3] = ':';
strlcat(buf, tzbuf, sizeof(buf));
@@ -244,11 +248,10 @@
{
struct utmpx utx;
struct tm *lt;
- struct timeval tv;
const char *dot, *t;
int century;
- lt = localtime(&tval);
+ lt = localtime(&tspec.tv_sec);
if (lt == NULL)
errx(1, "invalid time");
lt->tm_isdst = -1; /* divine correct DST */
@@ -329,18 +332,17 @@
}
/* convert broken-down time to GMT clock time */
- if ((tval = mktime(lt)) == -1)
+ if ((tspec.tv_sec = mktime(lt)) == -1)
errx(1, "nonexistent time");
+ tspec.tv_nsec = 0;
if (!jflag) {
utx.ut_type = OLD_TIME;
memset(utx.ut_id, 0, sizeof(utx.ut_id));
(void)gettimeofday(&utx.ut_tv, NULL);
pututxline(&utx);
- tv.tv_sec = tval;
- tv.tv_usec = 0;
- if (settimeofday(&tv, NULL) != 0)
- err(1, "settimeofday (timeval)");
+ if (clock_settime(CLOCK_REALTIME, &tspec) != 0)
+ err(1, "clock_settime");
utx.ut_type = NEW_TIME;
(void)gettimeofday(&utx.ut_tv, NULL);
pututxline(&utx);
@@ -351,6 +353,66 @@
}
}
+size_t
+extendedstrftime(char * __restrict s, size_t maxsize,
+ const char * __restrict format, const struct tm * __restrict t)
+{
+ size_t ret;
+ const char * tok;
+ char * newformat;
+ char * oldformat;
+ bool seen_percent;
+ int rc;
+
+ const char *prefix;
+ int prefixlen;
+ const char *suffix;
+
+ seen_percent = false;
+ if (asprintf(&newformat, "%s", format) < 0)
+ err(1, "asprintf");
+ tok = newformat;
+ while (*tok != '\0') {
+ switch (*tok) {
+ case '%':
+ /*
+ * If the previous token was a percent sign,
+ * then there are two percent tokens in a row.
+ */
+ if (seen_percent)
+ seen_percent = false;
+ else
+ seen_percent = true;
+ break;
+ case 'N':
+ if (seen_percent) {
+ oldformat = newformat;
+ prefix = oldformat;
+ prefixlen = tok - oldformat - 1;
+ suffix = tok + 1;
+ rc = asprintf(&newformat,
+ "%.*s%.9ld%s", prefixlen, prefix,
+ tspec.tv_nsec, suffix);
+ free(oldformat);
+ if (rc < 0)
+ err(1, "asprintf");
+ tok = newformat + prefixlen + 9;
+
+ }
+ seen_percent = false;
+ break;
+ default:
+ seen_percent = false;
+ break;
+ }
+ tok++;
+ }
+
+ ret = strftime(s, maxsize, newformat, t);
+ free(newformat);
+ return (ret);
+}
+
static void
badformat(void)
{
diff --git a/bin/date/tests/format_string_test.sh b/bin/date/tests/format_string_test.sh
--- a/bin/date/tests/format_string_test.sh
+++ b/bin/date/tests/format_string_test.sh
@@ -30,6 +30,27 @@
date -r ${TEST2} +%${format_string}
}
+atf_test_case flag_r_file_test
+flag_r_file_test_body()
+{
+ cat <<-EOF >> check_mtime.c
+ #include <sys/stat.h>
+ #include <stdio.h>
+
+ int main(int argc, char *argv[]) {
+ struct stat sb;
+ stat(argv[1], &sb);
+ printf("%ld,%.9ld\n", sb.st_mtim.tv_sec, sb.st_mtim.tv_nsec);
+ return (0);
+ }
+EOF
+ cc check_mtime.c -o check_mtime
+ touch testfile
+ mtime="$(./check_mtime ./testfile)"
+ atf_check -o "inline:${mtime}\n" \
+ date -r testfile +%s,%N
+}
+
format_string_test()
{
local desc exp_output_1 exp_output_2 flag
@@ -98,6 +119,8 @@
atf_init_test_cases()
{
+ atf_add_test_case flag_r_file_test
+
format_string_test A A Saturday Monday
format_string_test a a Sat Mon
format_string_test B B February November
@@ -118,6 +141,7 @@
format_string_test l l " 7" " 9"
format_string_test M M 04 20
format_string_test m m 02 11
+ format_string_test N N 000000000 000000000
format_string_test p p AM PM
format_string_test R R 07:04 21:20
format_string_test r r "07:04:03 AM" "09:20:00 PM"
@@ -143,6 +167,5 @@
iso8601_string_test hours hours "" "1970-02-07T07+00:00" "2001-11-12T21+00:00"
iso8601_string_test minutes minutes "" "1970-02-07T07:04+00:00" "2001-11-12T21:20+00:00"
iso8601_string_test seconds seconds "" "1970-02-07T07:04:03+00:00" "2001-11-12T21:20:00+00:00"
- # BSD date(1) does not support fractional seconds at this time.
- #iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00"
+ iso8601_string_test ns ns "" "1970-02-07T07:04:03,000000000+00:00" "2001-11-12T21:20:00,000000000+00:00"
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Aug 9, 4:17 PM (18 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
36282081
Default Alt Text
D44905.id137503.diff (7 KB)
Attached To
Mode
D44905: date: Add support for nanoseconds
Attached
Detach File
Event Timeline
Log In to Comment