TdC
Trem's hunky sex love muffin
- Joined
- Dec 20, 2003
- Messages
- 30,925
I've got an irksome issue. I tend to use awk for textual manipulation and stuff. Now I've been asked to get it to calculate things. Well and good, but it's not doing what I expect. Someone care to help me out?
I have this in KSH:
it works, or seems to heheh.
I've turned it into this in nawk:
the input number in both cases is [highlight]1121351040[/highlight]. the KSH computes it thusly: 2005:14:7:14:24:0 and it's correct.
the nawk code computes it like so: 1820.74:0.2:-2598.63:4.04235e+09:0:0 which is pissing me off. anyone spot the mistake?
I have this in KSH:
Code:
days=$(( secs / 86400 ))
secs=$(( secs - 86400 * days ))
if (( secs < 0 )); then
days=$(( days - 1 ))
secs=$(( secs + 86400 ))
fi
j=$(( days/146097 ))
i=$(( days - 146097*j ))
a=$(( i + 719468 ))
b=$(( ( 4*a + 3 )/146097 ))
c=$(( a - ( 146097*b )/4 ))
d=$(( ( 4*c + 3 )/1461 ))
e=$(( c - ( 1461*d )/4 ))
m=$(( ( 5*e + 2 )/153 ))
year=$(( 400*j + 100*b + d + m/10 ))
mon=$(( m + 3 - 12*( m/10 ) ))
day=$(( e - ( 153*m + 2 )/5 + 1 ))
# Time of day.
hour=$(( secs / 3600 ))
secs=$(( secs - 3600 * hour ))
min=$(( secs / 60 ))
sec=$(( secs - 60 * min ))
echo "$year:$day:$mon:$hour:$min:$sec"
it works, or seems to heheh.
I've turned it into this in nawk:
Code:
nawk {
SECS=$1
DAYS=(SECS / 86400)
SECS=((SECS - 86400) * DAYS)
if (SECS < 0) {
DAYS=(DAYS - 1)
SECS=(SECS + 86400)}
j= (DAYS / 146097)
i= ((DAYS - 146097)*j)
a= (i+719468)
b= (((4*a)+3)/146097)
c= ((a-(146097*b))/4)
d= ((4*c + 3)/1461)
e= ((c -(1461*d))/4)
m= ((5*e + 2)/153)
year= ((400*j) + (100*b) + d + (m/10))
mon= (m + 3 - 12 *(m/10))
day= (e - (153*m+2)/5 + 1)
hour= (SECS / 3600)
secs= (SECS - 3600 * hour)
min= (secs / 60)
sec= (secs - 60 * min)
print year":"day":"mon":"hour":"min":"sec
}
the input number in both cases is [highlight]1121351040[/highlight]. the KSH computes it thusly: 2005:14:7:14:24:0 and it's correct.
the nawk code computes it like so: 1820.74:0.2:-2598.63:4.04235e+09:0:0 which is pissing me off. anyone spot the mistake?