Perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script
# 15  
Old 12-23-2011
Epoch is a reference point which is set as 01-Jan-1970 00:00:00 on Unix systems. Time in seconds from epoch refers to the number of seconds that has passed since epoch. So, 1324636284 refers to Fri Dec 23 10:31:24 2011. Work your math and see if time in seconds from 01-Jan-2011 00:00:00 to 23-Dec-2011 10:31:24 is 1324636284.

'timelocal' is a routine available in the Time::Local module of Perl. This routine takes the following parameters: sec, min, hour, mday, mon, year. 'mday' refers to date since beginning of month. 'mon' refers to month (0 for Jan through to 11 for Dec). That's why I've put '$mt - 1'. This routine with all these parameters will return time in seconds since epoch.

If input date is 12/22/2011 (mm/dd/yyyy), then timelocal(0, 0, 0, 22, 11, 2011) would return 1324512000.

'localtime' is a built-in function in Perl adapted from C's library time.h. Read man pages of localtime to see the values this function returns. The 7th element (referred by index #6) is a number, which is the count of number of days that has passed since beginning of current week. So, if you were to use localtime on a Thursday, 7th element from returned list would be 4. Thursday is the 5th day from Sunday (with Sun as 0, Thu is 4).

Since your requirement was to refer Monday as start of week, I subtracted one from this number 4 in line 16, to mean that Mon was 4 days before Thu. Now (4 * 86400) refers to number of seconds in 4 days. This value subtracted from 1324512000 would give number of seconds passed from epoch till Mon i.e. from 01-Jan-1970 to 19-Dec-2011.

End of week is Sunday. We need to find how many days to go from Thu to following Sunday. This is taken care in line 17 by (6 - ((localtime ($sec))[6] - 1). (6 - (4 - 1)) = 3. So, 3 days to go from Thu to Sun. We're subtracting from 6 because, localtime considers Sun as start of week referred to by 0. Sun - 0 ... Mon - 1 ... Thu - 4 ... Sat - 6.

1324512000 + ( 3 * 86400 ) gives number of seconds passed from epoch till Sun i.e. from 01-Jan-1970 to 25-Dec-2011

If time in seconds is passed as a parameter to localtime, the return value if captured as a scalar variable would be date in human readable format which is what line 19 and 20 does.

I saw in another thread that you wanted to calculate the previous weeks' start/end date and next weeks' start/end date. This is plain math from here on.

Just out of curiosity, which school/college are you studying in?
This User Gave Thanks to balajesuri For This Post:
# 16  
Old 12-23-2011
@balajesuri:
just tell me one more thing how to find month start date and month end date of current date which is given at run time...

---------- Post updated at 08:09 AM ---------- Previous update was at 08:04 AM ----------

hi have find out the answers of my another thread...Your post help me a lot...Thanks again for that.
Can you give me some idea about to find month start date and month end date of current date which is given at run time...
I study in PEC, Chandigarh.
# 17  
Old 12-24-2011
C'mon kiddo. Give it a try. How would you find that out with pen & paper? What is the algorithm that you would follow to find the start and end date of current month? Wouldn't it depend on what month it is? Wouldn't it depend on whether the year is a leap year or not? So, you've to incorporate all these conditions (and probably more) in your pseudo code. Once you're done with the pseudo-code, Perl would be just another tool (albeit, a good one) to implement your ideas. Let us know the sugar & spice that you've gathered so far Smilie
# 18  
Old 12-24-2011
hurray i have done....

---------- Post updated at 12:40 PM ---------- Previous update was at 12:37 PM ----------

Code:
#!/etc/edi/bin/perl -w
 use Date::Manip;
 use Time::Local;
  my ($mt, $dt, $yr, $sec, $wk_st, $wk_nd, $st_date, $nd_date);
   
   (@ARGV != 1) && die "Invalid parameters. Enter date in mm/dd/yyyy format. Exiting";
   ($ARGV[0] !~ /^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/[\d]{4}$/)
   && die "Invalid date format. Enter date in mm/dd/yyyy. Exiting";
    
    $mt = substr $ARGV[0], 0, 2;
    $dt = substr $ARGV[0], 3, 2;
    $yr = substr $ARGV[0], 6, 4;
     
$s=`cal $mt $yr | head -3 | tail -1`;
$s =~ s/^\s+//;
$sd=substr($s,0,1);
print "current month start date: $mt/$sd/$yr";
print "\n";
$e=`cal $mt $yr | tail -2 | head -1`;
$e =~ s/\s+$//;
$ed=substr($e,-2,2);
print "Current month end date: $mt/$ed/$yr";
print "\n";
$mn=$mt+1;
$yn=$yr;
$mt=$mt-1;
if ( $mt == 0 )
  {
   $mt =12;
   $yr=$yr-1;
}

$ps=`cal $mt $yr | head -3 | tail -1`;
$ps =~ s/^\s+//;
$psd=substr($ps,0,1);
print "previous month start date: $mt/$psd/$yr";
print "\n";
$es=`cal $mt $yr | tail -2 | head -1`;
$es =~ s/\s+$//;
$ees=substr($es,-2,2);
print "prevoius end date: $mt/$ees/$yr";
print "\n";
if ( $mn == 13 )
  {
   $mn =1;
   $yn=$yn+1;
}

$ns=`cal $mn $yn | head -3 | tail -1`;
$ns =~ s/^\s+//;
$nsd=substr($ns,0,1);
print "next month start date: $mn/$nsd/$yn";
print "\n";
$nes=`cal $mn $yn | tail -2 | head -1`;
$nes =~ s/\s+$//;
$nees=substr($nes,-2,2);
print "next month end date: $mn/$nees/$yn";


Last edited by Franklin52; 12-24-2011 at 02:13 PM.. Reason: Please use code tags for data and code samples, thank you
# 19  
Old 12-24-2011
Quote:
Originally Posted by parthmittal2007
...I have a date 12/22/2011(mm/dd/yyyy) what i have to do is to find
the start date of the week in which that date lies and end date of that week. In this scenario the start date will be 12/19/2011 and the end date will be 12/25/2011...
...
...
...
how to find month start date and month end date of current date which is given at run time...
...
...
Code:
C:\>
C:\> REM the script
 
C:\> type date_arithmetic.pl
#!perl -w
# Usage: perl date_arithmetic.pl YYYY MM DD
use strict;
use DateTime;
 
# Takes as arguments:
#  - The date
#  - The target day (1 is Monday, 7 Sunday)
#  - The day that we want to call the start of the week (1 is Monday, 7 Sunday)
sub get_day_in_same_week {
  my ($dt, $target, $start_of_week) = @_;
 
  # Work out what day the date is within the (corrected) week
  my $wday = ($dt->day_of_week() - $start_of_week + 7) % 7;
 
  # Correct the argument day to our week
  $target = ($target - $start_of_week + 7) % 7;
 
  # Then adjust the current day
  return $dt->clone()->add(days => $target - $wday);
}
 
my ($yr, $mt, $dt) = @ARGV;
my $dt1 = DateTime->new (
   year   => $yr,
   month  => $mt,
   day    => $dt
);
 
print "Given date         = ", $dt1->ymd,"\n";
print "Start of the week  = ", get_day_in_same_week($dt1, 1, 1)->ymd,"\n";
print "End   of the week  = ", get_day_in_same_week($dt1, 7, 1)->ymd,"\n";
print "First day of month = ", DateTime->new( year => $yr, month => $mt, day => 1 )->ymd,"\n";
print "Last  day of month = ", DateTime->last_day_of_month( year => $yr, month => $mt )->ymd,"\n";
 
C:\>
C:\> REM a few test runs...
C:\> perl date_arithmetic.pl 2011 12 22
Given date         = 2011-12-22
Start of the week  = 2011-12-19
End   of the week  = 2011-12-25
First day of month = 2011-12-01
Last  day of month = 2011-12-31
 
C:\>
C:\> perl date_arithmetic.pl 2011 7 1
Given date         = 2011-07-01
Start of the week  = 2011-06-27
End   of the week  = 2011-07-03
First day of month = 2011-07-01
Last  day of month = 2011-07-31
 
C:\>
C:\> perl date_arithmetic.pl 2011 2 4
Given date         = 2011-02-04
Start of the week  = 2011-01-31
End   of the week  = 2011-02-06
First day of month = 2011-02-01
Last  day of month = 2011-02-28
 
C:\>
C:\> perl date_arithmetic.pl 2012 2 27
Given date         = 2012-02-27
Start of the week  = 2012-02-27
End   of the week  = 2012-03-04
First day of month = 2012-02-01
Last  day of month = 2012-02-29
 
C:\>
C:\> perl date_arithmetic.pl 2011 1 1
Given date         = 2011-01-01
Start of the week  = 2010-12-27
End   of the week  = 2011-01-02
First day of month = 2011-01-01
Last  day of month = 2011-01-31
 
C:\>
C:\>

tyler_durden

Last edited by durden_tyler; 12-24-2011 at 05:11 PM..
# 20  
Old 12-24-2011
perl code:
  1. #! /usr/bin/perl -w
  2. use strict;
  3. use Time::Local;
  4.  
  5. my ($mt, $dt, $yr, $sec, $wk_st, $wk_nd, $st_date, $nd_date);
  6. my ($mt_st, $mt_nd, $mt_st_date, $mt_nd_date);
  7.  
  8. (@ARGV != 1) && die "Invalid parameters. Enter date in mm/dd/yyyy format. Exiting";
  9. ($ARGV[0] !~ /^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/[\d]{4}$/)
  10. && die "Invalid date format. Enter date in mm/dd/yyyy. Exiting";
  11.  
  12. $mt = substr $ARGV[0], 0, 2;
  13. $dt = substr $ARGV[0], 3, 2;
  14. $yr = substr $ARGV[0], 6, 4;
  15.  
  16. $sec = timelocal (0, 0, 0, $dt, $mt - 1, $yr);
  17. $wk_st = $sec - (((localtime ($sec))[6] - 1) * 86400);
  18. $wk_nd = $sec + ((6 - ((localtime ($sec))[6] - 1)) * 86400);
  19.  
  20. $st_date = localtime ($wk_st);
  21. $nd_date = localtime ($wk_nd);
  22.  
  23. print "Week Start date: $st_date\n";
  24. print "Week End date: $nd_date\n";
  25.  
  26. $mt_st = $sec - (($dt-1) * 86400);
  27. if ($mt == 12) {
  28.     $mt = 1;
  29.     $yr= $yr + 1;
  30. }
  31. else {
  32.     $mt++;
  33. }
  34. $mt_nd = timelocal (0, 0, 0, 1, $mt - 1, $yr) - 86400;
  35.  
  36. $mt_st_date = localtime ($mt_st);
  37. $mt_nd_date = localtime ($mt_nd);
  38.  
  39. print "Month Start Date: $mt_st_date\n";
  40. print "Month End Date: $mt_nd_date\n";

Cheers!

Last edited by balajesuri; 12-24-2011 at 11:29 PM..
# 21  
Old 12-25-2011
good one Smilie

Last edited by parthmittal2007; 12-25-2011 at 02:16 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Excuting perl script from within a perl script with variables.

Not sure what I am doing wrong here, but I can print the list with no issue. Just a blank screen with the 'do'. #!/usr/bin/perl open FILE, "upslist.txt"; while ($line=<FILE>){ if ($line=~/^(.*?),(.*?)$/){ #print "ups:$1 string:$2\n"; do 'check_snmp_mgeups-0.1.pl -H $1 -C $2'; } ... (1 Reply)
Discussion started by: mrlayance
1 Replies

3. Shell Programming and Scripting

Perl : embedding java script with cgi perl script

Hi All, I am aware that html tags can be embedded in cgi script as below.. In the same way is it possible to embed the below javascript in perl cgi script ?? print("<form action="action.htm" method="post" onSubmit="return submitForm(this.Submitbutton)">"); print("<input type = "text"... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

calling a perl script with arguments from a parent perl script

I am trying to run a perl script which needs input arguments from a parent perl script, but doesn't seem to work. Appreciate your help in this regard. From parent.pl $input1=123; $input2=abc; I tried calling it with system("/usr/bin/perl child.pl $input1 $input2"); and `perl... (1 Reply)
Discussion started by: grajp002
1 Replies

6. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question