awk: mktime returns -1

Shuvo Shams picture Shuvo Shams · Mar 19, 2012 · Viewed 8.4k times · Source

Can someone help me understand why I get "-1" returned by mktime in the following code. Thanks.

#!/usr/local/bin/bash
f_name="crap.stat"
S_Date="2012-02-10"
E_Date="2012-02-13"

gawk -F '\t' -v s_date="$S_Date" -v e_date="$E_Date" 'BEGIN {s_time = mktime(s_date);e_time = mktime(e_date);print s_time, e_time}' $f_name

Answer

Birei picture Birei · Mar 19, 2012

You have to use a specific format for that function. Here your code fixed:

#!/usr/local/bin/bash
f_name="crap.stat"
S_Date="2012-02-10"
E_Date="2012-02-13"

gawk -F '\t' -v s_date="$S_Date" -v e_date="$E_Date" '
  BEGIN { 
    gsub( /-/, " ", s_date ); 
    s_date = s_date " " 00 " " 00 " " 00; 
    s_time = mktime(s_date);
    gsub( /-/, " ", e_date ); 
    e_date = e_date " " 00 " " 00 " " 00;
    e_time = mktime(e_date);
    print s_time, e_time
  }
' $f_name

Result:

1328828400 1329087600

It must be next format: YYYY MM DD HH MM SS[ DST], so I replace - with spaces and append zeros for the time.