#!/usr/bin/perl
# Example: 2009-09-23 17:01:25
print date( "%Y-%m-%d %H:%M:%S" );
# ************************************************************
# Return the current time in the given FORMAT.
#
# FORMAT controls the output.
#
# %a => Sun
# %A => Sunday
# %b => Jan
# %B => January
# %d => 01 (day of month)
# %F => %Y-%m-%d
# %g => 09 (last to digit of the year)
# %H => 00..23 (hour)
# %j => 001..366 (day of year)
# %m => 01..12 (month)
# %M => 00..59 (minute)
# %S => 00..59 (second)
# %s => seconds since 1970-01-01 00:00:00 UTC
# %u => 1..7 (day of week 1=Mon)
# %w => day of week 0..6 0 is Sun
# %Y => year 2009
sub date {
my ( $format, $tzone ) = @_;
$format="%s" if !$format;
$tzone=0 if !$tzone;
my @day = ( 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' );
my @days = ( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' );
my @month = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
my @months = ( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
# Get the localtime by the defined time zone.
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time+($tzone*3600));
my $years = 1900+$year;
( $year ) = ( $years =~ m/(\d{2})$/g );
$mon = $mon+1;
# Get two digits.
$sec = sprintf( "%02d", $sec );
$min = sprintf( "%02d", $min );
$hour = sprintf( "%02d", $hour );
$mon = sprintf( "%02d", $mon );
$mday = sprintf( "%02d", $mday );
# Convert the input format into the current time format.
$format =~ s#\%a#$day[($wday-1)]#g;
$format =~ s#\%A#$days[($wday-1)]#g;
$format =~ s#\%b#$month[($wday-1)]#g;
$format =~ s#\%B#$months[($wday-1)]#g;
$format =~ s#\%d#$mday#g;
$format =~ s#\%F#$years\-$mon\-$mday#g;
$format =~ s#\%H#$hour#g;
$format =~ s#\%j#$yday#g;
$format =~ s#\%m#$mon#g;
$format =~ s#\%M#$min#g;
$format =~ s#\%S#$sec#g;
$format =~ s#\%Y#$years#g;
$format =~ s#\%g#$year#g;
# Get the unix timestamp.
my $tstamp = time+($tzone*3600);
$format =~ s#\%s#$tstamp#g;
return $format;
}