Skip to content

Instantly share code, notes, and snippets.

@geraldlai
Last active December 16, 2017 19:34
Show Gist options
  • Save geraldlai/b233fc81f574d5a2b42d1771982ca71d to your computer and use it in GitHub Desktop.
Save geraldlai/b233fc81f574d5a2b42d1771982ca71d to your computer and use it in GitHub Desktop.
dayo: date alternative
#!/usr/bin/perl
# Daylight come and he wan' go home ...
# $ dayo 12am
# Thu Oct 19 00:00:00 2017 PDT -0700 | America/Los_Angeles | 1508396400
#
# $ dayo 'yesterday - 2 hours'
# Wed Oct 18 20:39:18 2017 PDT -0700 | America/Los_Angeles | 1508384358
#
# $ dayo -est '4 days'
# Tue Oct 24 01:42:15 2017 EDT -0400 | America/New_York | 1508823735
#
# $ dayo -ist '1 month ago'
# Wed Sep 20 11:12:29 2017 IST +0530 | Asia/Kolkata | 1505886149
#
# $ dayo -wes 20160331
# Thu Mar 31 00:00:00 2016 WEST +0100 | Europe/Lisbon | 1459378800
#
# $ dayo -c 136734883
# Thu May 2 08:54:43 1974 CDT -0500 | America/Chicago | 136734883
#
# $ dayo --iso -c 136734883
# 1974-05-02T08:54:43-0500 | Thu | America/Chicago | CDT | 136734883
# Author: Gerald Lai
# License: 0BSD
use warnings;
use strict;
my $date = ($^O eq "darwin") ? "gdate" : "date";
$ENV{TZ} = "America/Los_Angeles";
my $use_iso = 0;
if (@ARGV && $ARGV[0] eq "--iso") {
$use_iso = 1;
shift @ARGV;
}
if (@ARGV && $ARGV[0] =~ s/^-//) {
local $_ = shift @ARGV;
$_ = "America/Sao_Paulo" if /^br$/;
$_ = "Asia/Kuala_Lumpur" if /^myt?$/;
$_ = "Asia/Shanghai" if /^ch[ds]?t?$/;
$_ = "Asia/Taipei" if /^tw?t?$/;
$_ = "America/Anchorage" if /^(a([kh]|[ps])?[ds]?|ys)t?$/;
$_ = "America/Los_Angeles" if /^p[ds]?t?$/;
$_ = "America/Phoenix" if /^m[dsw]?t?$/;
$_ = "America/Chicago" if /^c[ds]?t?$/;
$_ = "America/New_York" if /^e[ds]?t?$/;
$_ = "America/Argentina/Buenos_Aires" if /^cmt?$/;
$_ = "UTC" if /^(z|ut?c?t?|gm?t?)$/;
$_ = "Europe/Lisbon" if /^wes?t?$/;
$_ = "Europe/Madrid" if /^ces?t?$/;
$_ = "Europe/Athens" if /^ees?t?$/;
$_ = "Asia/Kolkata" if /^is?t?$/;
$_ = "Asia/Bangkok" if /^bm?t?$/;
$_ = "Asia/Jakarta" if /^wib$/;
$_ = "Australia/Perth" if /^a[uw][ds]?t?$/;
$_ = "Asia/Seoul" if /^k[ds]?t?$/;
$_ = "Asia/Tokyo" if /^j[ds]?t?$/;
die "Invalid timezone: $_\n" unless m{/} || !m{[[:lower:]]};
$ENV{TZ} = $_;
}
my $t = @ARGV ? pop @ARGV : "now";
# account for: HH, HHMM, YYMMDD, YYYYMMDD
# otherwise, treat as epoch time
if ($t =~ /^([0-9]{9,}|[0-9]{7}|[0-9]{5})$/) {
$t = "@" . $t;
}
unless (@ARGV) {
push @ARGV, $use_iso
? "+%FT%T%z | %a | $ENV{TZ} | %Z | %s"
: "+%c %Z %z | $ENV{TZ} | %s";
}
exec $date, "-d" => $t, @ARGV;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment