Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Thursday, January 22, 2015

How to Find UTC Time Offset from your Local Time?

This is the sample script to find the offset in seconds of your local time from GMT.
#!/usr/bin/perl
use strict;

use POSIX;

my @local = localtime();
my @utc   = gmtime();

my $timezone_offset = mktime(@local) - mktime(@utc);

print "Offset in second(s) from GMT = $timezone_offset\n";
print "Offset in hour(s) from GMT = " . ($timezone_offset / 3600) . "\n";

Output:
Offset in second(s) from GMT = 28800
Offset in hour(s) from GMT = 8

Saturday, April 2, 2011

Check for Holiday with Perl


In some cases we would like to avoid to execute the script during public holiday e.g. Don’t send alert during holiday. This script contains 2 functions i.e. “load_holiday_file” and “is_holiday” where you can include in your script to check for holiday.

#!/usr/bin/perl
use strict;

my @holidays = ();
my @now = localtime(time());

my $date = sprintf("%02d-%02d-%04d", $now[3], $now[4]+1, $now[5]+1900 );

load_holiday_file(\@holidays, 'holiday.txt');

if (is_holiday(\@holidays, $date)) {
    print "Today is holiday";
}

# Check the date is holiday
# "date_to_check" is in "DD-MM-YYYY" format.
sub is_holiday {
    my $holidays = shift;
    my $date_to_check = shift;
       
    my $counter = 0;
    my $found = 0;
       
    while($counter <= $#{holidays} && !$found) {
        my $holiday = $holidays->[$counter];
               
        if ($date_to_check =~ /^$holiday/) {
            $found = 1;
        }
        else {           
            $counter++;
        }   
    }

    return $found;
}

# Load holiday file
# Holiday file
# 1. DD-MM-YYYY
# 2. DD-MM (Every year on this date)
sub load_holiday_file {
    my $holidays     = shift;
    my $holiday_file = shift;
       
    if (open(HOLIDAY_FILE, $holiday_file)) {
   
        my $line;
        while (defined($line = <HOLIDAY_FILE>)) {   
            chomp($line);
             
            next if ($line =~ /^\s*#/ );
            next if ($line =~ /^$/);
           
            # Trim the string
            $line =~ s/^\s+//;   
            $line =~ s/\s+$//;
                                               
            push @{holidays}, $line;
        }        
        close(HOLIDAY_FILE);
    }
} 



Holiday File

# Holiday file
# 1. DD-MM-YYYY
# 2. DD-MM (Every year on this date)
27-01-2011       
03-02-2011
03-02-2011
22-04-2011
02-05-2011
17-05-2011
09-08-2011
30-08-2011
26-10-2011
06-11-2011
26-12-2011
02-01-2012

Sunday, September 12, 2010

Sort Date in "DD MMM YYYY" format

To sort date in "DD MMM YYYY" format (e.g. "02 DEC 2010")

#/usr/bin/perl -w
use strict;

# Constants
my %MONTH_NUMBER = (
    'JAN' => 1,
    'FEB' => 2,
    'MAR' => 3,
    'APR' => 4,
    'MAY' => 5,
    'JUN' => 6,
    'JUL' => 7,
    'AUG' => 8,
    'SEP' => 9,
    'OCT' => 10,
    'NOV' => 11,
    'DEC' => 12,    
);

my @myarray = (
    '08-DEC-10',
    '30-NOV-10',
    '26-NOV-10',
    '25-NOV-10',
    '25-NOV-10',
    '24-NOV-10',
    '11-NOV-10',
    '10-NOV-10',
    '10-NOV-10',
    '08-NOV-10',
    '03-NOV-10',
    '02-NOV-10',    
    '01-NOV-10',
);

my @sorted_array = sort sort_func @myarray;

print "@sorted_array"; 

####################
# Subroutine
####################
# Sort function
sub sort_func() {
    # To sort in desc, just swap $a and $b
    #return convert_datenumber($b) 
    #   cmp convert_datenumber($a);
  
    return convert_datenumber($a) 
       cmp convert_datenumber($b);
}

# Convert "DD MMM YYYY" into "YYYYMMDD"
sub convert_datenumber() {
    my $in_date = $_[0];
    
    my $out_date = 0;
            
    if (length($in_date)) {
        my $day        = substr($in_date, 0, 2);
        my $month      = $MONTH_NUMBER{ substr($in_date, 3, 3) };
        my $year       = substr($in_date, 7);
        
        $out_date = $year * 10000 
                  + $month * 100
                  + $day;                                    
    }
        
    return $out_date;
}
Output:

01-NOV-10 02-NOV-10 03-NOV-10 08-NOV-10 10-NOV-10 10-NOV-10 11-NOV-10 24-NOV-10 25-NOV-10 25-NOV-10 26-NOV-10 30-NOV-10 08-DEC-10