CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
dates!!!!!!!

 



zizo
Novice

Jun 1, 2005, 4:16 PM

Post #1 of 16 (4695 views)
dates!!!!!!! Can't Post

hey guys i got this bit here that prints out the date on each log file but the format of the date is 1-6-2005. i would like it to be 01-06-2005.....how would y ou do this so that it adds that extra zero on there as it will look more tidier wen tryyin to serch the logs..

thnks for your help!



my @t = localtime(time) ;
open(OUTPUT_FILE, ">>/export/home/elsonba/zizo/$t[3]-" . ($t[4]+1) . "-" . ($t[5]+1900) . ".log");


KevinR
Veteran


Jun 1, 2005, 7:30 PM

Post #2 of 16 (4687 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post


Code
use POSIX; 
my $date = strftime("%d-%m-%Y", localtime);
$date;
open(OUTPUT_FILE, ">>/export/home/elsonba/zizo/$date.log");

-------------------------------------------------


zizo
Novice

Jun 1, 2005, 8:27 PM

Post #3 of 16 (4685 views)
Re: [KevinR] dates!!!!!!! [In reply to] Can't Post

where would that go exactly?? above or below the script i showed you!


KevinR
Veteran


Jun 1, 2005, 8:37 PM

Post #4 of 16 (4680 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

without knowing anything about your script except those two lines you posted, put:

use POSIX;

at the beginning of your script (more or less):


#!/usr/bin/perl
use POSIX;


and the other lines put them in your code where they are now:


Code
my $date = strftime("%d-%m-%Y", localtime);  
open(OUTPUT_FILE, ">>/export/home/elsonba/zizo/$date.log");


PS: not sure why I had that extra $date in my original post, it's not needed:


Code
my $date = strftime("%d-%m-%Y", localtime);  
$date; <-- not needed
open(OUTPUT_FILE, ">>/export/home/elsonba/zizo/$date.log");

-------------------------------------------------


zizo
Novice

Jun 2, 2005, 2:10 PM

Post #5 of 16 (4672 views)
Re: [KevinR] dates!!!!!!! [In reply to] Can't Post

ok thnks for ur help but i got this folwiing one working..i just got a problem here with my script..what it does is stores the logs into files daily which is run on a 24 hr machine but i tried my script and my script

does not roll over in such a way when it notices a change in time 12:00am it shoudl create a new file with the new date name and start writting to that new file with the follwoign date...would you be able to help in this one??

this code works fine...it jst doesnt roll over and store to a new file when the date changes in the system..it jst keeps writting to the same file everytime!!



use strict;
use Socket;

# initialize host and port
my $host = shift || '172.16.10.30';
my $port = shift || 5221;

my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET
, $paddr) or die "connect: $!";

my @t = localtime(time) ;

open(OUTPUT_FILE, ">>/dat/elsonba/zizo/" . ($t[5]+1900) . "-" . ($t[4]+1) . "-$t[3].log");


KevinR
Veteran


Jun 2, 2005, 5:27 PM

Post #6 of 16 (4668 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

how is this script going to be aware of the date/time? No where in the script are you comparing the time of the old file with the current time to see if the filename should be updated. And how is this script invoked? What causes it to run? How often does it run?
-------------------------------------------------


zizo
Novice

Jun 2, 2005, 5:31 PM

Post #7 of 16 (4667 views)
Re: [KevinR] dates!!!!!!! [In reply to] Can't Post


In Reply To
how is this script going to be aware of the date/time? No where in the script are you comparing the time of the old file with the current time to see if the filename should be updated. And how is this script invoked? What causes it to run? How often does it run?



ok what i want is little script in which it can predict the tiem cutof so when it gets to midnite a new file is created for the following new day!

adt the moment this script just creates a file for the day and keeps writing logs to it..do you know wht i mean?


KevinR
Veteran


Jun 2, 2005, 5:43 PM

Post #8 of 16 (4662 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

scripts do not make "predictions", at least not in the sense that humans do. You have to make your script aware of the time. You did not answer my questions:

How is this script invoked/What causes it to run?
How often does it run?
-------------------------------------------------


zizo
Novice

Jun 2, 2005, 6:22 PM

Post #9 of 16 (4661 views)
Re: [KevinR] dates!!!!!!! [In reply to] Can't Post

it runs all the time on the commercial system here wehre we have networks...so its capturing logs all the time and redirects them to an output file..


KevinR
Veteran


Jun 2, 2005, 7:57 PM

Post #10 of 16 (4659 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

Well, it looks like it should work now that I take a closer loook. You are creating the filename dynamically each time the script is invoked. When the date rolls over to the next day a new file should be created.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Jun 3, 2005, 7:06 AM

Post #11 of 16 (4655 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

I assume that you're talking about a daemon process that is running constantly. You want to create a new log file for every day.

Something like this should work (untested).


Code
#!/usr/bin/perl 

use strict;
use warnings;
use POSIX 'strftime';

my $date_fmt = '%d-%m-%Y';

my $curr_date = strftime($date_fmt, localtime);
my $file = '/some/path/to/your/logfile';

open LOGFILE, '>', "$file-$curr_date.log" or die $!;

while (1) {
# do whatever your program needs to do, writing log data
# to LOGFILE

# Check the date at the end of each processing loop
my $this_date = strftime($date_fmt, localtime);
# If the date has changed...
if ($this_date eq $curr_date) {
# change the current date variable...
$curr_date = $this_date;
# close the existing logfile...
close LOGFILE;
# and open a new one
open LOGFILE, '>', "$file-$curr_date.log" or die $!;
}
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

Jun 6, 2005, 2:37 PM

Post #12 of 16 (4624 views)
Re: [davorg] dates!!!!!!! [In reply to] Can't Post

wherre would i place that in my code??


davorg
Thaumaturge / Moderator

Jun 6, 2005, 2:47 PM

Post #13 of 16 (4623 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

Well, I thought the comments made that pretty clear, but here's an attempt at clarification.

Any initialisation code you need to execute should go somewhere before the start of the while loop.

The main body of your processing should go in the main loop as indicated by the comment.

If you need to do any processing as the program closes down then it will need to go after the end of the while loop. But you'll need to change the condition of the while loop as currently it never terminates.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

Jun 6, 2005, 2:52 PM

Post #14 of 16 (4622 views)
Re: [davorg] dates!!!!!!! [In reply to] Can't Post

thnks alot for your help and sorry iam a begineer so i hope you bare with me :)
ok below is my actualy code which works perfect by wirting the logs continously to a log file but it
doesnt stop before the start of a new day..i need it to stop writing to the same file which is what is doing now..
so yeah...

#! /usr/bin/perl -w
# client1.pl - a cool client
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || 'xxx';
my $port = shift || xxx;



my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET
, $paddr) or die "connect: $!";

my @t = localtime(time) ;

open(OUTPUT_FILE, ">>/data2/elsonba/zizo/" . ($t[5]+1900) . "-" . ($t[4]+1) . "-
$t[3].log");




select OUTPUT_FILE; $| = 1;

my $line;

while (<SOCKET>)
{
print OUTPUT_FILE "$_\n";
}
close SOCKET or die "close: $!";
close OUTPUT_FILE;


zizo
Novice

Jun 6, 2005, 2:58 PM

Post #15 of 16 (4621 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post

Just having problems organizing the script....:S


davorg
Thaumaturge / Moderator

Jun 7, 2005, 2:17 AM

Post #16 of 16 (4614 views)
Re: [zizo] dates!!!!!!! [In reply to] Can't Post


In Reply To
ok below is my actualy code which works perfect by wirting the logs continously to a log file but it
doesnt stop before the start of a new day..i need it to stop writing to the same file which is what is doing now..
so yeah...


I understand that and I have given you the skeleton of a solution to that problem. Now, it seems to me that you want me to combine my solution with your code to give you the complete answer. I'm not inclined to do that as it would waste my time and wouldn't really help you.

But on the other hand, I'm eally not sure how much clearer I can make my instructions. Both my skeleton code and your program have very similar structures. They have an initialisation section (the part that happens before the "while" loop) and a main processing loop. In your case, you also have some clean-up code that happens after the loop ends.

What I'm saying is that you need to take the pieces of code from skeleton program and insert them in the relevant places of your program. If you don't understand any parts of my code then either read up in the relevant parts of the Perl documentation or ask me to explain it further.

As I think I mentioned before, this sounds like quite a complex project to be taking on as your first Perl program. I still think that you would be better off taking the time to work through a good Perl introductory book like "Learning Perl" or "Beginning Perl". If your employers have an urgent need for a Perl program to be written then they should pay a Perl programmer to write it.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives