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 Hope you can help. Carver |
Use $RANDOM, multiply it up/down appropriately, and feed it into date to reformat it.
Code:
$ temp=$(echo "$RANDOM * $RANDOM * $RANDOM / 1000" | bc -l) |
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 |
Quote:
I prefer duckduckgo for searching. No tracking, no ads. ...but then, I'm probably not an "ordinary Web surfer" :) |
Ranges I was looking for...
Quote:
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//-/} 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 |
Quote:
Carver |
Quote:
Quote:
Code:
man date |
as others have suggested, $RANDOM with these constraints:
Code:
[schneidz@hyper ~]$ date -d 1999-01-01 +%s |
Yeah, I get it...sort of...no I don't quite get it.
Quote:
Code:
date: invalid date ‘@17897989834.74600000000000000000’ And I sort of understand how to set the ranges/parameters, but I need help combining Code:
date -d 1999-01-01 +%s Carver |
Quote:
|
you can test it by:
Code:
$ date --version Code:
$ date -d @17897989834.74600000000000000000 |
Quote:
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) |
About my "date" version, etc.
date --version returns
Code:
date (GNU coreutils) 8.25 (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 |
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 Carver |
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 Looking forward to some useful answers, per usual. Carver |
All times are GMT -5. The time now is 07:19 AM. |