xmltv/lib Date.pm,1.5,1.6

Geoff <[email protected]>
Newsgroups gmane.comp.tv.xmltv.cvs
Message-ID <[email protected]>
Update of /cvsroot/xmltv/xmltv/lib
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv11065

Modified Files:
	Date.pm 
Log Message:
added some subroutines for XMLTV/ISO8601 date conversion

Index: Date.pm
===================================================================
RCS file: /cvsroot/xmltv/xmltv/lib/Date.pm,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** Date.pm	24 Apr 2010 12:28:22 -0000	1.5
--- Date.pm	22 May 2014 08:05:14 -0000	1.6
***************
*** 13,21 ****
  
  package XMLTV::Date;
  use warnings;
  use strict;
  use Carp qw(croak);
  use base 'Exporter';
! our @EXPORT = qw(parse_date);
  use Date::Manip;
  
--- 13,26 ----
  
  package XMLTV::Date;
+ 
+ # use version number for feature detection:
+ # 0.005066 : added time_xxx subs
+ our $VERSION = 0.005066;
+ 
  use warnings;
  use strict;
  use Carp qw(croak);
  use base 'Exporter';
! our @EXPORT = qw(parse_date time_xmltv_to_iso time_iso_to_xmltv time_xmltv_to_epoch time_iso_to_epoch);
  use Date::Manip;
  
***************
*** 106,108 ****
--- 111,222 ----
  }
  
+ 
+ 
+ =pod
+ 
+ =head1 C<time_xmltv_to_iso()>
+ 
+ Converts a XMLTV time  e.g. "20140412090000 +0300"
+ to ISO format i.e. "2014-04-12T09:00:00.000+03:00"
+ 
+ Argument is string time to convert.
+ 
+ =cut
+ sub time_xmltv_to_iso ( $ )
+ {
+ 		$_[0] =~ m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\s([\+-])(\d{2})(\d{2})$/;
+ 		return "$1-$2-$3T$4:$5:$6.000$7$8:$9";
+ }
+ 
+ 
+ =pod
+ 
+ =head1 C<time_iso_to_xmltv()>
+ 
+ Converts an ISO time e.g. "2014-04-12T09:00:00.000+03:00" 
+ to XMLTV format, i.e.  "20140412090000 +0300"
+ 
+ Argument is string time to convert.
+ 
+ =cut
+ sub time_iso_to_xmltv ( $ )
+ {
+     my $time = shift;
+ 		$time =~ s/[:-]//g;
+ 		$time =~ /^(\d{8})T(\d{6}).*(\+\d{4})$/;
+ 		return $1.$2.' '.$3;
+ }
+ 
+ 
+ =pod
+ 
+ =head1 C<time_xmltv_to_epoch()>
+ 
+ Converts a XMLTV time  e.g. "20140412090000 +0300"
+ to seconds since the epoch
+ 
+ (uses POSIX::mktime rather than Date::Manip to avoid issues with the latter)
+ 
+ Alternatively you could use DateTime::Format::XMLTV on CPAN
+ 
+ Argument is string time to convert.
+ Optional 2nd argument: set to 1 ignore the tz offset in the calculation
+ 
+ =cut
+ sub time_xmltv_to_epoch ( $;$ )
+ {
+ 		my $time = shift;
+ 		my $ignoreoffset = shift;		# set to 1 to ignore tz offset (i.e. 'local' epoch; else will get utc)
+ 		
+ 		my ($y, $m, $d, $h, $i, $s, $t, $th, $tm) = $time =~ 
+ 		  m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\s([\+-])(\d{2})(\d{2})$/;
+ 		$y -= 1900; $m -= 1;	# re-base for mktime()
+ 		use POSIX qw(mktime);
+ 		my $epoch = POSIX::mktime($s, $i, $h, $d, $m, $y);
+ 		
+ 		if (!defined $ignoreoffset || !$ignoreoffset) {
+ 			# note this isn't exact since it doesn't account for leap seconds, etc
+ 			my $offset = ($th * 3600) + ($tm * 60);
+ 			$epoch += $offset  if $t eq '-';
+ 			$epoch -= $offset  if $t eq '+';
+ 		}
+ 		return $epoch;
+ 		
+ }
+ 
+ 
+ =pod
+ 
+ =head1 C<time_iso_to_epoch()>
+ 
+ Converts an iso time (e.g. "2014-04-12T09:00:00.000+03:00") to epoch time
+ 
+ (uses POSIX::mktime rather than Date::Manip to avoid issues with the latter)
+ 
+ Alternatively you could use DateTime::Format::XMLTV on CPAN
+ 
+ Argument is string time to convert.
+ Optional 2nd argument: set to 1 ignore the tz offset in the calculation
+ 
+ =cut
+ sub time_iso_to_epoch ( $;$ )
+ {
+ 		my $time = shift;
+ 		my $ignoreoffset = shift;		# set to 1 to ignore tz offset (i.e. 'local' epoch; else will get utc)
+ 		
+ 		my ($y, $m, $d, $h, $i, $s, $ms, $t, $th, $tm) = $time =~
+ 		  m/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})([\+-])(\d{2}):(\d{2})$/;
+ 		$y -= 1900; $m -= 1;	# re-base for mktime()
+ 		use POSIX qw(mktime);
+ 		my $epoch = POSIX::mktime($s, $i, $h, $d, $m, $y);
+ 		
+ 		if (!defined $ignoreoffset || !$ignoreoffset) {
+ 			# note this isn't exact since it doesn't account for leap seconds, etc
+ 			my $offset = ($th * 3600) + ($tm * 60);
+ 			$epoch += $offset  if $t eq '-';
+ 			$epoch -= $offset  if $t eq '+';
+ 		}
+ 		return $epoch;
+ }
+ 
  1;


------------------------------------------------------------------------------
"Accelerate Dev Cycles with Automated Cross-Browser Testing - For FREE
Instantly run your Selenium tests across 300+ browser/OS combos.
Get unparalleled scalability from the best Selenium testing platform available
Simple to use. Nothing to install. Get started now for free."
http://p.sf.net/sfu/SauceLabs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.