| 1 | #+############################################################################## | ||||||||||||||
| 2 | # # | ||||||||||||||
| 3 | # File: No/Worries/Date.pm # | ||||||||||||||
| 4 | # # | ||||||||||||||
| 5 | # Description: date handling without worries # | ||||||||||||||
| 6 | # # | ||||||||||||||
| 7 | #-############################################################################## | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # | ||||||||||||||
| 10 | # module definition | ||||||||||||||
| 11 | # | ||||||||||||||
| 12 | |||||||||||||||
| 13 | package No::Worries::Date; | ||||||||||||||
| 14 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 4 | ▶ | use 5.005; # need the four-argument form of substr() | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 4 | our $VERSION = "1.7"; | |||||||||||||
| 18 | 4 | our $REVISION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/); | |||||||||||||
| 19 | |||||||||||||||
| 20 | # | ||||||||||||||
| 21 | # used modules | ||||||||||||||
| 22 | # | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 4 | ▶ | use HTTP::Date qw(str2time); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 4 | ▶ | use No::Worries::Die qw(dief); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | 4 | ▶ | use No::Worries::Export qw(export_control); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | 4 | ▶ | use Params::Validate qw(validate_pos :types); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 4 | ▶ | use POSIX qw(strftime); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | # | ||||||||||||||
| 31 | # constants | ||||||||||||||
| 32 | # | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 4 | ▶ | use constant STRFTIME_STRING_FORMAT => "%Y-%m-%dT%H:%M:%SZ"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 4 | ▶ | use constant STRFTIME_STAMP_FORMAT => "%Y/%m/%d-%H:%M:%S"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | |||||||||||||||
| 37 | # | ||||||||||||||
| 38 | # handle the given time that could be undef or '1.433330218094E9' | ||||||||||||||
| 39 | # | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 20 | ▶ | sub _time ($) { | ||||||||||||
|
Subroutine
_time: called CC=5 SCAR=16.1
| |||||||||||||||
| 42 | 20 | my($time) = @_; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 20 | ▶ | return(time(), 0) unless defined($time); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 18 | eval { | |||||||||||||
| 46 | 4 | ▶ | use warnings FATAL => qw(numeric); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 18 | $time += 0; | |||||||||||||
| 48 | }; | ||||||||||||||
| 49 | 18 | ▶ | dief("invalid time: %s", $time) if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 16 | ▶ | if ($time =~ /^(\d+)$/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 51 | 10 | return($1, 0); | |||||||||||||
| 52 | } elsif ($time =~ /^(\d+)\.(\d+)$/) { | ||||||||||||||
| 53 | 6 | return($1, $2); | |||||||||||||
| 54 | } else { | ||||||||||||||
| 55 | 0 | dief("invalid time: %s", $time); | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | # | ||||||||||||||
| 60 | # convert a string to a time | ||||||||||||||
| 61 | # | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 14 | ▶ | sub date_parse ($) { | ||||||||||||
|
Subroutine
date_parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 64 | 14 | my($string) = @_; | |||||||||||||
| 65 | 14 | my($time); | |||||||||||||
| 66 | |||||||||||||||
| 67 | 14 | validate_pos(@_, { type => SCALAR }); | |||||||||||||
| 68 | 14 | $time = str2time($string); | |||||||||||||
| 69 | 14 | ▶ | dief("invalid date: %s", $string) unless defined($time); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 13 | return($time); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # | ||||||||||||||
| 74 | # convert a time to human friendly string (local time) | ||||||||||||||
| 75 | # | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 10 | ▶ | sub date_stamp (;$) { | ||||||||||||
|
Subroutine
date_stamp: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 78 | 10 | my($time) = @_; | |||||||||||||
| 79 | 10 | my($int, $frac, $string); | |||||||||||||
| 80 | |||||||||||||||
| 81 | 10 | ▶ | validate_pos(@_, { type => SCALAR }) if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 10 | ($int, $frac) = _time($time); | |||||||||||||
| 83 | 9 | $string = strftime(STRFTIME_STAMP_FORMAT, localtime($int)); | |||||||||||||
| 84 | 9 | ▶ | $string .= ".$frac" if $frac; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 9 | return($string); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | # | ||||||||||||||
| 89 | # convert a time to an ISO 8601 compliant string (UTC based) | ||||||||||||||
| 90 | # | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 10 | ▶ | sub date_string (;$) { | ||||||||||||
|
Subroutine
date_string: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 93 | 10 | my($time) = @_; | |||||||||||||
| 94 | 10 | my($int, $frac, $string); | |||||||||||||
| 95 | |||||||||||||||
| 96 | 10 | ▶ | validate_pos(@_, { type => SCALAR }) if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 10 | ($int, $frac) = _time($time); | |||||||||||||
| 98 | 9 | $string = strftime(STRFTIME_STRING_FORMAT, gmtime($int)); | |||||||||||||
| 99 | 9 | ▶ | substr($string, -1, 0, ".$frac") if $frac; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 9 | return($string); | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | # | ||||||||||||||
| 104 | # export control | ||||||||||||||
| 105 | # | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 4 | ▶ | sub import : method { | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||
| 108 | 4 | my($pkg, %exported); | |||||||||||||
| 109 | |||||||||||||||
| 110 | 4 | $pkg = shift(@_); | |||||||||||||
| 111 | 4 | grep($exported{$_}++, map("date_$_", qw(parse stamp string))); | |||||||||||||
| 112 | 4 | export_control(scalar(caller()), $pkg, \%exported, @_); | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 4 | 1; | |||||||||||||
| 116 | |||||||||||||||
| 117 | __DATA__ | ||||||||||||||