日付の管理


最初何を言っているのか分からなかった私はダメダメですが、確かに処理中に変わってしまったら困ってしまうわけです。本当は awk にも多値が処理できれば、サクッと一気に取得できるのですが、gawk には strftime() しかありません。
それ以外の awk は srand() の戻り値がエポックタイムからの経過秒数を返す処理系 (またはコンパイル環境) であれば、それを先に取得しておいて後からじっくり料理するのが望ましいのかもしれません。

#! /usr/bin/gawk -f
# strftime.awk - get current date and time
#   input:  none
#   output: return current date and time with 2 methods

BEGIN {

    # ダメな例
    year  = strftime("%Y");
    month = strftime("%m");
    day   = strftime("%d");
    hour  = strftime("%H");
    min   = strftime("%M");
    sec   = strftime("%S");
    print year, month, day, hour, min, sec;

    # 良い例 (1)
    now   = systime();
    year  = strftime("%Y", now);
    month = strftime("%m", now);
    day   = strftime("%d", now);
    hour  = strftime("%H", now);
    min   = strftime("%M", now);
    sec   = strftime("%S", now);
    print year, month, day, hour, min, sec;

    # 良い例 (2)
    now2  = -srand() + srand() + 1;
    year  = strftime("%Y", now2);
    month = strftime("%m", now2);
    day   = strftime("%d", now2);
    hour  = strftime("%H", now2);
    min   = strftime("%M", now2);
    sec   = strftime("%S", now2);
    print year, month, day, hour, min, sec;

    # 良い例 (3)
    now3  = strftime("%Y %m %d %H %M %S");
    print now3;
}

改めて作成してみると、-srand()+srand() だと 1 秒前になってしまいます。なので 1 を足しています。