Help with awk

Apologies if this is not a correct forum to ask, but I have a problem with awk (opensuse 11.1), which totally escapes my comprehension. In an awk program, I wanted to convert the time to Unix time (sec since 1970-1-1), and to check, I convert back. I use the date program piped into a variable.

The problem is that it works sometimes, but sometimes not and I do not understand why. I have made a small test program, which essentially consists of 3 lines, and I repeat these lines a number of times with different times.

The first 3 result are ok, but the 4th (and others following) are not. It appears that the results are only ok for times which haven’t been used before, but if the time is given for a second or next time, the result is wrong and the time printed is from a previous time (see for example at the last 6 results). It is the Unix time which incorrect (the first date pipe command), not the back conversion.

I have been looking for days, modifying and simplifying the code, until this test program was made. The program is below.

Any help or suggestion is welcome.

Charles

awk ’
BEGIN{
print
print “-------”
“date -d 10:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

"date -d 23:55 +%s “| getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 00:00 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 10:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 21:37 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 21:39 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 15:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 20:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 21:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 10:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 15:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 20:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 21:15 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

“date -d 00:00 +%s " | getline abs_time
print abs_time
“date -d @” abs_time " +%F” "%T " | getline a; print a

}

END{
}’ < /dev/null

There’s a Programming and Scripting forum for this.

I notice that you give an incomplete time spec, e.g. 10:15. It’s not clear to me whether date will use today or yesterday depending on whether it’s after or before 10:15. I think you really should specify the date as well.

Moved to Programming/Scripting.

General Chit-Chat is explicitly NOT for asking help!

Thanks for putting it in the right forum, I am new to this forum.

To reply to the suggestion, I do not think it is date(1), that is in error. It seems very permissible about the date format given. It defaults to the current day date.

I have tried to do something similar in the shell:

DD=“10:15”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

DD=“23:55”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

DD=“00:00”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

DD=“10:15”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

DD=“21:37”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

DD=“15:15”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

DD=“20:15”
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T

and that gives correct answers. Without any doubt, there will be a work around, but I am still flabbergasted.

Test a bit shorter:

for DD in “10:15” “23:55” “00:00” “10:15” “21:37” “15:15” “20:15”
do
echo $DD
SDD=date -d "$DD" +%s
date -d @$SDD +%F" "%T
done

I think its covered here(Note think I’m an awk noob)

According to POSIX, ‘expression | getline’ is ambiguous

Though I had a brief try with the expression parenthesized, I still got the same result. This has gone so far beyond my comprehension of awk I now step out, but to me you seem to be confirming the above.

I have given up as well and made a work around which. Anyhow, thanks for the replies.

Charles