Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A different output for this sub

Status
Not open for further replies.

BigDoug

IS-IT--Management
Feb 20, 2002
49
US
I need some help from anybody who can. Here is what I have. I have a sub that grabs the time remaining for an item in a database, and displays the time remaining as 6 Days 19 Hrs.+. What I need is the time remaining to display as when it closes instead (Month/Date) like 01/29 for January 29th. Can anybody render some help on this stumping problem. Believe me I have tried to do this, but it is beyond my skills. Here is the sub:

Code:
sub time_remain($){
    my $diff = $_[0] - (time + ($config{'timediff'} * 3600));
    if($diff < 0){ return &quot;Item closed&quot; }
    my $days  = int ( $diff                                  / 86400);
    my $hours = int (($diff - $days * 86400)                 / 3600 );
    my $mins  = int (($diff - $days * 86400 - $hours * 3600) / 60   );
    if($days > 1){
        return &quot;$days Days $hours Hrs.+&quot;;
    }elsif($days == 1){
        return &quot;1 Day $hours Hrs.+&quot;;
    }elsif($hours > 12){
        return &quot;$hours Hrs. $mins Min+&quot;;
    }elsif($hours > 0){
        return &quot;$hours Hrs. $mins Min+&quot;;
    }else{
        my $secs = int ($diff-($days*86400)-($hours*3600)-($mins*60));
        return &quot;$mins Min $secs Sec+&quot;;
    }
}
 
You can use the Time::CTime module and the [tt]strftime()[/tt] function that it provides. Assuming that argument that you pass in is seconds since the epoch for the system you're on, you could do something like:

[tt]use Time::CTime;

sub time_remain($) {
print strftime(&quot;%m/%d&quot;, localtime($_[0]));
}
[/tt]

The first parameter to strftime is a format string, which provides numerous options for date/time formatting.
 
I'm sorry, but you totally lost me.
 
[tt]time[/tt] returns the number of seconds since the epoch (which is really just some date and time a bunch of years back.) The reason I mentioned it at all is because of your code:
[tt](time + ($config{'timediff'} * 3600)) [/tt]

Let me just give you a quick example of a script that uses [tt]strftime()[/tt]:

[tt]#!/bin/perl
use Time::CTime;

print strftime(&quot;%m/%d&quot;, localtime(time));
[/tt]

Right now, for me, this prints &quot;01/25&quot;.

You can take this same function and replace [tt]time[/tt] with any variable that represents the time.

For interest, you can try: [tt]print time;[/tt]

On my system, right now, I get 1043548062. It'll go up by one every second.

So, you can replace [tt]time[/tt] with a variable or a constant number if you like. Say:
[tt]print strftime(&quot;%m/%d&quot;, localtime(1040000000)); [/tt]

This gives me 12/15.

Does this clear things up?
 
I think I see the light. My only problem is I don't have the Ctime module. Can I use anything else? This does make sense, and I could probably make it work if I could use Ctime, but I can't.
 
This is a call for help. This is driving me nuts. I thought what I needed to do would be simple but it is proving way beyond my skills. All I need is this. Currently if the item in the database is ending today in 12 hours and 38 minutes, then the time left displays as 12 Hrs. 38 Min., but I need the time left to show as 01/26 meaning it ends today. If the time left is 4 days and 23 Hrs. I need the time remaining to show as 01/31 and so on. Is there any easy way to just alter the current sub I have to display the time left as a day and month instead of days and hours and munutes? I know I am asking a lot, but I can't figure it out and I have to do this conversion come hell or high water. If anybody has the solution, please throw it out at me. You will have my eternal admiration for being able to accomplish something I could not (and probably easier than I thought it was). Here is the sub again:

sub time_remain($){
my $diff = $_[0] - (time + ($config{'timediff'} * 3600));
if($diff < 0){ return &quot;Auction closed&quot; }
my $days = int ( $diff / 86400);
my $hours = int (($diff - $days * 86400) / 3600 );
my $mins = int (($diff - $days * 86400 - $hours * 3600) / 60 );
if($days > 1){
return &quot;$days Days $hours Hrs.+&quot;;
}elsif($days == 1){
return &quot;1 Day $hours Hrs.+&quot;;
}elsif($hours > 12){
return &quot;$hours Hrs. $mins Min+&quot;;
}elsif($hours > 0){
return &quot;$hours Hrs. $mins Min+&quot;;
}else{
my $secs = int ($diff-($days*86400)-($hours*3600)-($mins*60));
return &quot;$mins Min $secs Sec+&quot;;
}
}
 
You can use [tt]localtime()[/tt] to get what you need. If the $_[0] is the expiration time for your function:

[tt]my ($sec, $min, $hour, $mday, $mon, $year) =
localtime($_[0]);
$mon++;
print &quot;Auction ends $mon/$mday\n&quot;;
[/tt]

Note the increment of [tt]$mon[/tt] since the return from [tt]localtime()[/tt] is for the month to range from 0 to 11.
 
That is excellent.............except for one thing. It displays the right date now to STDOUT, but it does not generate the right date on the script output. The display to screen shows as 01/26 (or whatever the date is) but the script writes to the file a &quot;1&quot; for every date instead of the date.
 
I got it! I needed to add an if statement. Thank you very much for your help. You are my hero!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top