Solved

Days

Posted on 1998-12-07
3
182 Views
Last Modified: 2010-05-18
I want to make a program to count a total days, examples i saved 01-31-98 (mm/dd/yy) then if today is 01-01-99 (mm/dd/yy) the program count a total days from 01-31-98. so the output from the program will be 1 days.
others examples, i save 02-01-99 then i input 03-01-99 then the outpun must be 28 days.. the output must be in days!!
Thanks
0
Comment
Question by:ichen
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
flivauda earned 100 total points
ID: 1206783
#!/usr/local/bin/perl

$days = &days_since_1990(1998, 3, 1) -  &days_since_1990(1998, 2, 1);
print "Days: $days\n";


sub leap_year
{
        local($year); $year = shift;
        local($x);
        $x = ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 ==0));
        return $x;
}

sub days_in_year
{
        local($year); $year = shift;
        return (&leap_year($year) ? 366 : 365);
}

sub days_in_month
{
        local($year); $year = shift;
        local($month); $month = shift;
        @days_per_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

        if (($month<1) || ($month>12))
        {

                $days=0;
        }
        else
        {
                $days = $days_per_month[$month - 1];
                if (&leap_year($year) && ($month == 2))
                {
                        $days++;
                }
        }
        return $days;
}

sub day_of_year
{
        $year = shift;
        $month = shift;
        $day = shift;
        local($x);
        local($day_count);

        $day_count=0;
        for ($x=1;$x<$month;$x++)
        {
                $day_count += &days_in_month($year, $x);
        }
        return ($day_count + $day);
}

sub days_since_1990
{
        local($year); $year = shift;
        local($month); $month = shift;
        local($day); $day = shift;
        local($x);
        local($day_count);

        $day_count=0;

        if ($year<1990)
        {
                return 0;
        }

        for ($x=1990;$x<$year;$x++)
        {
                $day_count += &days_in_year($x);
        }
        $day_count += &day_of_year($year, $month, $day) - 1;

        return $day_count;
}


0
 
LVL 84

Expert Comment

by:ozo
ID: 1206784
sub days_since_1990 { use Time::Local; (timegm(0,0,0,$_[2],$_[1]-1,$_[0]-1900)-timegm(0,0,0,1,1-1,1990-1900)) / (60*60*24) }
0
 
LVL 1

Expert Comment

by:flivauda
ID: 1206785
I just ported over my old C code, your is a little shorter =)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
execute pl/sql block from perl program 3 119
perl CPAN issue 3 99
rename outfile before writing 2 71
create a gui in perl 3 72
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now