Skip to content

Commit c6bb003

Browse files
committed
xtime: Add support for @epochseconds timestamps
1 parent 3da7fe8 commit c6bb003

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

src/xtime.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ static int xgetpart(const char **str, size_t n, int *result) {
206206
}
207207

208208
int xgetdate(const char *str, struct timespec *result) {
209+
// Handle @epochseconds
210+
if (str[0] == '@') {
211+
long long value;
212+
if (xstrtoll(str + 1, NULL, 10, &value) != 0) {
213+
goto error;
214+
}
215+
216+
time_t time = (time_t)value;
217+
if ((long long)time != value) {
218+
errno = ERANGE;
219+
goto error;
220+
}
221+
222+
result->tv_sec = time;
223+
goto done;
224+
}
225+
209226
struct tm tm = {
210227
.tm_isdst = -1,
211228
};
@@ -324,6 +341,7 @@ int xgetdate(const char *str, struct timespec *result) {
324341
}
325342
}
326343

344+
done:
327345
result->tv_nsec = 0;
328346
return 0;
329347

tests/gnu/used.sh

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
1-
iso8601() {
2-
printf '%04d-%02d-%02dT%02d:%02d:%02d\n' "$@"
3-
}
4-
51
cd "$TEST"
62

7-
now=$(date '+%Y-%m-%dT%H:%M:%S')
8-
9-
# Parse the current date
10-
[[ "$now" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]] || fail
11-
# Treat leading zeros as decimal, not octal
12-
YYYY=$((10#${BASH_REMATCH[1]}))
13-
MM=$((10#${BASH_REMATCH[2]}))
14-
DD=$((10#${BASH_REMATCH[3]}))
15-
hh=$((10#${BASH_REMATCH[4]}))
16-
mm=$((10#${BASH_REMATCH[5]}))
17-
ss=$((10#${BASH_REMATCH[6]}))
3+
now=$(epoch_time)
184

195
# -used is always false if atime < ctime
20-
yesterday=$(iso8601 $YYYY $MM $((DD - 1)) $hh $mm $ss)
21-
"$XTOUCH" -at "$yesterday" yesterday
6+
"$XTOUCH" -at "@$((now - 60 * 60 * 24))" yesterday
227

238
# -used rounds up
24-
tomorrow=$(iso8601 $YYYY $MM $DD $((hh + 1)) $mm $ss)
25-
"$XTOUCH" -at "$tomorrow" tomorrow
9+
"$XTOUCH" -at "@$((now + 60 * 60))" tomorrow
2610

27-
dayafter=$(iso8601 $YYYY $MM $((DD + 1)) $((hh + 1)) $mm $ss)
28-
"$XTOUCH" -at "$dayafter" dayafter
11+
"$XTOUCH" -at "@$((now + 60 * 60 * 25))" dayafter
2912

30-
nextweek=$(iso8601 $YYYY $MM $((DD + 6)) $((hh + 1)) $mm $ss)
31-
"$XTOUCH" -at "$nextweek" nextweek
13+
"$XTOUCH" -at "@$((now + 60 * 60 * (24 * 6 + 1)))" nextweek
3214

33-
nextyear=$(iso8601 $((YYYY + 1)) $MM $DD $hh $mm $ss)
34-
"$XTOUCH" -at "$nextyear" nextyear
15+
"$XTOUCH" -at "@$((now + 60 * 60 * 24 * 365))" nextyear
3516

3617
bfs_diff -mindepth 1 \
3718
-a -used 1 -printf '-used 1: %p\n' \

tests/run.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ make_xattrs() {
410410
esac
411411
}
412412

413+
# Get the Unix epoch time in seconds
414+
epoch_time() {
415+
# https://stackoverflow.com/a/12746260/502399
416+
awk 'BEGIN { srand(); print srand(); }'
417+
}
418+
413419
## Snapshot testing
414420

415421
# Return value when a difference is detected

0 commit comments

Comments
 (0)