[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-gawk] proposed new gawk time function strptime()
From: |
Ed Morton |
Subject: |
[bug-gawk] proposed new gawk time function strptime() |
Date: |
Thu, 28 Aug 2014 23:28:12 -0500 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 |
Here's a summary of a couple of posts I put on comp.lang.awk
(https://groups.google.com/d/msg/comp.lang.awk/FXrz0VIvz4A/k-ln4T6MEdUJ) to get
some general feedback but now I'd like to get some feedback from people
familiar with gawk internals.
I was just writing a script in a forum to convert a timestamp to a different
form:
$ cat file
27/Aug/2014:23:58
$ awk -f tst.awk file
Wed, Aug 27, 2014 11:58:00 PM
$ cat tst.awk
{
split($0,t,/[\/:]/)
mthNr = (match("JanFebMarAprMayJunJulAugSepOctNovDec",t[2])+2)/3
secs = mktime(t[3]" "mthNr" "t[1]" "t[4]" "t[5]" 0")
print strftime("%c", secs)
}
when someone else posted a perl script that uses a built in function named
strptime() that takes a string as the first argument and a description of the
contents of that string in terms of time specifiers and returns the number of
seconds. If that function existed in awk the whole of the above code would be
written as:
{ print strftime("%c", strptime($0,"%d/%b/%Y:%H:%M")) }
I know we want to avoid cluttering up the awk language but time conversions are
a VERY common problem and having to write the split()+match() with arithmetic
or populate an array that maps month names to numbers etc. is pretty painful (I
always have to look it up) and that strptime() seems much more like it'd be
filling a glaring hole in the gawk time functions rather than adding on to them.
I think most of us understand the general issues around introducing core
changes to gawk. The general issues though shouldn't stop us from having a
reasonable discussion of a specific feature request so you can then decide if
the benefits of this specific feature outweigh the effort of providing it.
In this case we have a suggestion to provide a function that would avoid a few
lines of fairly gritty hand written code for the very common problem of working
with time stamps in various formats.
The proposed function is well defined, exists in other languages and so is well
known, provides symmetry with an existing gawk function (strftime()), does not
impact any other part of the language, I would imagine is about as easy as it
can get to implement since it already exists in libraries, fits in like a
missing puzzle piece to the existing set of gawk time functions, and the only
impact to existing scripts would be if someone had their own function named
strptime() defined.
I'm not suggesting anything that would change the awk language, or take the
code in a different direction, or even require any effort to define. In short
it seems like it's a lot of pros and close to zero cons for very little effort.
So wrt this specific request - what do you think?
Ed.
- [bug-gawk] proposed new gawk time function strptime(),
Ed Morton <=