現在の日付から "xyz/[int][int]/[int][int][int][int]" 形式の日付を減算するには?

現在の日付から

想像する

私のコンテナの最後の配布日は次の形式です。

月/日/年または2024年2月11日。

最終配布日の出力方法を変更することはできません。

現在の日付から最後の配布日を引いた後、最後の配布日が2ヶ月以上経過した場合は、ファイルに書きたいと思います。

たとえば、コンテナ1 - 最後 - deploy.txt:

last deployed: jan/01/2024

これらのファイルはいくつかあります。

問題は、月が文字で提供されることです。正規表現を使用して日付を取得できます。

私が考えた1つの解決策は、次のように月を事前に置くことです。

dict=(
  ['jan']=1
  ['feb']=2
)

しかし、もっと良い方法があるでしょう。

アルファベットの月を整数に変換できますか?

これはmacOSのbash用であり、bash 3ソリューションが望ましいです。

ベストアンサー1

MacOS date- 日付文字列をエポックから秒に変換する

dateこのコマンドは、libc関数を介して文字列形式の月を理解できますstrptime(3)。私はmacosがないので答えをテストすることはできませんが、macosによるとdate 男性ページ:

date [-jRu] -f input_fmt new_date [+output_fmt]

[...]

   -f      Use input_fmt as the format string to parse the new_date provided rather than using the
           default [[[mm]dd]HH]MM[[cc]yy][.ss] format.  Parsing is done using strptime(3).

これで、マニュアルページで入力日をフォーマットする方法を知ることができます。strptime(3):

       %b or %B or %h
              The month name according to the current locale,  in  abbreviated
              form or the full name.

       %d or %e
              The day of month (1-31).

       %Y     The year, including century (for example, 1991).

したがって、これによれば、入力形式は次のようになります"%b/%e/%Y"

次に、次のフラグを使用する必要がありますdate

   -j      Do not try to set the date.  This allows you to use the -f flag in addition to the + option
           to convert one date format to another.  Note that any date or time components unspecified
           by the -f format string take their values from the current time.

最後に、結果から現在時刻を減算できるようにしたいので、+出力_fmtする:

       %s     The number of seconds since the Epoch, 1970-01-01 00:00:00
              +0000 (UTC).  Leap seconds are not counted unless leap
              second support is available.

したがって、日付形式をepochに変換する結果コマンドは次のようになります。

$ DATE="jan/01/2024"
$ date -jf "%b/%e/%Y" $DATE +%s
1704060000

繰り返しますが、Macosがないため、これを確認するためにテストすることはできません。また、小文字の月を許可するかどうかはわかりませんが、月を大文字に置き換える必要がある場合は、次のよう$DATEに置き換えることができます。${DATE^}

完全性のために、次のコマンドを使用して同じことを行う方法も示します。

GNU date- 日付文字列をエポックから秒に変換する

dateGNUバージョンはより多くの自由を提供するので、より簡単です。

それから男性ページ:

       The --date=STRING is a mostly free format human readable date
       string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29
       16:21:42" or even "next Thursday".  A date string may contain
       items indicating calendar date, time of day, time zone, day of
       week, relative time, relative date, and numbers.  An empty string
       indicates the beginning of the day.  The date string format is
       more complex than is easily documented here but is fully
       described in the info documentation.

dateしたがって、マイコンピュータでテストした結果、コマンドが理解できるように入力からスラッシュを削除する必要があります。

$ DATE="jan/01/2024"
$ echo ${DATE//\// }
jan 01 2024
$ date --date "${DATE//\// }"  +%s
1704060000

現在の日付から減算

これで、元の日付の新起源時間を持つようになりました。ただ変数に保存するだけです。たとえば、MacOSでは次のようになります。

$ ORIG_DATE=$(date -jf "%b/%e/%Y" $DATE +%s)

エポック以降の現在の日付を秒単位で表示するには、次の手順を実行します。

$ date +%s
1704965965

違いを秒単位で計算する方法は簡単です。

$ echo $(( $(date +%s ) - $ORIG_DATE ))
905967

おすすめ記事