| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
######################################################################## |
| 29 |
|
| 30 |
=item B<time_mktime> |
| 31 |
|
| 32 |
Params: $seconds, $minutes, $hours, $day_of_month, $month, $year_minus_1900 |
| 33 |
|
| 34 |
Return: $seconds [ -1 on error ] |
| 35 |
|
| 36 |
Performs a general mktime calculation with the given time components. |
| 37 |
Note that the input parameter values are expected to be in the format |
| 38 |
output by localtime/gmtime. Namely, $seconds is 0-60 (yes, there can |
| 39 |
be a leap second value of 60 occasionally), $minutes is 0-59, $hours |
| 40 |
is 0-23, $days is 1-31, $month is 0-11, and $year is 70-127. This |
| 41 |
function is limited in that it will not process dates prior to 1970 or |
| 42 |
after 2037 (that way 32-bit time_t overflow calculations aren't required). |
| 43 |
|
| 44 |
Additional parameters passed to the function are ignored, so it is |
| 45 |
safe to use the full localtime/gmtime output, such as: |
| 46 |
|
| 47 |
$seconds = LW2::time_mktime( localtime( time ) ); |
| 48 |
|
| 49 |
Note: this function does not adjust for time zone, daylight savings |
| 50 |
time, etc. You must do that yourself. |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
sub time_mktime { |
| 55 |
my ($sec,$min,$hour,$day,$mon,$yr)=@_; |
| 56 |
my @md=(0,31,59,90,120,151,181,212,243,273,304,334); |
| 57 |
foreach(@_[0..5]){ |
| 58 |
return -1 if !defined $_ || $_<0; } |
| 59 |
return -1 if($sec>60 || $min>59 || $hour>23 || $day>31 || $mon>11 |
| 60 |
|| $yr>137 || $yr<70); |
| 61 |
$yr += 1900; |
| 62 |
my $res = ($yr-1970)*365+$md[$mon]; |
| 63 |
$res += int(($yr-1969)/4) + int(($yr-1601)/400); |
| 64 |
$res -= int(($yr-1901)/100); |
| 65 |
$res = ($res+$day-1)*24; |
| 66 |
$res = ($res+$hour)*60; |
| 67 |
$res = ($res+$min)*60; |
| 68 |
return $res+$sec; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
=item B<time_gmtolocal> |
| 73 |
|
| 74 |
Params: $seconds_gmt |
| 75 |
|
| 76 |
Return: $seconds_local_timezone |
| 77 |
|
| 78 |
Takes a seconds value in UTC/GMT time and adjusts it to reflect the current |
| 79 |
timezone. This function is slightly expensive; it takes the gmtime() and |
| 80 |
localtime() representations of the current time, calculates the delta |
| 81 |
difference by turning them back into seconds via time_mktime, and then |
| 82 |
applies this delta difference to $seconds_gmt. |
| 83 |
|
| 84 |
Note that if you give this function a time and subtract the return value from |
| 85 |
the original time, you will get the delta value. At that point, you can just |
| 86 |
apply the delta directly and skip calling this function, which is a massive |
| 87 |
performance boost. However, this will cause problems if you have a long |
| 88 |
running program which crosses daylight savings time boundaries, as the DST |
| 89 |
adjustment will not be accounted for unless you recalculate the new delta. |
| 90 |
|
| 91 |
=cut |
| 92 |
|
| 93 |
sub time_gmtolocal { |
| 94 |
my $t = shift; |
| 95 |
my $now = time; |
| 96 |
my $utc = time_mktime(gmtime($now)); |
| 97 |
my $me = time_mktime(localtime($now)); |
| 98 |
return $t - ($utc - $me); |
| 99 |
} |
| 100 |
|