LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices


Reply
  Search this Thread
Old 01-02-2013, 02:59 AM   #1
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Rep: Reputation: 18
Exclamation perl date difference: strange behaviour


#!/usr/bin/perl

use Time::Local;

$da1 = "2012-12-27";
$da2 = "2013-01-02";


@DDATE1 = split(/-/, $da1);
@DDATE2 = split(/-/, $da2);
%months = ( '1', 0,
'2', 1,
'3', 2,
'4', 3,
'5', 4,
'6', 5,
'7', 6,
'8', 7,
'9', 8,
'10', 9,
'11', 10,
'12', 11 );

$DATE1 = timelocal(0, 0, 0, $DDATE1[2], $months['$DDATE1[1]'], $DDATE1[0]);
print "$DATE1\n";
$DATE2 = timelocal(0, 0, 0, $DDATE2[2], $months['$DDATE2[1]'], $DDATE2[0]);
print "$DATE2\n";
$diffSeconds = $DATE2 - $DATE1;
print "$diffSeconds\n";
$diffDays = $diffSeconds / (60 * 60 * 24);
print "$diffDays\n";



i'm getting 341 days !!!!


P.S. I'm talking about UNIX platform: AIX 5.3/6.1 and no additional perl modules can be installed
 
Old 01-02-2013, 05:12 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
You aren't using "perl -w".

Incorrect quoting gets both dates treated with an undefined (0) month.
Quote:
$DATE1 = timelocal(0, 0, 0, $DDATE1[2], $months['$DDATE1[1]'], $DDATE1[0]);
You can skip all the %month thing by using "-1".
Code:
$DATE1 = timelocal(0, 0, 0, $DDATE1[2], $DDATE1[1]-1 , $DDATE1[0]);
 
Old 01-02-2013, 05:37 AM   #3
fritz001
Member
 
Registered: Aug 2004
Posts: 176

Original Poster
Rep: Reputation: 18
Exclamation

Quote:
Originally Posted by fritz001 View Post
#!/usr/bin/perl

use Time::Local;

$da1 = "2012-12-27";
$da2 = "2013-01-02";


@DDATE1 = split(/-/, $da1);
@DDATE2 = split(/-/, $da2);
%months = ( '1', 0,
'2', 1,
'3', 2,
'4', 3,
'5', 4,
'6', 5,
'7', 6,
'8', 7,
'9', 8,
'10', 9,
'11', 10,
'12', 11 );

$DATE1 = timelocal(0, 0, 0, $DDATE1[2], $months['$DDATE1[1]'], $DDATE1[0]);
print "$DATE1\n";
$DATE2 = timelocal(0, 0, 0, $DDATE2[2], $months['$DDATE2[1]'], $DDATE2[0]);
print "$DATE2\n";
$diffSeconds = $DATE2 - $DATE1;
print "$diffSeconds\n";
$diffDays = $diffSeconds / (60 * 60 * 24);
print "$diffDays\n";



i'm getting 341 days !!!!


P.S. I'm talking about UNIX platform: AIX 5.3/6.1 and no additional perl modules can be installed

!!! FIXED !!!

WRONG:: $DATE1 = timelocal(0, 0, 0, $DDATE1[2], $months['$DDATE1[1]'], $DDATE1[0]);

GOOD: $DATE1 = timelocal(0, 0, 0, $DDATE1[2], $months{$DDATE1[1]}, $DDATE1[0]);



I've almost forgot how to access the individual elements of a hash....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl connect to SQLite database - strange behaviour totaluser Programming 6 11-10-2011 12:59 AM
ps -ef strange behaviour ?? lionking_x Linux - Newbie 1 11-28-2007 05:22 PM
Strange Behaviour! joshuarowley LQ Suggestions & Feedback 1 12-08-2005 03:36 PM
Strange behaviour Anmol SUSE / openSUSE 2 10-27-2005 11:05 PM
Strange Behaviour mikeyt_3333 Linux - General 4 08-06-2001 03:07 PM

LinuxQuestions.org > Forums > Other *NIX Forums > AIX

All times are GMT -5. The time now is 12:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration