All pastes #106730 Raw Edit

Something

public text v1 · immutable
#106730 ·published 2006-07-30 14:43 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use warnings;

print "Feed me with a date and time, e.x.: 2004 10 12 17 55 13:\n";
my @input = <STDIN>;

my @date_list = map { split ' ' } @input;

my $year = $date_list[0];
my $mon  = $date_list[1];
my $day  = $date_list[2];
my $hour = $date_list[3];
my $min  = $date_list[4];
my $sec  = $date_list[5];

my @months = (
           "28", "31", "30",
           "31", "30", "31",
           "31", "30", "31",
           "30", "31",
);

my $leapyear       = 0;
my $leapyear_check = 0;
my $temp_year      = $year;
while($temp_year >= 1970) {

        if ($temp_year % 4 == 0) {
           $leapyear_check = 1;
        } elsif ($temp_year %400 == 0) {
           $leapyear_check = 1;
        } else {
           $leapyear_check = 0;
        }

        $leapyear++ if ($leapyear_check == 1);

        $temp_year--;
}

my $temp_mon = $mon -2; #voor de array index...
my $months_total;
for(my $i = 0; $i <= $temp_mon; $i++) {
        $months_total += $months[$i];
}

my $years_total = 365 * ($year - 1970) + $leapyear;
my $days_total  = $day;
my $hours_total  = $hour * 60 * 60;
my $min_total    = $min  * 60;
my $total_days  = $years_total + $months_total + $days_total;


my $unix_timestamp = ($total_days * 86400) + ($hours_total + $min_total + $sec);

print "$unix_timestamp\n";