Advertisement

03.27.2008 at 12:19PM PDT, ID: 23275260
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

finding the time difference in Perl

5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      0:10
7      3TB      TB      cust015      3/27/2008      2:30      3/27/2008      2:36      SU      0:06


This is a tab delimited file with the 5th,7th col is the date while 6th,8th col is the time. I want to find the 8th col - 6th col. But the date in the 5th could be yesterdays.

I used this subroutine to find the time difference but I need to handle the date issue too.

sub timediff {
        my ($a , $b) = @_;
        # get hours:minutes:seconds, or just minutes:seconds if no hours
        my @a = $a =~ /(\d+):(\d+)[:]+(\d+)/;
        my @b = $b =~ /(\d+):(\d+)[:]+(\d+)/;
        if(@a and @b) {
                my $ta = $a[0]*60*60 + $a[1]*60 + $a[2];
                my $tb = $b[0]*60*60 + $b[1]*60 + $b[2];
                my $diff = $tb - $ta;
                my $h=int($diff/60/60);
                $diff -= $h*60*60;
                my $m = int($diff/60);
                $diff -= $m*60;
                return sprintf("%02d:%02d:%02d", $h, $m, $diff);
        }

        @a = $a =~ /(\d+):(\d+)/;
        @b = $b =~ /(\d+):(\d+)/;
        my $ta = $a[0]*60 + $a[1];
        my $tb = $b[0]*60 + $b[1];
        my $diff = $tb - $ta;
        my $m = int($diff/60);
        $diff -= $m*60;
        return sprintf("%02d:%02d", $m, $diff);
}


time could be in 00:00:00 or in this format 00:00. Please advise.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: saibsk
Solution Provided By: mjcoyne
Participating Experts: 2
Solution Grade: A
Views: 21
Translate:
Loading Advertisement...
03.27.2008 at 01:53PM PDT, ID: 21225654

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 02:19PM PDT, ID: 21225849

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 02:46PM PDT, ID: 21226095

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 03:17PM PDT, ID: 21226316

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 05:17PM PDT, ID: 21226992

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 06:07PM PDT, ID: 21227192

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 04:08AM PDT, ID: 21229273

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 11:31AM PDT, ID: 21232944

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 11:37AM PDT, ID: 21233008

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 11:55AM PDT, ID: 21233166

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 12:00PM PDT, ID: 21233212

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 12:15PM PDT, ID: 21233326

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 01:03PM PDT, ID: 21233723

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.27.2008 at 01:53PM PDT, ID: 21225654

Rank: Genius

The 00:00:00 time is hours:minutes:seconds, correct?
Is the 00:00 hours:minutes, or minutes:seconds?
 
03.27.2008 at 02:19PM PDT, ID: 21225849
00:00:00 time is hours:minutes:seconds
00:00 is minutes seconds.

But the date in the file could be previous day date say eg

 5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      0:10

could have the 5th col as

 5      3TA      TA      cust011      3/26/2008      2:30      3/27/2008      2:40      SU      0:10
 
03.27.2008 at 02:46PM PDT, ID: 21226095

Rank: Genius

What hours should be assumed when time is given as 00:00?  
eg: what is the difference between 34:56  and  12:34:56?
 
03.27.2008 at 03:17PM PDT, ID: 21226316
Actually the time is military hours. so if it 15:00 it is 3 pm.
 
03.27.2008 at 05:17PM PDT, ID: 21226992

Rank: Genius

What should be done when there are no hours given?

eg: 10:25:36 to 35:36.  Should the 35:36 be assumed to have an hour of 10, for a difference of 0 hours, 10 minutes, 0 seconds, or should the hours be assumed to be 0, for a difference of 10 hours, 10 minutes, 0 seconds?  Or something different?
 
03.27.2008 at 06:07PM PDT, ID: 21227192
The 5th & 7th column is in hh::mm while the difference is mm::ss in the 9th column.
 
03.28.2008 at 04:08AM PDT, ID: 21229273

Rank: Sage

If I run this code:

#!/usr/bin/perl -w
use strict;
use Date::Manip;
$ENV{'TZ'} = 'EST5EDT';

while (<DATA>) {
    my @line = split /\s+/; # you may have to change this to \t+
    my @delta = (split /:/, DateCalc("$line[4] $line[5]", "$line[6] $line[7]"));
    $delta[4] += $delta[3] * 24 if ($delta[3] > 0);
    $line[9] = sprintf("%02d:%02d", $delta[4], $delta[5]);
    print join ("\t", @line), "\n";

}

__DATA__
5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      0:10
7      3TB      TB      cust015      3/26/2008      2:30      3/27/2008      3:36      SU      0:06


I get back:

5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      00:10
7      3TB      TB      cust015      3/26/2008      2:30      3/27/2008      3:36      SU      25:06

so, the elapsed time (field 9) is expressed in hh:mm.  Is that what you're looking for?
 
03.28.2008 at 11:31AM PDT, ID: 21232944
How can I change this to be expressed in hh:mm:ss for the field 9?
 
03.28.2008 at 11:37AM PDT, ID: 21233008

Rank: Sage

I can change it for you, but I'm getting confused...

Given:

5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      0:10

What is 2:30 -- hh:mm or mm:ss?
What is 2:40 -- hh:mm or mm:ss?
What is 0:10 -- hh:mm or mm:ss?

 
03.28.2008 at 11:55AM PDT, ID: 21233166
2:30 is 2:30 am 2:40 is 2:40 am.

I used the code above posted by you to determine the difference ie. the 9th fileds so it would hh:mm. But this 9th field I have to compare against another file where the time is in mm:ss. So could you suggest me what would be the best way to compare?

I thought if i change the 9th field to hh:mm:ss and the data in another file also to hh:mm:ss. So I would have a standard format for the comparision. Please advise.
 
03.28.2008 at 12:00PM PDT, ID: 21233212

Rank: Sage

Ok, so were finding elapsed time by comparing two hh:mm times, so the seconds is always going to be 00, right?  So, this does that:

#!/usr/bin/perl -w
use strict;
use Date::Manip;
$ENV{'TZ'} = 'EST5EDT';

# assumes e.g. 2:30 & 2:40 are hh:mm, field [9] (elapsed time) in hh:mm:ss

while (<DATA>) {
    my @line = split /\s+/; # you may have to change this to \t+
    my @delta = (split /:/, DateCalc("$line[4] $line[5]", "$line[6] $line[7]"));
    $delta[4] += $delta[3] * 24 if ($delta[3] > 0);
    $line[9] = sprintf("%02d:%02d:%02d", $delta[4], $delta[5], 0);
    print join ("\t", @line), "\n";

}

__DATA__
5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      0:10
7      3TB      TB      cust015      3/26/2008      2:30      3/27/2008      3:36      SU      0:06

Returns:

5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      00:10:00
7      3TB      TB      cust015      3/26/2008      2:30      3/27/2008      3:36      SU      25:06:00

so the elapsed time for the first line, as shown in field 9, is 0 hours, 10 minutes, and 0 seconds, whereas the elapsed time for the second line is 25 hours, 6 minutes, and 0 seconds.
 
03.28.2008 at 12:15PM PDT, ID: 21233326
Currently the time in the file is hh:mm format so we could ss is 0 always. But this could change and the ss might not zero.

so if the above file changes to
5      3TA      TA      cust011      3/27/2008      2:30:00      3/27/2008      2:40:12      SU      00:10:12

What changes do I have to make in the above code? Sorry about my bunch of questions.
 
03.28.2008 at 01:03PM PDT, ID: 21233723

Rank: Sage

#!/usr/bin/perl -w
use strict;
use Date::Manip;
$ENV{'TZ'} = 'EST5EDT';

# assumes e.g. 2:30 & 2:40 are hh:mm, field [9] (elapsed time) in hh:mm:ss

while (<DATA>) {
    my @line = split /\s+/; # you may have to change this to \t+
    my @delta = (split /:/, DateCalc("$line[4] $line[5]", "$line[6] $line[7]"));
    $delta[4] += $delta[3] * 24 if ($delta[3] > 0);
    $line[9] = sprintf("%02d:%02d:%02d", $delta[4], $delta[5], $delta[6]);
    print join ("\t", @line), "\n";

}

__DATA__
5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      0:10
7      3TB      TB      cust015      3/26/2008      2:30      3/27/2008      3:36      SU      0:06
5      3TC      TA      cust011      3/27/2008      2:30:00      3/27/2008      2:40:12      SU      00:10:12

returns:

5      3TA      TA      cust011      3/27/2008      2:30      3/27/2008      2:40      SU      00:10:00
7      3TB      TB      cust015      3/26/2008      2:30      3/27/2008      3:36      SU      25:06:00
5      3TC      TA      cust011      3/27/2008      2:30:00      3/27/2008      2:40:12      SU      00:10:12

So it now works with or without seconds....
Accepted Solution
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628