Yesterday I posted a bash_profile function that would allow you to get the current Stardate. While this little bash tweak was cool I really wanted to do something a little more in-depth so I started searching for other ways of calculating the current Stardate. I eventually stumbled upon a perl script that I would like to share with you all now.
Here is the perl script for you to copy and paste…
#!/usr/bin/perl
# Taken from http://everything2.com/title/Stardate
$t = time();
$precision = 1;
$stardate_epoch = 11139552000;
$absolute = (($t - $stardate_epoch) / (31556.952));
$centuries = int($absolute/100000)-1;
$relative = int($absolute - $centuries*100000 + 0.5);
$part_of_day = (($t - $stardate_epoch) % 86400)/86400.0;
$part_of_day_str = sprintf("%.".$precision."f", $part_of_day);
$choped_part_of_day = substr($part_of_day_str, 2, length($part_of_day_str)-2);
print($relative,".",$choped_part_of_day,”\n&rdquoWinking;
And here is the original bash_profile function with a tweak that allows you to call the above perl script if it is located in your /usr/local/bin/ directory:
function stardate() {
if [ -n "$1" -a "$1" = "-a" ]; then
if [ ! -f /usr/local/bin/stardate ]; then
date +%Y.%j | sed 's/\.0*/\./g';
else
/usr/local/bin/stardate;
fi
else
date +%Y%d.%m | sed 's/\.0*/\./g';
fi
}