PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Sep 15, 2019 3:41 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Fri Oct 15, 2010 2:43 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
The code snippets section doesn't let you post and I forget/can't find the proper process for submitting, so I'm posting it here. Feel free to critique it if you want... It will accept years or ranges of years that are BADLY formatted and extract properly formatted information.

Usage:

Syntax: [ Download ] [ Hide ]
// a "happy" input
$range = new Ne8Vehicle_Year_Range('2000-01');
$range->isValid(); // true
$range->start(); // 2000
$range->end(); // 2001

// a "happy input"
$range = new Ne8Vehicle_Year_Range('2000-foo');
$range->isValid(); // true
$range->startYear()->isValid(); // true
$range->endYear()->isValid(); // false
$range->start(); // 2000
$range->end(); // 2000

// a "happy input"
$range = new Ne8Vehicle_Year_Range('2000');
$range->isValid(); // true
$range->start(); // 2000
$range->end(); // 2000

// a "happy input"
$range = new Ne8Vehicle_Year_Range('00');
$range->isValid(); // true
$range->start(); // 2000
$range->end(); // 2000

// a "sad input"
$range = new Ne8Vehicle_Year_Range('bar-foo');
$range->isValid(); // false
$range->start(); // Exception

// a "sad input"
$range = new Ne8Vehicle_Year_Range('23492934');
$range->isValid(); // false
$range->start(); // Exception

// a "sad input"
$range = new Ne8Vehicle_Year_Range('444');
$range->isValid(); // false
$range->start(); // Exception
 


There is also Ne8Vehicle_Year for non ranges. The Range class depends on the Year class for some parsing & validation of the years that are in the range. Future ideas would be converting 4 digit years to 2 digit years, and accepting more complex enumerations like "2000,2001,2002,2005-2008"

Unit tests & source code are attached.

PS > It simply compare a 2 digit year to a constant, 25. If it is greater, it assumes 20th century, otherwise 21st century. This is documented in the unit tests. Maybe another idea to expand this would be different heuristics for guessing the correct century.

Update - Century threshold is now adjustable:

Syntax: [ Download ] [ Hide ]
$year->setThreshold(5);
$range->setThreshold(5);
 


Update 2 - Can now disable Y2k Mode:
Syntax: [ Download ] [ Hide ]
$year->setY2kMode(false);
 


Attachments:
y2k-v5.zip [4.16 KiB]
Downloaded 310 times


Last edited by josh on Fri Oct 22, 2010 5:24 pm, edited 8 times in total.
Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 2:58 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I have found that it is handy to be able to set the "constant, 25". For example, for reports the dates are only in the past, so any two-digit date greater than 10 (this year) would be assumed to be in the 1900s. For other things you may want it to be a only a few years in the future -- give the possible dates.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 3:25 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
I use the same approach.

Don't you have start-end year pairs like this:
2001-

I.e. from year 2001 till "now".

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 3:32 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
You should check whether endYear is greater then or equal to startYear :)

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 3:35 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
I haven't had anyone use the 'now' notation. The question would be what is 'now'? The time of the import? The time the file was authored? I do switch the start & end years automatically in another layer, this class was just for parsing the strings. I'll begin to push back some of that code to these classes & incorporate your feedback.


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 3:40 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 3:52 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Funny... but really? So how would that work? ummm The mostly you can safely assume as far as I see, is that they want to add the part of it that is a valid year, and thats it. In what situation would a user mean otherwise?


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 4:09 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria
What would you put as end year (i.e. the last year of producing) of a vehicle that is still produced at this moment? Simply, a valid endYear doesn't exist yet :)

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 4:45 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Hmm but if the file was created 3yrs ago how does the author know a part fits on a vehicle that didn't exist at the time? (PS> Background info me and Vlad are both working on some automotive stuff and have been comparing notes as we go along). I do have the ability for them to put {{all}} in the year field, but I don't understand what kind of part can be known to "fit all years except ones before this year". I understand it fitting all years forwards & backwards, but I still don't understand how something can be known to fit vehicles into the future when it didnt fit some of them in the past.

I think some of this stuff is outside the scope of this little Y2k framework though, but feel free to continue "off topic", I do not mind (I still consider it on topic).


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 5:22 pm 
Offline
DevNet Master
User avatar

Joined: Wed Jun 27, 2007 9:44 am
Posts: 4313
Location: Sofia, Bulgaria

_________________
There are 10 types of people in this world, those who understand binary and those who don't


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 5:58 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Unless the person doing the updates assumes its safe to edit last year's file and doesn't know about this delima


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 6:02 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
You can't know the future ... obviously. My point was just to have the separation year (0-99) be definable depending on the needs of the app. An example is -- reports are always on past dates, whereas credit card expiration dates can go out 10+ years. Difference situations, different needs. Your default was +25 which is reasonable, but for historic data +1-5 would probably be better. That adds 20% to the range of dates handled.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 6:15 pm 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Yep, there was no question as to your suggestion. I was mainly discussing with Vlad. I agree with you 100%.

Another idea is of you had 02-45 it probably means 1902-1945, rather than 2002-2045, or 1902-2045, and they definitely don't mean 2002-1945 which is what the script would do right now. but I'll have to think about this one more. The fact they used 2 digit notations at all in the first place also further suggests is further back in time, before 4 digits became commonplace.


Top
 Profile  
 
PostPosted: Fri Oct 15, 2010 9:05 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sat Oct 16, 2010 8:21 pm 
Offline
DevNet Master
User avatar

Joined: Thu Mar 15, 2007 6:28 pm
Posts: 2765
Location: Redding, California
Very interesting little project. On application I can think of would be parsing user input of this type. I definitely agree that the developer should be able to change the century threshold. This object is also good at simply containing this data. Would this be an implementation of the Value Object pattern? Sorry if this seems obvious :roll: ; Christopher's been teaching me about this.

One question. Why do you break up the range every time the validity is checked, instead of just once in the constructor? Also, why do you check the validity every time the range is accessed, instead of just setting a boolean variable?

Edit: oops, that's two questions :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group