| 1 | # -*- perl -*- | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # $Id: Date.pm,v 1.3 2003/01/16 14:29:10 eserte Exp $ | ||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | # Copyright (C) 2001 Online Office Berlin. All rights reserved. | ||||||||||||||
| 8 | # Copyright (C) 2002 Slaven Rezic. | ||||||||||||||
| 9 | # This is free software; you can redistribute it and/or modify it under the | ||||||||||||||
| 10 | # terms of the GNU General Public License, see the file COPYING. | ||||||||||||||
| 11 | |||||||||||||||
| 12 | # | ||||||||||||||
| 13 | # Mail: slaven@rezic.de | ||||||||||||||
| 14 | # WWW: http://we-framework.sourceforge.net | ||||||||||||||
| 15 | # | ||||||||||||||
| 16 | |||||||||||||||
| 17 | package WE::Util::Date; | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 NAME | ||||||||||||||
| 20 | |||||||||||||||
| 21 | WE::Util::Date - date-specific functions | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | use WE::Util::Date; | ||||||||||||||
| 26 | print scalar localtime isodate2epoch("2001-12-12 12:23:00"); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||
| 29 | |||||||||||||||
| 30 | This is a helper class for date functions. | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =cut | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 16 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 16 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 16 | ▶ | use vars qw($VERSION @EXPORT @EXPORT_OK); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 16 | $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 16 | @EXPORT = qw(isodate2epoch epoch2isodate); | |||||||||||||
| 41 | 16 | @EXPORT_OK = qw(short_readable_time); | |||||||||||||
| 42 | |||||||||||||||
| 43 | 16 | ▶ | use constant T_FMT => "%04d-%02d-%02d %02d:%02d:%02d"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | # some methods stolen from DateHelper.pm elsewhere | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =head1 FUNCTIONS | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =head2 isodate2epoch($isodate) | ||||||||||||||
| 50 | |||||||||||||||
| 51 | Return time in seconds since UNIX epoch for the given ISO 8601 string | ||||||||||||||
| 52 | (YYYY-MM-DD HH:MM:SS or YYYY-MM-DD). If Date::Manip is installed, more | ||||||||||||||
| 53 | ISO formats are allowed. | ||||||||||||||
| 54 | |||||||||||||||
| 55 | =cut | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub isodate2epoch { | ||||||||||||
|
Subroutine
isodate2epoch: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 58 | 0 | my $isodate = shift; | |||||||||||||
| 59 | 0 | require Time::Local; | |||||||||||||
| 60 | 0 | my($y,$m,$d,$H,$M,$S); | |||||||||||||
| 61 | 0 | ▶ | if ($isodate =~ /^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | 0 | ($y,$m,$d,$H,$M,$S) = ($1,$2,$3,$4,$5,$6); | |||||||||||||
| 63 | } elsif ($isodate =~ /^(\d{4})-(\d{2})-(\d{2})$/) { # crippled without time | ||||||||||||||
| 64 | 0 | ($y,$m,$d,$H,$M,$S) = ($1,$2,$3,0,0,0); | |||||||||||||
| 65 | } else { | ||||||||||||||
| 66 | 0 | require Date::Manip; | |||||||||||||
| 67 | 0 | return Date::Manip::ParseDate($isodate); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | 0 | Time::Local::timelocal($S,$M,$H,$d,$m-1,$y-1900); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =head2 epoch2isodate($time) | ||||||||||||||
| 73 | |||||||||||||||
| 74 | Return time as ISO 8601 date from given time in seconds since UNIX epoch. | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =cut | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 1 | ▶ | sub epoch2isodate { | ||||||||||||
|
Subroutine
epoch2isodate: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||
| 79 | 1 | ▶ | my @l = @_ ? localtime $_[0] : localtime; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 1 | sprintf(T_FMT, | |||||||||||||
| 81 | $l[5]+1900, $l[4]+1, $l[3], | ||||||||||||||
| 82 | $l[2], $l[1], $l[0]); | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head2 short_readable_time($epoch) | ||||||||||||||
| 86 | |||||||||||||||
| 87 | Return the short time E<agrave> la "ls -al". (Not exported by default). | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =cut | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | ▶ | sub short_readable_time { | ||||||||||||
|
Subroutine
short_readable_time: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 92 | 0 | my $epoch = shift; | |||||||||||||
| 93 | 0 | my @l = localtime $epoch; | |||||||||||||
| 94 | 0 | my @now = localtime; | |||||||||||||
| 95 | 0 | my $s = sprintf "%3s %2d ", | |||||||||||||
| 96 | [qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/]->[$l[4]], | ||||||||||||||
| 97 | $l[3]; | ||||||||||||||
| 98 | 0 | ▶ | if ($l[5] == $now[5]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 0 | $s .= sprintf "%02d:%02d", $l[2], $l[1]; | |||||||||||||
| 100 | } else { | ||||||||||||||
| 101 | 0 | $s .= sprintf "%5d", $l[5]+1900; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 0 | $s; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 16 | 1; | |||||||||||||
| 107 | |||||||||||||||
| 108 | __END__ | ||||||||||||||