#!/usr/bin/perl

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozcodate2iso8601
#
# The Initial Developer of the Original Code is Google Inc.
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#  Mark Mentovai <mark@moxienet.com> (Original Author)
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

# mozcodate2iso8601 converts date strings in the format used for MOZ_CO_DATE
# into ISO 8601.  MOZ_CO_DATE is the date specification used with
# "cvs checkout -D".  It is set by tools/tinderbox/build-seamonkey-util.pl
# for automated builds when UseTimeStamp is turned on.  Note that UseTimeStamp
# is usually off for release builds, which come from a fixed CVS tag.  Most
# non-automated buids also don't have MOZ_CO_DATE set.  To handle those
# cases, this script will also accept no argument or an empty argument and
# exit without failing or producing any output.

use strict;
use warnings;
use POSIX;

sub error() {
  print STDERR ('usage: ' . $0 . " 'mm/dd/yyyy hh:mm +zzzz'\n");
  return 1;
}

if ($#ARGV > 0) {
  exit(error());
}
elsif ($#ARGV == 0 && length($ARGV[0])) {
  if ($ARGV[0] =~
      /^(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}) ([-+])([\d]{2})([\d]{2})$/) {
    # mm/dd/yyyy hh:mm +zzzz
    # $1/$2/$3   $4:$5 $6$7$8

    # Set the timezone.  POSIX wants the offset as a number to add to a local
    # time to get UTC, so flip the sign.
    $ENV{'TZ'} = 'UTC' . ($6 eq '+' ? '-' : '+') . $7 . ':' . $8;

    # The input string has only one-minute resolution, so set the seconds
    # field to 0.
    my $time = mktime(0, $5, $4, $2, $1 - 1, $3 - 1900);
    if (!defined($time)) {
      exit(error());
    }

    # Print the date and time in ISO 8601 combined format, in the zulu (UTC)
    # zone.
    print(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($time)) . "\n");
  }
  else {
    exit(error());
  }
}

exit(0);
