I am currently learning the zsh and now I wanted to use strftime
but i get:
zsh: command not found: strftime
I think I'm doin' something wrong, since I see people using that function all the time in their dotfiles.
I am currently learning the zsh and now I wanted to use strftime
but i get:
zsh: command not found: strftime
I think I'm doin' something wrong, since I see people using that function all the time in their dotfiles.
strftime
is defined in a zsh
module called datetime
. The zmodload
command loads modules from a colon-separated list of directories in $module_path
.
On my system:
% echo $module_path
/usr/lib/zsh/4.3.10
% ls -l $module_path
drwxr-xr-x 3 root root 4096 2009-11-05 01:03 zsh
% ls -l $module_path/zsh/datetime*
-rw-r--r-- 1 root root 9828 2009-09-10 02:45 /usr/lib/zsh/4.3.10/zsh/datetime.so
% type -a strftime
strftime not found
% zmodload zsh/datetime
% strftime %Y-%m-%d 1271603087
2010-04-18
% type -a strftime
strftime is a shell builtin
% /bin/date -d @1271603087 +%Y-%m-%d
2010-04-18
For anyone who arrives here via Google, there's now a way to do this that doesn't require a zmodload
. (I use it to detect when .zshrc is slow due to resource contention, so I don't want it to add new filesystem requests.)
${(%):-"%D{STRFTIME_STRING_HERE}"}
The gist of it is as follows:
${VAR:-DEFAULT}
parameter substitution without a VAR
.(%)
before VAR
enables expansion of the substitution tokens used in PS1
and friends. (This is a very powerful feature because it also grants access to things like %F{green}
. See "expansion of prompt sequences" in man zshmisc
for more.)%D{...}
invokes strftime(3)
.I find %D{%s}
particularly useful because %s
retrieves the "seconds since the epoch" timestamp, which you can then do arithmetic on.
(Just be aware that, while %s
is supported on Linux, AIX, Illumos, MINIX, OSX, and all BSD-family systems, it's not part of POSIX and isn't listed as supported in the online strftime
documentation for Solaris, HP-UX, UnixWare, OpenServer, or the Windows libraries Cygwin and MinGW depend on.)
strftime
is a C function, I believe date
has all the same functionality on the command line.
See man date
for more.
One uses the --date=STRING
option to specify a date to format instead of getting current time.
© 2022 - 2024 — McMap. All rights reserved.