#! /bin/bash

# Public Domain

(date '+%Y-%m-%d %H:%M:%S UTC' -u | perl -pe 's/\n/ /'; date '+%Y-%m-%d %H:%M:%S local filename') | perl -e '
    use Time::Piece;
    while (<<>>) {
      chomp;
      if (not /^([-: 0-9]+) UTC ([-: 0-9]+) local /) {
        if (/^===/) {
          print "$_\n";
        } else {
          print "Error: \"$_\"\n";
        }
      } else {
        my $utc = $1;
        my $local = $2;
        my $target = Time::Piece->strptime("$utc +0000", "%Y-%m-%d %H:%M:%S %z");
        my $ifutc = Time::Piece->strptime("$local +0000", "%Y-%m-%d %H:%M:%S %z");
        my $offset = int( ($ifutc - $target) / 60);
        my $zone;
          my $negative = $offset < 0;
          my $abs = abs($offset);
          my $sign = "-";
          if (not $negative) { $sign = "+"; };
          my $min = $abs % 60;
          my $hour = int($abs / 60);
          $zone = sprintf("%s%02u%02u", $sign, $hour, $min);
          my $candidate = Time::Piece->strptime("$local $zone", "%Y-%m-%d %H:%M:%S %z");
          if ($candidate->epoch != $target->epoch) {
            $zone = "";
          };
        if ($zone eq "") {
          print "Error: \"$_\"\n";
        } else {
          print "$local $zone\n";
        }
      }
    }'
