goawk
goawk copied to clipboard
Add Gawk built-in functions such as mktime and strftime
It would be useful to have the standard build-in functions for arithmetic, string and time manipulation.
https://www.gnu.org/software/gawk/manual/html_node/Built_002din.html
Particularly in my use case I really miss mktime
and strftime
Hi @microo8, GoAWK has never aspired to be compatible with Gawk and its vast suite of built-in functions, but rather with the much smaller POSIX spec. So just to set expectations, I'm probably not going to go down the track of adding Gawk's "standard library". However, I'm going to leave this issue open for a while to see if others have any comments.
To do what you're trying to do in a POSIX-compatible way you could shell out and use redirection, for example:
$ awk 'BEGIN { "date" | getline x; print "Date:", x }'
Date: Sat 16 Jul 2022 09:52:46 NZST
That's fair. I didn't realize that these where not the POSIX standard, but just GNU additions. I think that then you don't add them.
For my use case it wouldn't be good to allow calling the shell. Maybe
I'll just write these time functions myself and just add them trough
interp.Config.Funcs
.
Ah yes, if you're using GoAWK as a Go package, that's an excellent use for Config.Funcs
-- nice! What is your use case, out of interest, and is it open source?
It's not open source :/ It's a server that grabs files from a remote server and parses some values from them, then sends them further.
And instead of creating all kinds of parsers, for all kinds file formats, there would be just one - awk parser that can have the awk script saved, and then run on the specific files.
The parser (and also the script for it) is created trough a web form, then it runs on the server, when a file with a specific extension is encountered. So it wouldn't be a good idea to let them shell out :)
That sounds really neat, good work!