Skip to content

Instantly share code, notes, and snippets.

@adkinss
Created August 12, 2016 14:41
Show Gist options
  • Save adkinss/fe08593183d3785eed7cd250204740f1 to your computer and use it in GitHub Desktop.
Save adkinss/fe08593183d3785eed7cd250204740f1 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
# Without parameters, the script displays the current date in three different
# formats (epoch, julian, and gregorian). Here is a example run:
#
# $ ./complicated_date.pl
# epoch: 1471012564 Fri Aug 12 10:36:04 2016
# julian: 2457613.10837963
# gregorian: 1471012564 Fri Aug 12 10:36:03 2016
#
# You can also supply an epoch time (seconds since 1970) and the script will
# display the three different formats for that particular point in time.
use strict;
sub display_date (@)
{
my $now_time = shift @_;
my $julian = shift @_;
my $newline = shift @_;
my $now_date = localtime($now_time);
my $greg_time = ($julian - 2440587.5) * 86400;
my $greg_round = int($greg_time + $greg_time/abs($greg_time*2));
my $greg_date = localtime($greg_time);
print "\n" if $newline;
print "epoch: $now_time $now_date\n";
print "julian: $julian\n";
print "gregorian: $greg_round $greg_date\n";
}
if (@ARGV > 0) {
my $newline = 0;
foreach my $arg (@ARGV) {
my ($now_time, $julian) = (0, 0);
if (length($arg) >= 9 && $arg =~ /^\d+$/) {
$now_time = $arg;
$julian = ($now_time / 86400) + 2440587.5;
}
if (length($arg) < 9 || $arg =~ /\./) {
$julian = $arg;
$now_time = ($julian - 2440587.5) * 86400;
}
display_date($now_time, $julian, $newline);
$newline = 1;
}
} else {
my $now_time = time();
my $julian = ($now_time / 86400) + 2440587.5;
display_date($now_time, $julian, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment