Perl: Finding next day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Finding next day
# 1  
Old 06-10-2010
Perl: Finding next day

Hi All,

I have date format like 09/10/2005(MM/DD/YYYY) format.
Using this as input i want to get the next day. The ouput should be in MM/DD/YYYY).
Please help me how to do it using perl.

Example:: Input 09/10/2005
Output 09/11/2005

Thanks in advance!!

Rgds,
Giri.
# 2  
Old 06-10-2010
Here you go
Code:
$ cat time.pl
#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;
use POSIX qw/strftime/;

my ($tstr,$time,@tval);

$tstr = "09/10/2005";

@tval = ( $tstr =~ m|(\d{2})/(\d{2})/(\d{4})| );

$time = timelocal( 0, 0, 0, $tval[1], $tval[0] - 1, $tval[2] );
print strftime( "%m/%d/%Y", localtime( $time + 24 * 3600 ) ), "\n";



$tstr = "02/28/2004";

@tval = ( $tstr =~ m|(\d{2})/(\d{2})/(\d{4})| );

$time = timelocal( 0, 0, 0, $tval[1], $tval[0] - 1, $tval[2] );
print strftime( "%m/%d/%Y", localtime( $time + 24 * 3600 ) ), "\n";
$ perl time.pl
09/11/2005
02/29/2004

# 3  
Old 06-10-2010
Quote:
Originally Posted by girish.raos
...
Example:: Input 09/10/2005
Output 09/11/2005
...
There's more than one way to do it in Perl !

Code:
$
$ echo "09/10/2005" | perl -M"Date::Calc" -lne '@x=split(/\//,$_);
  @y=Date::Calc::Add_Delta_YMD($x[2],$x[0],$x[1],0,0,1); printf("%02d/%02d/%4d\n", $y[1],$y[2],$y[0])'
09/11/2005
$
$
$ echo "02/28/2008" | perl -M"Date::Calc" -lne '@x=split(/\//,$_);
  @y=Date::Calc::Add_Delta_YMD($x[2],$x[0],$x[1],0,0,1); printf("%02d/%02d/%4d\n", $y[1],$y[2],$y[0])'
02/29/2008
$
$ echo "12/31/2009" | perl -M"Date::Calc" -lne '@x=split(/\//,$_);
  @y=Date::Calc::Add_Delta_YMD($x[2],$x[0],$x[1],0,0,1); printf("%02d/%02d/%4d\n", $y[1],$y[2],$y[0])'
01/01/2010
$
$ echo "02/28/1900" | perl -M"Date::Calc" -lne '@x=split(/\//,$_);
  @y=Date::Calc::Add_Delta_YMD($x[2],$x[0],$x[1],0,0,1); printf("%02d/%02d/%4d\n", $y[1],$y[2],$y[0])'
03/01/1900
$
$

tyler_durden
# 4  
Old 06-12-2010
@pludi
@tyler

Guys,
Maybe I am very wet behind the ears. However I am not sure why someone would want to obtain a different date then current. Can you please elaborate on this post? I am a student to all of this.



Warm Regards,

jaysunn
# 5  
Old 06-12-2010
@tyler_durden: true, but unlike Time::Local, Date::Calc isn't part of the core distribution, which I aimed for. I don't think that for a task this simple it should be necessary to install another dependency.

@jaysunn: why not. Say you're getting the date as a parameter from a web page, where the designer stupidly decided to use the american date format, and you have to get the previous days records.
# 6  
Old 06-13-2010
@pludi,Thanks for the insight on my question.


Please understand that I am learning, However I am still confused in regards to why someone would fake a date entry.




Sorry for my lack of understanding. However can you please provide me with another real world example?


Thank you for your mentoring.




Jaysunn
# 7  
Old 06-13-2010
Quote:
Originally Posted by jaysunn
@pludi,Thanks for the insight on my question.


Please understand that I am learning, However I am still confused in regards to why someone would fake a date entry.




Sorry for my lack of understanding. However can you please provide me with another real world example?


Thank you for your mentoring.




Jaysunn
Here are some examples:
  1. You keep data in files that are written each day and named that day. You want to access a file that is -1 day or some other number of days from today...
  2. Your script is programmed to make a decision based on a future date. You want to access characteristics about a date +1, +2, +n days in the future.
  3. You are writting a hotel or dining reservation system on the web, and you need to access a future date...
  4. Many many more examples...
This User Gave Thanks to drewk For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl script with lock to execute only once in a day

Hi, I am new to perl and have a script to which i want to ensure that no matter how many ever times i execute the script it should execute only once per day. Cronjob is not a safe method as I want to built in capability inside the script. (1 Reply)
Discussion started by: ctrld
1 Replies

2. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

3. Shell Programming and Scripting

Finding Day of the week from date

I have a problem of Finding Day of the week from date, but i need to do it within awk On SOLARIS Input:20101007(YYYYMMDD) Output:Thursday kindly provide suggestions. Thanks in advance (8 Replies)
Discussion started by: junaid.nehvi
8 Replies

4. Shell Programming and Scripting

Finding perl files without documentation

I have an application consisting of a number of perl files. I want to find those perl files that have no documentation yet, so I tried the following from the root level of the directory where the application resides: perldoc -r * The output is something like the following: No documentation found... (2 Replies)
Discussion started by: figaro
2 Replies

5. Shell Programming and Scripting

Deleting / finding files older than X days missess a day

Hi When trying to find and delete files which are, say, 1 day, the find command misses a day. Please refer the following example. xxxd$ find . -type f -ctime +1 -exec ls -ltr {} \; total 64 -rw-rw-r-- 1 oracle xxxd 81 Apr 30 11:25 ./ful_cfg_tmp_20080429_7.dat -rw-rw-r-- 1... (4 Replies)
Discussion started by: guruparan18
4 Replies

6. UNIX for Dummies Questions & Answers

How to find Day of the Week from the given date (Perl)?

How to find the Day of the Week of the given Date using perl? If I have a date in YYY--MM-DD format, how to find the DOW? Based on that, I need to find the following sunday. Pls help. (5 Replies)
Discussion started by: deepakwins
5 Replies

7. Shell Programming and Scripting

Previous day's date in Perl?

Hi All, I want to find the previous day's date and store that in a variable, which will be usuful for further processing. Any help please. Regards, raju (4 Replies)
Discussion started by: rajus19
4 Replies

8. Shell Programming and Scripting

Finding out the first business Day of the month

Hi , I want to find out the first business day of the month using korn shell programming.... ie if March 1 is saturday , March 2 is sunday and 3 is monday My code should identify the First business day..as March 3.. hope u got it.. suggestions ??? Pls look into this asap...... (1 Reply)
Discussion started by: phani
1 Replies

9. Shell Programming and Scripting

How to find 1 day old file on Windows for Active Perl

How to find 1 day old file on Windows for Active Perl? Is there anyone know the method? If UNIX or LINUX, we can use the "find ...". How to we do for Windows? (4 Replies)
Discussion started by: lcfoo
4 Replies

10. Shell Programming and Scripting

finding duplicates with perl

I have a huge file (over 30mb) that I am processing through with perl. I am pulling out a list of filenames and placing it in an array called @reports. I am fine up till here. What I then want to do is go through the array and find any duplicates. If there is a duplicate, output it to the screen.... (3 Replies)
Discussion started by: dangral
3 Replies
Login or Register to Ask a Question