Shell determines the year code according to date, month and week

Time:2021-4-23

Three time variables are known
time1=”Fri Aug 23 11:35:21″
time2=”Mon Sep 2 16:18:09″
time3=”Tue Sep 3 16:06:33″
Given that these three times can be determined to occur in 2011-2013, how to quickly determine which year?

Desired output:
time1=”2013/8/23 11:35:21″
time2=”2013/9/2 16:18:09″
time3=”2013/9/3 16:06:33″


#!/bin/bash
time1="Fri Aug 23 11:35:21"
time2="Mon Sep 2 16:18:09"
time3="Tue Sep 3 16:06:33"
time4="Sun Dec 2 16:06:34"
for((i=1;i<=4;i++)); do
  for((j=2011;j<=2013;j++)); do
    str=$(eval echo $time$i)
    WEEK_DAY=$(date -d "${str:0:10} $j ${str:10}" "+%a")
    if [ "${str:0:3}" == $WEEK_DAY ]; then
      echo time$i=\"$(date -d "${str:0:10} $j ${str:10}" "+%Y/%m/%d %T")\"
      # break
    fi
  done
done

Recommended Today

Hash algorithm

In 2011, CSDN’s “off database” incident, at that time, CSDN’s website was attacked by hackers, and the registered mailbox and password plaintext of more than 6 million users were leaked. Many netizens were dissatisfied with CSDN’s plaintext saving of user passwords. If you were an engineer of CSDN, how would you store such important data […]