Jenkinz Job time adjustment - cron

I am working on the Jenkins jobs. There is already a job created with this command. I need someone's help to guide me about the time settings in command line. There are two variables for start and end time. Kindly , can you explain that what will be the start and ending time in simple English
export start_date=$(date -d "yesterday 00:00:00 " +%s)
export end_date=$(date +%s)

From man date:
%s is the number of seconds since 1970-01-01 00:00:00 UTC
So for the start_date, the command is saying grab the date from yesterday, with time 00:00:00. And then once you have that date, then get the number of seconds elapsed from the Unix epoch (01/01/1970) until that date. So for example, today is 4/14/2016, so start_date is equal to the number of seconds from 1/1/1970 00:00:00 to 4/13/2016 00:00:00.
The end_date is the number of seconds elapsed since the Unix epoch (01/01/1970), from right now (the current time).

Related

Given YYYY-MM-DD, how to find start date of week & end date of week? [duplicate]

This question already has answers here:
Get Monday and Sunday etc.. for a week for any date as parameter in Unix
(2 answers)
Closed 2 years ago.
Is there a way to use the date function in linux to get the exact date of the start of the week & end of the week for a given YYYY-MM-DD?
For example, I could enter 2020-07-24 and it would return 2020-07-20 (Monday) & 2020-07-26 (Sunday) as start & end date for this particular week respectively.
This shell script should work on most Linux as mostly they use GNU date
It converts the input to epoch seconds and then goes back a day until Monday is
found
#!/bin/bash
# take the parameter from command line
d="$1"
# find the current time as seconds since 1st Jan 1970 (epoch time)
start=$(date -d "$d" '+%s')
consider="$start"
# day of the week for the time we are considering
dow=$(date -d "#$consider" '+%A')
# is the day of the week monday? if not, carry on
while [[ "$dow" != "Monday" ]]; do
# adjust the time to be a day further in the past, 24*60*60 seconds is 1 day
let "consider=$consider - 86400"
dow=$(date -d "#$consider" '+%A')
done
# output the found date
date -d "#$consider"

Adding Hours to Date & Time in Linux

I am trying to add 5 hours to a date & time in Linux (Ubuntu 18.04.03) using the 'date' command. I get different results if I specify a date vs if I don't.
Example 1: I want to add 5 hours to a specific date:
[12:45:25 ~]$ date -d "2019-12-08 12:16:45 +5 hours"
Sun Dec 8 03:16:45 EST 2019
But the result is 9 hours BEFORE the specified date and time. I was expecting to have a date of 12/8 and time of 17:16:45.
Example 2: I add 5 hours to the system date:
[12:45:37 ~]$ date -d "+5 hours"
Sun Dec 8 17:46:02 EST 2019
This result is correct and as I expected.
I want to use this in a bash script and can't determine what I am doing incorrectly that I get these different results.
Any help will be appreciated.
After playing around a bit it seems like specifying the timezone with no spaces achieves what you're looking for:
date -d "2019-12-08 11:16:45EST+5hours"
This command outputs Sun Dec 8 16:16:45 EST 2019
Hope this helps!
GNU date very strangely parses the date, which makes it unreliable. The most reliable thing is to convert to seconds since epoch.
Convert the date to seconds.
Add 5 hours of seconds.
Convert the data to your favourite format.
date -d "#$(( $(date -d "2019-12-08 12:16:45" %s) + (5 * 60 * 60) ))"
date -d "2019-12-08 12:16:45 +5 hours"
I guess the +5 get's parsed as the timezone. So if you are in a timezone +5 and want EST, then you need to substact 5 hours for the timezone and 4 hours for current EST time. The hours get's ignored.
Add EST to the date & time string will give the correct answer.
I found it worthwhile to get the timezone and offset like so:
dateInfo=$(date +"%Z %z")
dateTZ=${dateInfo:0:3}
dateOffset="+"${dateInfo:5:2}
Then use them in the conversion code:
DATE=$(date -d "$CDATE1 $dateTZ $dateOffset hours" +%Y%m%d)
(where CDATE1 was parsed from a file's creation date as date & time)

Calendar months variables in BASH script

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"
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.

Date not showing in shell program

I am using this shell program where i need to display date with time for some particular task. For some reason the date is not working and its only showing time.
date +'%H:%M:%S Calling' >> /home/hmm/AppFlatForRent/log.txt #calling
Can anyone tell me whats wrong with it
You're only adding formatting to the date command for the time in hours minutes and seconds (i.e. '%H:%M:%S'). Try:
date +'%Y-%b-%d %H:%M:%S Calling' >> /home/hmm/AppFlatForRent/log.txt #calling

Get Monday and Sunday etc.. for a week for any date as parameter in Unix

How to get the date of Monday and Sunday in a week for a date?
This gives date for 'last' monday:
date -dlast-monday +%Y%m%d
I want to pass a date as parameter to find the Monday and Sunday for that week. Basically, I want to get Sunday and Monday for a week, for ANY date, NOT only for last monday.
Try this:
export day=2013-10-01
date -d "$day -$(date -d $day +%w) days"
This will always print the Sunday before the given date (or the date itself).
date -d "$day -$(date -d $day +%u) days"
This will always print the Sunday before the given date (and never the date itself).
For Mondays you need to add + 1 day:
date -d "$day -$(date -d $day +%u) days + 1 day"
You should also consider what Monday or Sunday you want to get (this wasn't quite clear) for which date. This also depends on whether you consider the Sunday the first or the last day of the week.
date can parse the date on the command line like so:
date -j -f %s 1380628152
which is the date+time in seconds since the UNIX epoch. You can combine the command abovewith your command. The -j means you don't want to actually set the date. The -f specifies a strftime string to use to parse the date on the command line.
Please note that this is on a BSD system and it looks like you are on a GNU system with different options to date (but it must support something similar).

Resources