LinuxQuestions.org
Help answer threads with 0 replies.
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 06-26-2017, 10:17 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Rep: Reputation: Disabled
bash generate random dates with times


I have no idea why the ordinary Web surfer thinks Google is superior to the other search engines. (Or has that changed?) I am attaching a screenshot of hits that came up for me in response to the above search string.

In the meantime, maybe I can pick your brains for an answer or two to the implied question: "How do I generate a set of random dates (and times) and re-format them for use with GNU touch (the -t option; all numbers and one dot delimiting seconds)?"

In short:
Code:
YYMMDDhhmm.ss
I have found a few good scripts to do the Year-Month-Day (pseudo)random generation in a specified range, but I'm looking for a way to generate random times (in a 24-hour range).

Hope you can help.

Carver
Attached Thumbnails
Click image for larger version

Name:	hitsgooglegenrandomd.png
Views:	101
Size:	174.0 KB
ID:	25327  
 
Old 06-26-2017, 10:44 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
Use $RANDOM, multiply it up/down appropriately, and feed it into date to reformat it.

Code:
$ temp=$(echo "$RANDOM * $RANDOM * $RANDOM / 1000" | bc -l)
$ date -d @$temp
Tue Jun  7 08:31:54 MDT 2011
$ date -d @$temp +%y%m%d%H%M.%S
1106070831.54
You didn't provide any specifics with regards to allowable ranges, so you'd have to tweak things as necessary.

Last edited by suicidaleggroll; 06-26-2017 at 10:46 AM.
 
3 members found this post helpful.
Old 06-26-2017, 11:03 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,482

Rep: Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530
you can use $RANDOM to generate a random number, $RANDOM % 24 to generate a random number between 0 and 23 (you can use as hour). Similar way you can generate 60 minutes and 60 seconds:
Code:
echo $RANDOM % 60 | bc -l
 
1 members found this post helpful.
Old 06-27-2017, 10:57 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,420

Rep: Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044
Quote:
Originally Posted by L_Carver View Post
I have no idea why the ordinary Web surfer thinks Google is superior to the other search engines. (Or has that changed?)
Off-topic 'cause you already have a couple of excellent responses that should solve your question:
I prefer duckduckgo for searching. No tracking, no ads.
...but then, I'm probably not an "ordinary Web surfer"
 
1 members found this post helpful.
Old 06-28-2017, 10:28 AM   #5
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Wink Ranges I was looking for...

Quote:
Originally Posted by suicidaleggroll
You didn't provide any specifics with regards to allowable ranges...
I was looking to generate dates with ranges (in years) from 1999 to 2013, in times, all 24 hours in a day.
I think pan64's method may be better for this than the one you (suicidaleggroll) suggested. I'm inclined to try that one first, and if it fails, to try yours. Breaking the output down to GNU touch -t-ready strings should be no problem, as I already have a nice routine from my gimmetdate script, to wit:
Code:
dart=${dart//-/}
dart=${dart/ /}
dart2=${dart/:/}
dart=${dart2/:/.}
where "dart" is the date-time string in standard stat -c %y output format.

As it happens, I just found a file on the Net where the server-side mod date has a year of 2017, which I definitely want to change. So I'll know right away which random date-time method garners the best results.

Carver

Last edited by L_Carver; 06-28-2017 at 10:29 AM. Reason: Added punctuation.
 
Old 06-28-2017, 10:32 AM   #6
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Off-topic 'cause you already have a couple of excellent responses that should solve your question:
I prefer duckduckgo for searching. No tracking, no ads.
...but then, I'm probably not an "ordinary Web surfer"
Thank you for the suggestion. My LM 18 install had duckduckgo for the default search engine in Firefox and, when I found and installed them, one or two other browsers. I'm thinking now I should have stuck with it.

Carver
 
Old 06-28-2017, 11:23 AM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.8.2003
Posts: 5,420

Rep: Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044Reputation: 2044
Quote:
Originally Posted by L_Carver View Post
I was looking to generate dates with ranges (in years) from 1999 to 2013, in times, all 24 hours in a day.
I think pan64's method may be better for this than the one you (suicidaleggroll) suggested.
Um. Both of them suggested the same solution, to wit: Use $RANDOM

Quote:
Originally Posted by L_Carver View Post
Breaking the output down to GNU touch -t-ready strings should be no problem, as I already have a nice routine from my gimmetdate script, to wit:
Code:
dart=${dart//-/}
dart=${dart/ /}
dart2=${dart/:/}
dart=${dart2/:/.}
where "dart" is the date-time string in standard stat -c %y output format.
Code:
man date
You work way harder than you need to.
 
Old 06-28-2017, 02:37 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-30
Posts: 5,290

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
as others have suggested, $RANDOM with these constraints:
Code:
[schneidz@hyper ~]$ date -d 1999-01-01 +%s
915166800
[schneidz@hyper ~]$ date -d 2013-12-31 +%s
1388466000
 
Old 06-29-2017, 12:55 PM   #9
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Yeah, I get it...sort of...no I don't quite get it.

Quote:
Originally Posted by suicidaleggroll View Post
Use $RANDOM, multiply it up/down appropriately, and feed it into date to reformat it.

Code:
$ temp=$(echo "$RANDOM * $RANDOM * $RANDOM / 1000" | bc -l)
$ date -d @$temp
Tue Jun  7 08:31:54 MDT 2011
$ date -d @$temp +%y%m%d%H%M.%S
1106070831.54
Every few times I run RANDOM this way, I get an
Code:
date: invalid date @17897989834.74600000000000000000
message. (the above is just one example.) Is there any way to stop this from happening? Or is it part of the nature of RANDOM?

And I sort of understand how to set the ranges/parameters, but I need help combining
Code:
date -d 1999-01-01 +%s
et cetera with RANDOM.

Carver
 
Old 06-29-2017, 12:57 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
Quote:
Originally Posted by L_Carver View Post
Every few times I run RANDOM this way, I get an
Code:
date: invalid date @17897989834.74600000000000000000
message. (the above is just one example.) Is there any way to stop this from happening? Or is it part of the nature of RANDOM?
Your example works fine with mine, maybe you have an older version of date that is limited in its valid range? Once you scale it down to fit within the range posted by schneidz I imagine that will stop happening.
 
Old 06-29-2017, 01:08 PM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,482

Rep: Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530Reputation: 5530
you can test it by:
Code:
$ date --version
date (GNU coreutils) 8.26
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.
Code:
$ date -d @17897989834.74600000000000000000
Fri Mar  1 16:50:34 CET 2537

Last edited by pan64; 06-29-2017 at 01:10 PM.
 
Old 06-29-2017, 01:14 PM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138Reputation: 2138
Quote:
Originally Posted by L_Carver View Post
And I sort of understand how to set the ranges/parameters, but I need help combining
Code:
date -d 1999-01-01 +%s
et cetera with RANDOM.
Well you wouldn't combine that command with $RANDOM. The -d flag in date says to use the date you provide rather than the current date. You need to feed $RANDOM into -d, not a specific date like you've shown here. The +%s flag in date tells it what output format to use. In this case, %s means "seconds since 1970-01-01 00 UT", or what is more traditionally known as the Unix Epoch.

So "date -d 1999-01-01 +%s" specifically means, tell me what Jan 1, 1999 was in Unix Epoch time.

Anyway, for your problem schneidz provided you with the valid epoch time ranges, 915166800 to 1388466000. That's a range of 473299200. $RANDOM covers 0-32767, if you square it and multiply by 0.4408 that'll get you basically there, then just add 915166800. You might miss out on a couple of hours on the top end of that time range, but you can adjust the precision as neceesary.
Code:
temp=$(echo "$RANDOM * $RANDOM * 0.4408 + 915166800" | bc -l)
should always put temp in that range, then you can pass it to date like I had in my example above.

Last edited by suicidaleggroll; 06-29-2017 at 01:20 PM.
 
1 members found this post helpful.
Old 07-01-2017, 01:24 PM   #13
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Talking About my "date" version, etc.

date --version returns
Code:
date (GNU coreutils) 8.25
Should I look to install a newer version? Is there an i386 deb
(Enough on that -- got the coreutils-8.26-3ubuntu3_i386.deb and installed it with gdebi. It wasn't available on the pages for Xenial [16.04] on packages.ubuntu.com, but I found it on the pages for Zesty [16.10]. Time will tell if the bug fix I presume is in the better/newer version of date pans out.)

Carver

Last edited by L_Carver; 07-01-2017 at 01:26 PM.
 
Old 07-01-2017, 04:00 PM   #14
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Smile And the obvious last step: a script to...

..generate random dates & times in my preferred range.
Indeed, the upgrade of coreutils did "cure" that behaviour with 'date.'

Code:
#!/bin/bash -i
echo "Generating a random modified date (for use with 'touch -t')"
temp=$(echo "$RANDOM * $RANDOM * 0.4408 + 915166800" | bc -l)
tdate=$(date -d @$temp +%C%y%m%d%H%M.%S)
echo "Your new modified date is $tdate ."
/bin/echo -n "$tdate" | xsel -b
echo "It is now on your clipboard."

exit 0
I call it tdr for touch dates randomly.

Carver
 
1 members found this post helpful.
Old 09-26-2017, 10:13 AM   #15
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
How do I get the "highest" date range?

I've changed my mind about the range of dates I want to generate with the script. Is there any way to specify the "highest" year to generate dates in/for? Having dates like
Code:
202005201744.32
seems a little bizarre to me. I'm much more inclined to make 2016 my maximum year in the range, and set 2012 for the start of the range.

Looking forward to some useful answers, per usual.

Carver
 
  


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 04:28 PM
Random hardfreeze at random times - Dell e6500 Capt_Krill Slackware 5 05-07-2012 09:30 PM
Loop that will generate dates from now to beginning of year mrobertson Programming 1 10-06-2005 09:02 AM
can bash generate random digit? sorno Linux - Newbie 5 08-17-2005 06:05 AM
Dates and Times in MySQL oulevon General 2 09-13-2001 03:27 AM

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

All times are GMT -5. The time now is 10:57 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