Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to subtract month without using module?

by Anonymous Monk
on Jul 25, 2005 at 05:22 UTC ( [id://477685]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I have to subtract 6 month from the today's date without using any module. If possible using core modules also accepted. please give me the idea or code.

Thanks in advance

Replies are listed 'Best First'.
Re: How to subtract month without using module?
by Zaxo (Archbishop) on Jul 25, 2005 at 05:35 UTC
    . . . without using any module.

    Who is handicapping you in this way? Do they have any stake in the result?

    You'll have to decide whether six months have 182 or 183 days (or 182.62). I'll show two ways of getting a readable time.

    my $before_time = time() - 183 * 24 * 60 * 60; print scalar localtime $before_time; use POSIX 'strftime'; print strftime "\n%D\n", localtime $before_time;
    The POSIX::strftime() method is the more flexible, so pick it if requirements tend to slip around.

    After Compline,
    Zaxo

Re: How to subtract month without using module?
by jbrugger (Parson) on Jul 25, 2005 at 05:29 UTC
    try searching, you'll find perldoc -f  localtime
    (localtime)

    So you'll probably want to do something like this:
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year += 1900; $mon++; # month = 0 based; if ($mon <= 6){ $mon += 6; $year -=1; }else { $mon -= 6; } print "the date minus 6 months = \" $mday - $mon - $year \"\n";
    But if you want it more exactly, you'll have to count each day in a mont, and do a more secure substraction.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: How to subtract month without using module?
by ikegami (Patriarch) on Jul 25, 2005 at 06:44 UTC
    Are you allowed using the modules that come with Perl? If so, the safest way is:
    use Time::Local qw( timelocal_nocheck ); my ($day, $month, $year); my $now = time; ($day, $month, $year) = (localtime($now))[3, 4, 5]; $month -= 6; my $new_time = timelocal_nocheck(0, 0, 0, ,$day, $month, $year); ($day, $month, $year) = (localtime($new_time))[3, 4, 5]; printf("%04d-%02d-%02d\n", $year, $month, $day);
Re: How to subtract month without using module?
by sk (Curate) on Jul 25, 2005 at 05:53 UTC
    This is quick and dirty

    If possible use modules for tricky issues like date/time. You need to worry about daylight saving etc.

    #!/usr/bin/perl my $sixmthb4= time() - ( 24 * 60 * 60 * 30.5 * 6); # assuming 30.5 da +ys on avg. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($six +mthb4); print ("Month = ", $mon +1, " Day = $mday, Year = ",$year+1900,"\n");
Re: How to subtract month without using module?
by radiantmatrix (Parson) on Jul 25, 2005 at 14:00 UTC

    Well, you could copy the relevant code from Date::Calc (here) and paste it into your application.

    However, I am curious as to what the reason is for not using any modules? If it is due to distribution concerns (i.e. desire to distribute one file), you should look into PAR, which may solve that issue.

    If it is a question of not requiring someone to download and install CPAN modules, you could always include the Date::Calc module in your code distribution.

    Date math is notoriously tricky and caveat-ridden; why do you need to reinvent the wheel?

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: How to subtract month without using module?
by Molt (Chaplain) on Jul 25, 2005 at 09:05 UTC

    I guess this isn't what you want to hear, but the best way to do this without installing a module is to use a module.

    There exists a nice date module Date::Manip which will do almost anything with dates, and which (thanks to it being entirely Perl) you can simple copy the module's .pm files into the place you're installing your own code and it.

    In general date usage there are lighter and quicker modules that'll get the job done just as well, but for use when you can't install anything this module is the one to go for.

    Update: Slight clarification as to how to use it without needing to install it.

Re: How to subtract month without using module?
by Anonymous Monk on Jul 25, 2005 at 14:51 UTC
    Tricky question! What is 6 months before todays date (25 July)? Is it 25 January? If it is, what's 6 months before August 28? 6 months before August 30? Or do you want 182 days and 12 hours before the current time? And 183 days in a leap year? Or at least, between March 1 of a leap year until February 28 in the year after?

    Questions, questions, questions.

Re: How to subtract month without using module?
by tulsyan (Beadle) on Jul 25, 2005 at 06:15 UTC
    my ($month, $year) = (localtime())[4]; print $month;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://477685]
Approved by jbrugger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-29 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found