UNIXの日付と秒の変換

UNIXの日付と秒の変換

次の形式で時間を提供する必要があります。

2019-02-08T19:24:30.220Zこれにより、与えられた日付と現在の日付の間の日数を出力する必要があります。

与えられた日付=2019-02-08T19:24:30.220Z 現在の日付=2019-02-20T19:24:30.220Z

出力 =12

ベストアンサー1

ksh93通常はAIXやSolarisなどの商用SysVベースのuniceにデフォルトでインストールされています)これは、/bin/shSolaris 11以降でも発生します。

date=2019-02-08T19:24:30.220Z
export LC_ALL=C # to make sure the decimal radix is "."
then_in_seconds=$(printf '%(%s.%N)T\n' "$date")
now_in_seconds=$(printf '%(%s.%N)T\n' now)
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"

2019-02-20T11:17:30Zでこれは私に次のことを与えます:

Result: 11.6618110817684377

差が整数になるようにするには、C のように$((f(difference_in_24h_periods)))where を、、、、、のいずれかを使用するか、f型指定を使用して有効桁数を指定できます。roundfloorceilnearbyinttruncrintintprintf

そしてzsh

zmodload zsh/datetime
date=2019-02-08T19:24:30.220Z
TZ=UTC0 strftime -rs then_in_seconds '%Y-%m-%dT%H:%M:%S' "${date%.*}"
then_in_seconds+=.${${date##*.}%Z}
now_in_seconds=$EPOCHREALTIME
difference_in_seconds=$((now_in_seconds - then_in_seconds))
difference_in_24h_periods=$((difference_in_seconds / 24 / 60 / 60))
echo "Result: $difference_in_24h_periods"

おすすめ記事