Add some specific time while using the linux command “date”

前端 未结 7 976
误落风尘
误落风尘 2021-01-07 20:18

In linux, date can help me to print the current time. If want to print the current time + 1 hour, what option should I give?

相关标签:
7条回答
  • 2021-01-07 20:47

    Linux and macOS in a single command

    If you need to write scripts that work on both Linux servers and macOS workstations, you can silence the error of the first date call and 'OR' it (||) with the other. It doesn't matter which comes first.

    date -u -d "+${max_age}Seconds" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
    date -u -v "+${max_age}S"       +"%Y-%m-%dT%H:%M:%SZ"
    
    

    For example, this bash function uploads a file to AWS S3 and sets a Expires: and Cache-Control: headers.

    s3_upload_cache_control(){
      local max_age_seconds="${1}" ;shift         # required
      local bucket_path="${1}"     ;shift         # required
      local filename="${1}"        ;shift         # required
      local remote_filename="/${1:-${filename}}"  # optional
      local fmt="+%Y-%m-%dT%H:%M:%SZ"
      aws s3 cp                                                          \
        "${filename}"                                                    \
        "s3://${bucket_path}${remote_filename}"                          \
        --expires                                                  "$(   \
          date -u -d "+${max_age_seconds}Seconds" $fmt 2>/dev/null  ||   \
          date -u -v "+${max_age_seconds}S"       $fmt               )"  \
        --cache-control max-age=$max_age,public                          \
        --acl public-read
    }
    
    
    0 讨论(0)
提交回复
热议问题