Skip to content

Commit

Permalink
xtime: Add support for @epochseconds timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator committed Jun 4, 2024
1 parent 3da7fe8 commit c6bb003
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
18 changes: 18 additions & 0 deletions src/xtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ static int xgetpart(const char **str, size_t n, int *result) {
}

int xgetdate(const char *str, struct timespec *result) {
// Handle @epochseconds
if (str[0] == '@') {
long long value;
if (xstrtoll(str + 1, NULL, 10, &value) != 0) {
goto error;
}

time_t time = (time_t)value;
if ((long long)time != value) {
errno = ERANGE;
goto error;
}

result->tv_sec = time;
goto done;
}

struct tm tm = {
.tm_isdst = -1,
};
Expand Down Expand Up @@ -324,6 +341,7 @@ int xgetdate(const char *str, struct timespec *result) {
}
}

done:
result->tv_nsec = 0;
return 0;

Expand Down
31 changes: 6 additions & 25 deletions tests/gnu/used.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
iso8601() {
printf '%04d-%02d-%02dT%02d:%02d:%02d\n' "$@"
}

cd "$TEST"

now=$(date '+%Y-%m-%dT%H:%M:%S')

# Parse the current date
[[ "$now" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]] || fail
# Treat leading zeros as decimal, not octal
YYYY=$((10#${BASH_REMATCH[1]}))
MM=$((10#${BASH_REMATCH[2]}))
DD=$((10#${BASH_REMATCH[3]}))
hh=$((10#${BASH_REMATCH[4]}))
mm=$((10#${BASH_REMATCH[5]}))
ss=$((10#${BASH_REMATCH[6]}))
now=$(epoch_time)

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

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

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

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

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

bfs_diff -mindepth 1 \
-a -used 1 -printf '-used 1: %p\n' \
Expand Down
6 changes: 6 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ make_xattrs() {
esac
}

# Get the Unix epoch time in seconds
epoch_time() {
# https://stackoverflow.com/a/12746260/502399
awk 'BEGIN { srand(); print srand(); }'
}

## Snapshot testing

# Return value when a difference is detected
Expand Down

0 comments on commit c6bb003

Please sign in to comment.