Quote:
Originally Posted by geetalicious
Hi,
I'm trying to process a field
'd/m/yyyy h:mm' to 'yyyy-mm-dd hh:mm:ss' for sql inserts using awk.
|
Assume you have the field in variable
timestamp. First, split it to e.g.
timefield array, setting the seconds part to zero if not specified:
Code:
timefields = split(timestamp, timefield, /[\t\n\v\f\r \/:]+/)
if (timefields < 6)
timefield[6] = 0
Note that it would be a good idea to check you do have at least five
timefields, otherwise the timestamp is garbled. Also, you could check the century, unless you are sure your timestamps have the full year.
Finally, reformat the fields the way you prefer. Note that I use
%.0f instead of
%d for seconds, for proper rounding in case it has a fractional part.
Code:
formattedtime = sprintf("%04d-%02d-%02d %02d:%02d:%02.0f",
timefield[3], timefield[2], timefield[1],
timefield[4], timefield[5], timefield[6])
Hope this helps,