LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2017, 11:37 AM   #16
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137Reputation: 2137

You've been given all of the tools necessary to change your date range earlier in the thread. Where, specifically, are you lost?
 
Old 09-26-2017, 12:49 PM   #17
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,554

Rep: Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156Reputation: 3156
While not as neat or compact as your previous solution this allows you to easily change the start and end years as desired and a bit easier to understand. To keep it simple I don't mess with leap year.

Code:
#!/bin/bash

convertsecs() {
 ((h=${1}/3600))
 ((m=(${1}%3600)/60))
 ((s=${1}%60))
 printf "%02d%02d.%02d" $h $m $s
}

start=2012
end=2016

num=$(shuf -i $start-$end -n 1)
day=$(shuf -i 0-364 -n 1) 
tdate=$(date -d "$day days $num-01-01" +"%Y%m%d")
time=$(convertsecs $(shuf -i 0-86399 -n 1) )

echo $tdate$time
version 2 just calculates a random year/day and converts it to seconds instead of needing an additional function.

Code:
#!/bin/bash

start=2012
end=2016
interv=$((end-start))
sstart=$(date -d "$start-01-01" +%s)
num=$(shuf -i 0-$interv -n 1)
day=$(shuf -i 1-365 -n 1) 
time=$(shuf -i 0-86399 -n 1)
yrs=$(( (num*365+day)*86400))
end=$((sstart+yrs+time))
tdate=$(date -d @$end +%C%y%m%d%H%M.%S)
echo $tdate

Last edited by michaelk; 09-26-2017 at 06:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] generate dates between two dates ip_address Programming 4 03-21-2013 05:28 PM
Random hardfreeze at random times - Dell e6500 Capt_Krill Slackware 5 05-07-2012 10:30 PM
Loop that will generate dates from now to beginning of year mrobertson Programming 1 10-06-2005 10:02 AM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 07:05 AM
Dates and Times in MySQL oulevon General 2 09-13-2001 04:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration