- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Stop Poking Me!
- Operation Erred Successfully
- A Dark Turn
- Nothing Doing
- Home By Another Way
- Coast Star
- Forsooth
- Epic
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Frist!
<!-- Posted by SockBot 0.12.5 "Guileful Ginny" on Thu Oct 23 2014 01:37:33 GMT-0400 (EDT)-->Admin
Admin
Looks like today is one of those obligatory PHP-bashing days. Paging @Arantor
Admin
TRWTF: storing a date as string.
Bonus WTF in the replacement code: it uses strtotime. Description of that function:
If you wonder what "an English date format" means, there's this note:
So anybody can insert whatever date format they want inside the database and it might even work?!?
Admin
Function is
_isSmsCodeExpired()
That's TRWTF: The function now returns a Discobool (true when not expired, false when expired).
Admin
yup strtotime IS the real wtf, not a very good function to trust when working in a multi locale environment. Always enforce ISO 8106, or just plain Y-m-d H:i:s
Admin
I love seeing my anonymizer in action.....
I wonder who wanted to post frist but didn't want to do it publically? (resisting urge to log in as sockbot and find out, successfully so far)
Admin
No anyone can insert a unix date into the database, the application will take a date string and may or may not generate the result the user was expecting. This nasty UI problem though and always has been. There isn't really a "good" way to differentiate types of date entries other than to validate and reject dates like 30/05/14, but 10/11/14 how can you know? if you were expecting US format. You can try telling the user what to do but they won't read it. You can split the fields out but people don't like entering them separately, you can make them confirm the date after entry but that irritates them, etc.
Admin
Didn't realize the new format changed the tags... silly remmy comments.
Admin
TRWTF is storing dates/times in string format here. Like JBert said. No developer with a few months (or the common sense to use google/stackexchange) experience does this.
Smarter solution would be to modify the system passing those strings to pass timestamps instead (Does mktime scare people?) so that you don't need to faff about with strtotime or worry about ISO/European/US formats.
Edit: Even better, I just had to deal with this on Monday porting data from a legacy system. Fortunately I'm strictly dealing with European date format (And it was server side, not user input fortunately). Solution? Nearly identical. Oh, woe is me. Bonus points for months stored in string format (OCT, DEC, etc).
Admin
That's when you use a format string / template.
The blank template would look like.
yyyy/mm/dd hh:mm:ss
Then the user starts to type
20yy/mm/dd hh:mm:ss
Put the pattern right where they type and it doesn't obstruct.
Admin
Won't somebody please think of the llamas?
Admin
After thinking about it, I don't really get the whole increment thing anyway.
So, you're going to check if the license expired... by adding 2 mins to the license-end date, and seeing if now is greater than that?
Why 2 mins? In case you started the program seconds before it expired and it takes a while to get to this point?
???
Not to mention this crap.
intervalSec = 120 {(2 minutes)}.
explode date into array.
intervalMin = intervalSec / 60 {(2)}.
intervalSec = intervalSec - (intervaleMin * 60) {(120 - 60*2, or 0)}
seconds += 0; minutes += 2; seconds += 0;
if (seconds > 60) ...
Seriously? You didn't trust seconds?
Admin
Would you rather fight 60 second sized minutes, or 1 minute sized second?
Admin
I have the pleasure of maintaining several sites where the developers thought it was a good idea to store date/time as a unix timestamp in a
VARCHAR(255)
(Its better for indexing?). Also they knew that the database couldn't be trusted to generate a unique primary key, so whenever they insert a new record, they make sure to set theid
to a call to PHP'stime()
function (which has the added benefit of NOT WORKING when two records are inserted in the same second, how joyous!)And these apps were supposed to be written by people with 10+ years experience in web development.
Admin
I think we both know they weren't
Admin
Actually, that's what they call "having 2 + 2 + 2 + 2 + 2 years experience".
Admin
Nope, this isn't PHP idiocy, this is dev stupidity for not knowing that strtotime is even a thing.
Admin
Yes, mktime (or even gmmktime) scares people. The number of people I have dealt with who don't understand WTF a timestamp is surely is TRWTF - but then again to their credit, they're not developers.
Admin
I know of one for definite that didn't as of a week ago. I.E. legacy system support I got roped into. I can't quite recall the conversation but the following happened.
And this is the education sector. Is that irony? I can never remember the correct definition. Is *that* irony?Admin
I'm assuming said developer has never heard of the concept of separation of logic and presentation.
As for it being ironic... it would only be ironic if he were educating people in IT. Right now it's merely another WTF in the room.
As for the bonus irony... not unless the place you work at was supposed to have taught you what irony was in the first place.
Admin
Was his shirt wrinkled? Collar unstarched? I fell like I'm not getting something here.
Admin
On that note, what about tinny, zincy or maybe alumin[spoiler]i][/spoiler]umy?
Admin
About that...
Admin
Yes?
Admin
That was an excellent Blakey impression, but I'm going to have to dock points for the lack of gratuituous profanity.
Admin
FUCK YOU<o
Admin
I've still got that keyboard with the razor, you--oh, wait.
Good one.
Admin
What you see:
(code that gets date from DB)
What I see:
// this needs to be completely replaced
Admin
Well I think the whole mess could just have been done in the db (as thay already query the db for the expiredate the way I see it.
So something along 'SELECT now() > date + INTERVAL '2 minutes' FROM table' (example if using postgresql) should do the trick..... That way they do not have to worry about date format or anything like that....
Yazeran
Plan: To go to Mars one day with a hammer.
Admin
If it was a timestamp in the database, sure, that would be a thing.
Would appear they were not that smart. Though I suspect for bonus points they were using a datetime column which would actually have let them do such things right at the SQL layer :laughing:
Admin
If people engineered the way they code:
On noes, the engine is still vibrating too much. Quick add more parts until it doesn't.
Admin
Oh noes, the engine is still vibrating too much, and now excess parts are flying everywhere.
Admin
Now you know which ones weren't necessary!
Admin
I know, to make sure we account for every configuration of the engine,
the engine should come with a factory to build engines, and then a factory factory, in case the specifications change so we can dynamically construct factories.
Then.... I'll never have to work again.
pffft... maintenance.
Admin
The Kerbal Space Program thread is that way :arrow_backward: :arrow_double_down: :arrow_right_hook: :arrows_counterclockwise:
Admin
Sorry to rain on your parade, but if you compare a modern engine with its double overhead camshafts, balance shafts, torsion dampers, active engine mounts, flexible exhaust downpipe, and bellows in the air intake, to a simple pushrod design with rigid exhaust and bolt-on inlet manifold, you will see that "Add more parts until it doesn't" is exactly what has happened.
Admin
I love how it handles extra seconds (well between 60 and 119) then shuffles back the minutes, hours, days, months and years if they overflow except for the last (add year) overflow that has forgotten to subtract 12 from the months. And also if the month overflows $daysInMonth will probably break. I am not au-fait with PHP but maybe someone can comment. But it looks like 2014-10-01 10:55:77 would be handled but 2014-20-01 10:55:20 would crash?
Admin
Did it hurt?
Admin
Here's the big surprise, he's actually using strtotime already.
Admin
Adding seconds without taking care of any DST? Because I didn't see any mention of using UTC in there.
Admin
Admin
Having a serious ISO fetish I was also deeply offended by the formats the function uses, but it was the only function I found being able to add time interval of P1M. It had to be expressed as
+1M
, but at least it was correctly one month.Admin
Unless you are using SQLite where it seems to be the recommended practice as that thing does not have any real types, only integer, real, string and blob (and is manifest-typed).
Admin
He educates people in IT.
Admin
I'm assuming this is MySQL, which has a datetime type - which is accessed as a string and returned as a string even if it is not really a proper string.
Admin
If it uses PDO, wouldn't you be able to call
fetchObject
or would the MySQL driver really force this to a string field?Disclaimer: I don't use PHP very often.
Admin
If you're getting a DateTime column, it's coming back as a string whatever happens.
fetchObject returns the row as an object, which is basically in this case a standard class instance with each of the columns coming back as public properties in that class. It's basically syntactic sugar.
Here's the thing... what should it come back with? PDO in particular means abstraction, but even if we were being specific to MySQL for a minute with specific bindings (e.g. MySQLi), the reality is that a datetime is going to come back as a string. Partly for historical use (since I don't remember MySQL ever returning a datetime as anything other than what amounts to a string) and partly because things like PHP's DateTime are more recent than PHP's support for MySQL.
But whatever... what would a datetime column become? Having it as a string is reasonably malleable and means you can strtotime... (oh wait haha)
Admin
MSSQL stores datetimes as a numeric representing the number of days since the epoch (01-01-1900 IRC). This allows an unambiguous way of passing them around and converting them
Admin
Ah, MySQL's datetime is not epoch related, meaning you can store pre-epoch dates and times if so inclined. It's also good until Y10k.
I'd argue that having a string delineating d/m/y h:m:s is as unambiguous as a timestamp. The only question then becomes timezones and that's up to you to figure out anwyay ;)