Creating a date (without time) in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a date (without time) in perl
# 1  
Old 04-14-2011
Creating a date (without time) in perl

I have a perl script that automatically runs on Mondays.
I need to have it create a variable for last Monday's date thru that Sunday's date.

example: 04-01-2011 thru 04-08-2011

Its reporting numbers for the previous week beginning with Monday and ending on Sunday. So i dont have to go in each week and manually enter the date range in before the script runs.
I no nothing about perl except that the script is in perl.
# 2  
Old 04-14-2011
I'm not sure how wise it is for you to modify a perl script if you literally know nothing about perl. But here is a way to generate those dates:
Code:
$
$ date
Thu Apr 14 17:30:28 EDT 2011
$ cat range
#! /usr/bin/perl -w

@d=localtime (time() - (60*60*24*7) );
printf "%02d-%02d-%4d\n", $d[4]+1,$d[3],$d[5]+1900;
@d=localtime (time() - (60*60*24) );
printf "%02d-%02d-%4d\n", $d[4]+1,$d[3],$d[5]+1900;

$ ./range
04-07-2011
04-13-2011
$

Now that I posted my script, the perl experts will come around and show how it's really done. Smilie
# 3  
Old 04-14-2011
and that will automatically change the range each week?
# 4  
Old 04-14-2011
Quote:
Originally Posted by bbraml
and that will automatically change the range each week?
Yep. time() - (60*60*24*7) That says take the current time and then back up 7 days.
# 5  
Old 04-14-2011
Of course, you must run script on the monday to get mon-sun of Previous week. If, for example, you dont run the program until 1am on Tue you will get dates for Tue(last week) to Mon(yesterday).

Slight change to always give MON thru SUN dates:
Code:
#!/usr/bin/perl
@d=localtime( time() );
@e=localtime(time() - (24*3600*$d[6]) ) ;
@s=localtime(time() - (24*3600*($d[6]+6)) ) ;
printf("%02d-%02d-%4d\n", $s[4]+1, $s[3], $s[5]+1900);
printf("%02d-%02d-%4d\n", $e[4]+1, $e[3], $e[5]+1900);

# 6  
Old 04-15-2011
thank you both. I can use either because they do exactly what I need.

You're awesome!

---------- Post updated at 03:52 PM ---------- Previous update was at 07:50 AM ----------

one more thing, how would I make that printf statement a variable?
That way I can just call the variable and not use that long string...
# 7  
Old 04-15-2011
Not sure I understand the request, but maybe....
Code:
$ cat range
#! /usr/bin/perl -w

@d=localtime (time() - (60*60*24*7) );
$start = sprintf "%02d-%02d-%4d\n", $d[4]+1,$d[3],$d[5]+1900;
@d=localtime (time() - (60*60*24) );
$end = sprintf "%02d-%02d-%4d\n", $d[4]+1,$d[3],$d[5]+1900;


print "start = ", $start;
print "end = ", $end;

$
$
$
$
$ ./range
start = 04-08-2011
end = 04-14-2011
$

This User Gave Thanks to Perderabo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl:Script to append date and time stamp

Help with Perl script : I have a web.xml file with a line <display-name>some_text_here</display-name> Need to append the current date and time stamp to the string and save the XML file Something like <display-name>some_text_here._01_23_2014_03_56_33</display-name> -->Finally want... (5 Replies)
Discussion started by: gaurav99
5 Replies

2. Shell Programming and Scripting

Perl : Module to get the last week date and time

Hello folks, I am looking for a Perl module or a program logic that gives the startdate and enddate of the last week.. Suppose say, assuming this week starts from Sunday to Saturday If I execute the script in this week. I need to get the last week sunday's date. Could anyone please... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

PERL, Date & Time issues

Hello All, This is my first script in PERL. Hence require your help in moving further. I have a script which should populate the values for Today, Yesterday output. For which I use timeFrame as a variable to obtain the time in hrs:mm as 10:00. All I want is, I want my timeFrame to start... (4 Replies)
Discussion started by: sathyaonnuix
4 Replies

4. Shell Programming and Scripting

perl : searching for month and storing the date and time in an array

I am writing the code in perl. I have an array in perl and each variable in the array contains the data in the below format Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug'... (5 Replies)
Discussion started by: giridhar276
5 Replies

5. Shell Programming and Scripting

How to find last updated date and time of a folder in Perl?

Hi All, I have a process which after some time continues move a files to some folder(say the name of the folder is logdir) What i am trying to do is as the files are coming to the logdir folder, I want the latest updated time and date of the folder in PERL. (1 Reply)
Discussion started by: parthmittal2007
1 Replies

6. UNIX for Dummies Questions & Answers

Formatting date time in unix using perl breaks

while read l do vTimeCreated=`perl -e '@d=localtime ((stat(shift))); printf "%02d-%02d-%04d %02d:% 02d:%02d\n", $d,$d+1,$d+1900,$d,$d,$d' ${l}` echo "${l} || ${vTimeCreated}" >> ${fPrefx}_Output_Files_${vDate}.txt done < servername.txt Using the above code to format date time for each of... (5 Replies)
Discussion started by: HeadBang
5 Replies

7. Shell Programming and Scripting

Date and Time in PERL

Hi, I want to get the current date and time and subtract 1 day to get to the previous day? I see timelocal( ) and (time) etc. How do I code this in PERL to get the previous day? Thanks Nurani (2 Replies)
Discussion started by: nurani
2 Replies

8. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

9. Shell Programming and Scripting

perl DBI inserting date\time

hi there, my mysql database has a date/time field using the standard mysql date|time format of 2009-08-31 00:16:44 when inserting into this field using perl DBI, is there is an easy way to insert the current date/time in without having to preformat the date/time string in perl before... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

10. Shell Programming and Scripting

Perl - naming a file after the date/time

I'm making log-files for 4different hp-ux's and I need to name the files after the date so I can keep the files. The way I want the filename to look is like this: yymmdd.log ( 020522<CompName>.log ). Can anyone please give me a helping hand. :confused: I have made the log-script and... (1 Reply)
Discussion started by: tgreig
1 Replies
Login or Register to Ask a Question