3 ms·
afaik the only way to get epoch with only posix-compliant tools is to use awk: $ awk 'BEGIN {srand(); t = srand() ; print t}' 1651247949 explaination:
by znpy 5y ago
afaik the only way to get epoch with only posix-compliant tools is to use awk:
$ awk 'BEGIN {srand(); t = srand() ; print t}'
1651247949
explaination:
1. srand if not provided with a seed will use the current unix epoch as seed
2. srand will return its previous seed in a new initialization (and we assign it to t)
3. we print t
I learned this via a "Systems we love" talk (https://www.youtube.com/watch?v=IfhMUed9RSE https://www.youtube.com/watch?v=IfhMUed9RSE)
- geocar 5y agopax is POSIX: (: > f;echo "$((0$(pax 2>/dev/null -x ustar -wd -- f|dd 2> /dev/null bs=4 skip=34 count=3|tr -d '\0')))";rm -f f) which might be beneficial to remember if your file server has a better clock than you do. also, if you're actually targeting POSIX, you should make sure you've set PATH="`getconf PATH`" to guarantee you get the POSIX version of utilities.
- schoen 5y agoIt's definitely not POSIX, but a good GNUish way is date +%s Your portable suggestion worked in gawk, nawk, and mawk -- cool!
- znpy 5y ago> Your portable suggestion worked in gawk, nawk, and mawk -- cool! indeed, that's the cool thing about it!
- 1vuio0pswjnm7 5y agoIsn't the c99 utility POSIX-compliant. The c99 utility is an interface to a standard C compilation system.^1 1. https://web.archive.org/web/20201202084055/https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html https://web.archive.org/web/20201202084055/https://pubs.open... cat > epoch.c int printf(const char *__restrict, ...); unsigned int time(unsigned int *tloc); int main(){printf("%u\n",time((unsigned int *)0));} ^D c99 epoch.c ./a.out