1

I am writing a BASH script that calls an API that presents metrics for specific time-frames. I plan to run the script on a cron job on the 1st of each month, the API call needs to contain the start and end time and be in a epoch format with milliseconds. Milliseconds should be ok to set to 000 as it doesnt need to be that specific but the API requires it.

How can I code the script to look at the current human readable time then look at the exact same time exactly 1 calendar month before, then convert both outputs to epoch, and then enter the epoch times in the curl command as a variable?

Example:

Script runs at 1am on July 1st, script then understands a full calendar month before was June 1st, converts both to epoch, places them into the curl command using variables.

I understand how to get a human readable date for 1 month ago, however I am unsure how best to convert these dates to epoch.

 date --date="1 month ago" +"%d%m%Y"
tadman
  • 194,930
  • 21
  • 217
  • 240
Matt B
  • 37
  • 6
  • 2
    date --date="1 month ago" +"%s" should work – Matt B May 17 '19 at 17:32
  • Is this a duplicate? https://stackoverflow.com/questions/1092631/get-current-time-in-seconds-since-the-epoch-on-linux-bash – Benjamin W. May 17 '19 at 17:33
  • I wouldnt say so, mines more specific to the script I am trying to create. Its the conversation into a variable thats part of my issue – Matt B May 17 '19 at 17:40

1 Answers1

1

To create a variable using date in epoch times with milliseconds:

ENDDATE=`date +%s%N | cut -b1-13`
STARTDATE=`date --date="1 month ago" +%s%N | cut -b1-13`

Use these variables in the cURL command.

Matt B
  • 37
  • 6