LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2010, 04:28 AM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
By way of thanks for all the help.


A script to generate random dates. It uses the year range 2006-2009, and truncates every month of the year to an ordinary February's 28 days, but otherwise it's pretty solid and safe.

Code:
spate=1
while [ $spate -le 120 ]
do
NUMBER=$[ ( $RANDOM % 864000 ) + 1 ]
RNUMBER=$((RANDOM%4+2006))
ERNUMB=$((RANDOM%11+1))
SHTMON=$[ ($RANDOM%27) + 1 ]
if [ ${#SHTMON} = 1 ]; then day=$(echo "0"$SHTMON); else day=$SHTMON; fi
if [ ${#ERNUMB} = 1 ]; then mon=$(echo "0"$ERNUMB); else mon=$ERNUMB; fi
if [ ${#RNUMBER} = 1 ]; then year=$(echo "0"$RNUMBER); else year=$RNUMBER; fi
b=$[NUMBER / 60]
c=$[b * 60]
seconds=$[NUMBER-c]
d=$[b / 60]
e=$[d * 60]
minutes=$[b-e]
hours=$[e/60] 

#Padding single-length time entries with zeros
if [ ${#seconds} = 1 ]; then sec=$(echo "0"$seconds); else sec=$seconds; fi
if [ ${#minutes} = 1 ]; then min=$(echo "0"$minutes); else min=$minutes; fi
if [ ${#hours} = 1 ]; then hrs=$(echo "0"$hours); else hrs=$hours; fi
longtime=$(echo $hrs$min.$sec) #<< the random time in 'touch -t'-friendly format (seconds marked off by a period, not a colon).
deatt=$(echo $year$mon$day) #<< Sets the date in 'touch -t" ready format.
time=$(echo $deatt$longtime) #<<Putting the date and time together, keeping the formats (no spaces)
echo "$time">>randomtime.txt
echo "Written - random date number $spate."
spate=$[spate+1]
done
YMMV, but when I used the following script on a folder of 120 JPEG files, no two dates were the same. The times? I'm not positive they were likewise all unique, but at a glance I didn't see much repetition.
Code:
while read 'line';
do
nardo=$(echo $line)
chachi=$(echo $nardo | cut -d, -f2)
joanie=$(echo $nardo | cut -d, -f1)
richie=$(basename "$joanie")
echo "Working on file $richie now."
touch -t $chachi $joanie

done<timetemp
And to think this was going to be the topic of a question, me looking for help yet again! Four hours of Google-ing, cross-referencing online MAN and Info pages, and coming across near-to-topic and spot-on threads in forums such as LQ and Unix-Linux Forums (www.unix.com), and such as that gave me just enough where I could turn it around and make this offering.

Again, thanks.

BZT
 
Old 08-03-2010, 06:38 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Why not just pick a random timestamp number and print it as a date? That's just a one liner. Or even why need a random date in the first place?
 
Old 08-03-2010, 09:07 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
On the lines of what Chris was saying:
Code:
let D=$RANDOM*$RANDOM; date -d @$D
 
  


Reply



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



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 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
Open Source Consulting | Domain Registration