You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
5.4 KiB
163 lines
5.4 KiB
package AN::Tools::Get; |
|
# |
|
# This module contains methods used to handle access to frequently used data. |
|
# |
|
|
|
use strict; |
|
use warnings; |
|
use Data::Dumper; |
|
|
|
our $VERSION = "3.0.0"; |
|
my $THIS_FILE = "Get.pm"; |
|
|
|
### Methods; |
|
# date_and_time |
|
|
|
=pod |
|
|
|
=encoding utf8 |
|
|
|
=head1 NAME |
|
|
|
AN::Tools::Get |
|
|
|
Provides all methods related to logging. |
|
|
|
=head1 SYNOPSIS |
|
|
|
use AN::Tools; |
|
|
|
# Get a common object handle on all AN::Tools modules. |
|
my $an = AN::Tools->new(); |
|
|
|
# Access to methods using '$an->Get->X'. |
|
# |
|
# Example using 'date_and_time()'; |
|
my $foo_path = $an->Get->date_and_time({...}); |
|
|
|
=head1 METHODS |
|
|
|
Methods in this module; |
|
|
|
=cut |
|
sub new |
|
{ |
|
my $class = shift; |
|
my $self = {}; |
|
|
|
bless $self, $class; |
|
|
|
return ($self); |
|
} |
|
|
|
# Get a handle on the AN::Tools object. I know that technically that is a sibling module, but it makes more |
|
# sense in this case to think of it as a parent. |
|
sub parent |
|
{ |
|
my $self = shift; |
|
my $parent = shift; |
|
|
|
$self->{HANDLE}{TOOLS} = $parent if $parent; |
|
|
|
return ($self->{HANDLE}{TOOLS}); |
|
} |
|
|
|
|
|
############################################################################################################# |
|
# Public methods # |
|
############################################################################################################# |
|
|
|
=head2 date_and_time |
|
|
|
This method returns the date and/or time using either the current time, or a specified unix time. |
|
|
|
NOTE: This only returns times in 24-hour notation. |
|
|
|
|
|
=head2 Parameters; |
|
|
|
=head3 date_only (optional) |
|
|
|
If set, only the date will be returned (in C<< yyyy/mm/dd >> format). |
|
|
|
=head3 file_name (optional) |
|
|
|
When set, the date and/or time returned in a string more useful in file names. Specifically, it will replace spaces with 'C<< _ >>' and 'C<< : >>' and 'C<< / >>' for 'C<< - >>'. This will result in a string in the format like 'C<< yyyy-mm-dd_hh-mm-ss >>'. |
|
|
|
=head3 offset (optional) |
|
|
|
If set to a signed number, it will add or subtract the number of seconds from the 'C<< use_time >>' before processing. |
|
|
|
=head3 use_time (optional) |
|
|
|
This can be set to a unix timestamp. If it is not set, the current time is used. |
|
|
|
=head3 time_only (optional) |
|
|
|
If set, only the time will be returned (in C<< hh:mm:ss >> format). |
|
|
|
=cut |
|
sub date_and_time |
|
{ |
|
my $self = shift; |
|
my $parameter = shift; |
|
my $an = $self->parent; |
|
|
|
my $offset = defined $parameter->{offset} ? $parameter->{offset} : 0; |
|
my $use_time = defined $parameter->{use_time} ? $parameter->{use_time} : time; |
|
my $file_name = defined $parameter->{file_name} ? $parameter->{file_name} : 0; |
|
my $time_only = defined $parameter->{time_only} ? $parameter->{time_only} : 0; |
|
my $date_only = defined $parameter->{date_only} ? $parameter->{date_only} : 0; |
|
|
|
# Are things sane? |
|
if ($use_time =~ /D/) |
|
{ |
|
die "Get->date_and_time() was called with 'use_time' set to: [$use_time]. Only a unix timestamp is allowed.\n"; |
|
} |
|
if ($offset =~ /D/) |
|
{ |
|
die "Get->date_and_time() was called with 'offset' set to: [$offset]. Only real number is allowed.\n"; |
|
} |
|
|
|
# Do my initial calculation. |
|
my $return_string = ""; |
|
my $time = {}; |
|
my $adjusted_time = $use_time + $offset; |
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - adjusted_time: [$adjusted_time]\n"; |
|
|
|
# Get the date and time pieces |
|
($time->{sec}, $time->{min}, $time->{hour}, $time->{mday}, $time->{mon}, $time->{year}, $time->{wday}, $time->{yday}, $time->{isdst}) = localtime($adjusted_time); |
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - time->{sec}: [".$time->{sec}."], time->{min}: [".$time->{min}."], time->{hour}: [".$time->{hour}."], time->{mday}: [".$time->{mday}."], time->{mon}: [".$time->{mon}."], time->{year}: [".$time->{year}."], time->{wday}: [".$time->{wday}."], time->{yday}: [".$time->{yday}."], time->{isdst}: [".$time->{isdst}."]\n"; |
|
|
|
# Process the raw data |
|
$time->{pad_hour} = sprintf("%02d", $time->{hour}); |
|
$time->{mon}++; |
|
$time->{pad_min} = sprintf("%02d", $time->{min}); |
|
$time->{pad_sec} = sprintf("%02d", $time->{sec}); |
|
$time->{year} = ($time->{year} + 1900); |
|
$time->{pad_mon} = sprintf("%02d", $time->{mon}); |
|
$time->{pad_mday} = sprintf("%02d", $time->{mday}); |
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - time->{pad_hour}: [".$time->{pad_hour}."], time->{pad_min}: [".$time->{pad_min}."], time->{pad_sec}: [".$time->{pad_sec}."], time->{year}: [".$time->{year}."], time->{pad_mon}: [".$time->{pad_mon}."], time->{pad_mday}: [".$time->{pad_mday}."], time->{mon}: [".$time->{mon}."]\n"; |
|
|
|
# Now, the date and time separator depends on if 'file_name' is set. |
|
my $date_separator = $file_name ? "-" : "/"; |
|
my $time_separator = $file_name ? "-" : ":"; |
|
my $space_separator = $file_name ? "_" : " "; |
|
if ($time_only) |
|
{ |
|
$return_string = $time->{pad_hour}.$time_separator.$time->{pad_min}.$time_separator.$time->{pad_sec}; |
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - return_string: [$return_string]\n"; |
|
} |
|
elsif ($date_only) |
|
{ |
|
$return_string = $time->{year}.$date_separator.$time->{pad_mon}.$date_separator.$time->{pad_mday}; |
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - return_string: [$return_string]\n"; |
|
} |
|
else |
|
{ |
|
$return_string = $time->{year}.$date_separator.$time->{pad_mon}.$date_separator.$time->{pad_mday}.$space_separator.$time->{pad_hour}.$time_separator.$time->{pad_min}.$time_separator.$time->{pad_sec}; |
|
#print $THIS_FILE." ".__LINE__."; [ Debug ] - return_string: [$return_string]\n"; |
|
} |
|
|
|
return($return_string); |
|
}
|
|
|