How to Find number of days in a month in mmddyyyy format?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Find number of days in a month in mmddyyyy format?
# 8  
Old 11-25-2011
Thanks Tyler i did check with OS, blogged online and found that we have perl yeah!!!! now few things pending are
could you please share how do capture the var1 and var2 values?
i have to take them via
var1 and var2 values will be captured on diffrent dates and the diffrence would be followed.

n do share the script (how does it work in details if possible)...thank you so much
Code:
var1=`date '+%m/%d/%Y'`
var2=`date '+%m/%d/%Y'`
perl -le 'BEGIN {use POSIX; use Time::Local; $fixed=0}
            $begin = "var1";
            $end   = "var2";
            @x = unpack "A2 A2 A4", $begin;
            $start = timelocal 0,0,0, $x[1], $x[0]-1, $x[2]-1900;
            @y = unpack "A2 A2 A4", $end;
            $end = timelocal 0,0,0, $y[1], $y[0]-1, $y[2]-1900;
            do {
              print strftime "%m%d%Y", localtime $start;
              $olddst = (localtime $start)[8];
              $start += 24*60*60;
              $newdst = (localtime $start)[8];
              if ($newdst == 0 and $olddst != $newdst and !$fixed) {
                $start += 1*60*60; $fixed = 1
              } elsif ($newdst == 1 and $olddst != $newdst and !$fixed) {
                $start -= 1*60*60; $fixed = 1
              }
            } while $start <= $end'


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 11-28-2011 at 03:28 PM..
# 9  
Old 11-26-2011
Quote:
Originally Posted by Gaurav198
...how do capture the var1 and var2 values?
...
While there are a couple of ways to pass the shell variables to a Perl one-liner, you may be better off with a proper Perl program. You pass parameters just like you would to a shell script.

Code:
$
$
$ cat -n print_timerange.pl
     1  #!/usr/bin/perl -w
     2  use POSIX;
     3  use Time::Local;
     4  use strict;
     5  my $olddst;
     6  my $newdst;
     7  my $fixed = 0;
     8
     9  # Accept "from" and "to" dates in "MMDDYYYY" format
    10  my $from_date = $ARGV[0];
    11  my $to_date   = $ARGV[1];
    12
    13  # Determine seconds from epoch and set "begin" and "end" values
    14  my @x = unpack( "A2 A2 A4", $from_date );
    15  my $begin = timelocal( 0, 0, 0, $x[1], $x[0] - 1, $x[2] - 1900 );
    16  my @y = unpack( "A2 A2 A4", $to_date );
    17  my $end = timelocal( 0, 0, 0, $y[1], $y[0] - 1, $y[2] - 1900 );
    18
    19  # Loop from "begin" to "end", adding a day's worth of
    20  # seconds in each iteration
    21  while ( $begin <= $end ) {
    22    printf( "%s\n", strftime( "%m%d%Y", localtime $begin ) );
    23    $olddst = ( localtime $begin )[8];
    24    $begin += 24 * 60 * 60;
    25    $newdst = ( localtime $begin )[8];
    26
    27    # Handle jumps due to Daylight Savings. DST start is not really
    28    # an issue here, as we only consider M/D/Y, but I've handled it
    29    # for completeness.
    30    if ( $newdst == 0 and $olddst != $newdst and !$fixed ) {
    31        $begin += 1 * 60 * 60;
    32        $fixed = 1;
    33    }
    34    elsif ( $newdst == 1 and $olddst != $newdst and !$fixed ) {
    35        $begin -= 1 * 60 * 60;
    36        $fixed = 1;
    37    }
    38  }
$
$ # Test for month of November, 2011
$
$ perl print_timerange.pl 11012011 11302011
11012011
11022011
11032011
11042011
11052011
11062011
11072011
11082011
11092011
11102011
11112011
11122011
11132011
11142011
11152011
11162011
11172011
11182011
11192011
11202011
11212011
11222011
11232011
11242011
11252011
11262011
11272011
11282011
11292011
11302011
$
$ # Test for leap year
$
$ perl print_timerange.pl 02202008 03042008
02202008
02212008
02222008
02232008
02242008
02252008
02262008
02272008
02282008
02292008
03012008
03022008
03032008
03042008
$
$ # Test for non-leap year
$
$ perl print_timerange.pl 02202009 03042009
02202009
02212009
02222009
02232009
02242009
02252009
02262009
02272009
02282009
03012009
03022009
03032009
03042009
$
$ # Test for month and year change
$
$ perl print_timerange.pl 12202011 01042012
12202011
12212011
12222011
12232011
12242011
12252011
12262011
12272011
12282011
12292011
12302011
12312011
01012012
01022012
01032012
01042012
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 10  
Old 11-30-2011
Thank You Tyler!!! really appreciate your help on this...

I did worked onto shell and found a working solution as well !! CHEERS!!!

Code:
#! /usr/bin/ksh
M=`date +%m`
Y=`date +%Y`
#              ja fe ma ap ma ju ju ag se oc no de
set -A lasts 0 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 dmonth dday
year=2011
while ((year<2015)) ; do
        leap=0
        if ((!(year%100))); then
                ((!(year%400))) && leap=1
        else
                ((!(year%4))) && leap=1
        fi
        lasts[2]=28
        ((leap)) && lasts[2]=29
        month=1
        while ((month<13)); do
                day=1
                while((day<(lasts[month])+1)) ; do
                        dday=$day
                        dmonth=$month
                        echo ${dmonth}${dday}${year}
                       ((day=day+1))
                done
                 ((month=month+1))
        done
        ((year=year+1))
done >date.txt
more date.txt | grep ^$M | grep $Y

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of days in current month

I have a homework assignment: ---------------------------------------- "Display" the number of days in the current month. For example: September 1996 has 30 days ---------------------------------------- I am trying to just display the head of cal to start the sentence. eg. cal | head ... (1 Reply)
Discussion started by: eaafuddy
1 Replies

2. Shell Programming and Scripting

find file older than one month not by x days olds

Hi, I would like to ask about some question on the -mtime option in the find command. I want to move a log files older than one month and planning to used the find -mtime +30 but i have some clarrification does -mtime +30 or -30 refer to x days beyond or between so how about the month. suppose... (2 Replies)
Discussion started by: jao_madn
2 Replies

3. Shell Programming and Scripting

display number of days in current month

hi all searched google and here, cant find and am begining to suspect there is no options for this. shell = born with either the date or cal command I need to display the number of days in current month. can anyone point me in the right direction? (10 Replies)
Discussion started by: rontopia
10 Replies

4. Programming

Number of days in month from certain parameters, c programming request.

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (2 Replies)
Discussion started by: modn3
2 Replies

5. Shell Programming and Scripting

Number of days in month from certain parameters

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (5 Replies)
Discussion started by: modn3
5 Replies

6. Shell Programming and Scripting

Number of days in the previous month

Hi all. I am scripting in a POSIX shell on HPUX. I am running a script that needs to determine the number of days in a month. I found this on the forum and it works great: X=`cal $(date +%m) $(date +%Y) | grep -v '' | wc -w` The issue is that I am running the script on the 7th day of... (11 Replies)
Discussion started by: lyoncc
11 Replies

7. Shell Programming and Scripting

Assigning number of days in the month to a variable

I am writing a script that requires the number of days in any given month. In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc... (3 Replies)
Discussion started by: skaptakalian
3 Replies

8. Shell Programming and Scripting

calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result (8 Replies)
Discussion started by: dunryc
8 Replies

9. Shell Programming and Scripting

Day of month and year to mmddyyyy

I have a date that looks like this: 2008/100:18:40:47.040 I need it to look like this: 2008 04 09 18 40 47 040 I have looked at datecalc and it doesn't seem like it takes the day of year for this year (or whatever year is input) and converts it into month and day. It also has to account... (2 Replies)
Discussion started by: ajgwin
2 Replies

10. Shell Programming and Scripting

Calc number of days in a month

Looking for some help on capturing the number of days in a month to set as a loop counter. Any ideas, please let me know. (3 Replies)
Discussion started by: flounder
3 Replies
Login or Register to Ask a Question