LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash generate random dates with times (https://www.linuxquestions.org/questions/linux-newbie-8/bash-generate-random-dates-with-times-4175608647/)

L_Carver 06-26-2017 10:17 AM

bash generate random dates with times
 
1 Attachment(s)
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

suicidaleggroll 06-26-2017 10:44 AM

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.

pan64 06-26-2017 11:03 AM

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

scasey 06-27-2017 10:57 AM

Quote:

Originally Posted by L_Carver (Post 5727350)
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" :)

L_Carver 06-28-2017 10:28 AM

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

L_Carver 06-28-2017 10:32 AM

Quote:

Originally Posted by scasey (Post 5727879)
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

scasey 06-28-2017 11:23 AM

Quote:

Originally Posted by L_Carver (Post 5728323)
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 (Post 5728323)
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. ;)

schneidz 06-28-2017 02:37 PM

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


L_Carver 06-29-2017 12:55 PM

Yeah, I get it...sort of...no I don't quite get it.
 
Quote:

Originally Posted by suicidaleggroll (Post 5727363)
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

suicidaleggroll 06-29-2017 12:57 PM

Quote:

Originally Posted by L_Carver (Post 5728803)
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.

pan64 06-29-2017 01:08 PM

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


suicidaleggroll 06-29-2017 01:14 PM

Quote:

Originally Posted by L_Carver (Post 5728803)
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.

L_Carver 07-01-2017 01:24 PM

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

L_Carver 07-01-2017 04:00 PM

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

L_Carver 09-26-2017 10:13 AM

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


All times are GMT -5. The time now is 07:19 AM.