Perl with date math

Platypus

Lifer
Apr 26, 2001
31,046
321
136
So I have a date in this format

050727, and I want to grab everything from that date, and the date from the date before 050726 from a directory that is filled with every kind of revision of that date all the way to 01. The problem is that I cannot simply decrease the current day by 00001 because of the case of the rolling over for next month.

I know perl has a way of handling this, can anyone enlighten men?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Get the Time:: Piece module that will let you do something like this:

$date = "050727"
$date = eval {Time:: Piece->strptime($date, "%y%m%d")};

This type has comparison operators overloaded, so you can do whatever searching/sorting/etc. you need.
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
Originally posted by: Armitage
Get the Time:: Piece module that will let you do something like this:

$date = "050727"
$date = eval {Time:: Piece->strptime($date, "%y%m%d")};

This type has comparison operators overloaded, so you can do whatever searching/sorting/etc. you need.


Basically what I need to do is take today's date, and yesterday's date, and remove everything else from a directory. Trying to script it as such.

I'm kind of new to perl, can you provide any sort of other explanation or perhaps a guide on this? Do I HAVE to install a module for this.. because I'd like to avoid having to do so if possible.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Well, you can use Time::Local, which I believe is part of the standard install. It doesn't come with an strptime() function though, so you'll have to parse the date yourself. It returns a unix timestamp (ie. seconds since Jan. 1 1970), so you can do the comparisons that way.

Actually, given the format you show, you should be able to do string comparisons on that date string to get the right answer, not?
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
Originally posted by: Armitage
Well, you can use Time::Local, which I believe is part of the standard install. It doesn't come with an strptime() function though, so you'll have to parse the date yourself. It returns a unix timestamp (ie. seconds since Jan. 1 1970), so you can do the comparisons that way.

Actually, given the format you show, you should be able to do string comparisons on that date string to get the right answer, not?

Basically I have a file that looks like this

xxxx_050727_xxx.xxx

which I am using perl to strip the date out of.

I want to just whack all the files that are not that date, forget the yesterdays date for now. So anything without 050727 in the filename needs to be deleted..

Does that make sense?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: CorporateRecreation
Originally posted by: Armitage
Well, you can use Time::Local, which I believe is part of the standard install. It doesn't come with an strptime() function though, so you'll have to parse the date yourself. It returns a unix timestamp (ie. seconds since Jan. 1 1970), so you can do the comparisons that way.

Actually, given the format you show, you should be able to do string comparisons on that date string to get the right answer, not?

Basically I have a file that looks like this

xxxx_050727_xxx.xxx

which I am using perl to strip the date out of.

I want to just whack all the files that are not that date, forget the yesterdays date for now. So anything without 050727 in the filename needs to be deleted..

Does that make sense?

That's just pattern matching then - no date math involved.
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
Right..


but the reason I am doing this is to only capture today's and yesterday's date from this gathering script.. as of now it grabs everything from the folder and puts it elsewhere. I only want today's date.

I can get today as you can see below.. how do I get yesterday too?
 

skace

Lifer
Jan 23, 2001
14,488
7
81
A really good PERL writer could probably compress this for you, but I was learning and this is how I understood it. ctime, cday and cmonth are the current time date and month. cepoch is the time in some value since the dawn of computers or whatever. Using this value I could subtract 31 days back, which is what 2678400 is. Cyear gets added up by 1900 because if it is over a certain time it is just a small value, cmonth gets added by 1 because it starts at 0. Umm... The perldoc for localtime is more indepth, but this code example can be a lot easier to understand than the docs as I had some trouble understanding it myself.

Here are my notes that I took while writing this:

localtime EXPR
Converts a time as returned by the time function to a 9-element list with the time analyzed for the local time zone. Typically used as follows:
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$time = timelocal($sec,$min,$hours,$mday,$mon,$year);

2678400 is the # of seconds in 31 days. 86400 seconds in a day.
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
I think I might have something working..

First one gets today's date, second subtracts 24 hours in second format.
 

skace

Lifer
Jan 23, 2001
14,488
7
81
I tried running what you just typed out and it refused to work, but I reformed it into a .PL and it still didn't come out properly, this is what you wnat I believe.

The output will be:
today 052807
yesterday 052707
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
Originally posted by: skace
PS, copy this code into notepad and then rename it to whateveryouwant.pl

well yeah, it's not formatted obviously but it does the basic idea of what I want