Comment On Maybe NOT_TODAY

Recently, time has been an issue on the minds of many people. Unfortunately, as far as I can tell, only about half of them installed the software required to deal with the new Daylight Saving schedule. This means that for about three weeks, people trying to share appointments and events will have trouble, at least sometimes. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Maybe NOT_TODAY

2007-03-28 09:12 • by T$ (unregistered)
That's not so bad actually, at least it's easily readable.

String checkMe = date.trim();


Why is this here, didn't it already say must be in the format YYYY?MM?DD

Re: Maybe NOT_TODAY

2007-03-28 09:34 • by Marcin (unregistered)
The real WTF is that they used fixed-length ints, which will cause this code to become a liability in only a few hundred thousand years.

Re: Maybe NOT_TODAY

2007-03-28 09:44 • by JamesKilton
So obviously booleans are extremely inefficient constructs, and code is infinitely more readable when given:

if (isCurrentDay("2007-20-03").equals("TODAy")) {
...
}

Oh wait...

Re: Maybe NOT_TODAY

2007-03-28 09:50 • by Zonkers
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;

Re: Maybe NOT_TODAY

2007-03-28 09:53 • by H|B (unregistered)
129048 in reply to 129047
0-based month codes, perhaps?

Re: Maybe NOT_TODAY

2007-03-28 09:54 • by htg (unregistered)
I'm glad they trust the source of the date string so much, otherwise you've got one dead application because of the NumberFormatException.

Surely you'd convert the String to a Date via a SimpleDateFormat parse() method and pass that into the method, and then you can check it.

public boolean isToday(Date toCheck)
{
Calendar c1 = new GregorianCalendar();
c1.setTime(toCheck);
Calendar c2 = new GregorianCalendar();
return ((c1.YEAR == c2.YEAR) && (c1.MONTH == c2.MONTH) && (c1.DAY_OF_MONTH == c2.DAY_OF_MONTH));
}

Re: Maybe NOT_TODAY

2007-03-28 09:57 • by T$ (unregistered)
129050 in reply to 129047
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;


I'm gonna take a wild shot in the dark and say that GregorianCalendar.MONTH is zero-based, so probably if the month is January, you get 0, February is 1, etc.

CAPTCHA is scooter, segue anyone?

Re: Maybe NOT_TODAY

2007-03-28 10:00 • by sionide21 (unregistered)
129051 in reply to 129047
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;


The month function will return January as 0. Most people would rather call it 1.

Re: Maybe NOT_TODAY

2007-03-28 10:07 • by Mikolaj (unregistered)
At least it's better than FILE_NOT_FOUND

Re: Maybe NOT_TODAY

2007-03-28 10:11 • by Look at me! I'm on the internets! (unregistered)
129055 in reply to 129047
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;


In Java, January is 0.

Re: Maybe NOT_TODAY

2007-03-28 10:20 • by BlueMoon (unregistered)
Hmm there is no checking if the input string has the correct length (10 characters) and if the YYYY, MM and DD characters are digit characters

Using subString on a string that is to short is a ArrayIndexOutOfBoundsException

Using parseInt on a non digit is a NumberFormattingException


A well they probably do catch (Exception e) somewhere in the webserver

Blue

Re: Maybe NOT_TODAY

2007-03-28 10:25 • by Murf (unregistered)
Yay a Java SOD. It's been a while.

Yeah this is butt-ugly - not being aware of StringIndexOutOfBoundsException, NumberFormatExceptions, using a GregorianCalendar directly, blergh, you could forgive.

But result = "TODAY"?

Nice :)

Re: Maybe NOT_TODAY

2007-03-28 10:33 • by snoofle
129060 in reply to 129047
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;

In Java, GregorianCalendar.get(Calendar.MONTH) returns 0..11

Re: Maybe NOT_TODAY

2007-03-28 10:34 • by whicker (unregistered)
129061 in reply to 129060
snoofle:
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;

In Java, GregorianCalendar.get(Calendar.MONTH) returns 0..11
wtf?!

Re: Maybe NOT_TODAY

2007-03-28 10:36 • by guy (unregistered)
It is actually pretty awesome how one of the possible return values is "NOT_TODAY" instead of "NOT TODAY". It seems the writer has seen someone use a constant, but didn't quite get the point.

Re: Maybe NOT_TODAY

2007-03-28 10:42 • by Gedoon (unregistered)
As others, I also fail to see the Real WTF, this code is not that bad at all and certainly it lacks the curious perversion. So it returns a string, that's not so bad. Come on now, the one with the thousands of lines of checksums and ifs and counters was way more wtf than this! This is like wtg, as in what the gosh. Booo todays wtf! Hooray yesterdays wtf!

Re: Maybe NOT_TODAY

2007-03-28 10:47 • by Licky Lindsay
129066 in reply to 129055
Look at me! I'm on the internets!:

In Java, January is 0.


Java is a lot like Soviet Russia, isn't it?

Re: Maybe NOT_TODAY

2007-03-28 10:49 • by Crint (unregistered)
129067 in reply to 129049
htg,
checking static constants will always be true.

Re: Maybe NOT_TODAY

2007-03-28 10:49 • by slöge (unregistered)
129068 in reply to 129060
I think that's an even bigger wtf.

Re: Maybe NOT_TODAY

2007-03-28 10:50 • by KattMan
Yes this one is minor.
It should have accepted a date rather than a string and simply pull the year, month, and day out of it for comparisons then return a boolean true or false.

Wrap this in a call that accepts a string that converts it to a date and calls this and you have a very good piece of reusable code that works as intended.

So he missed a few exceptions, seeing this I think it is safe to assume they are caught somewhere else up the line as this is a deep internal function and may not need to handle it's own errors.

Re: Maybe NOT_TODAY

2007-03-28 10:51 • by Licky Lindsay
129070 in reply to 129061
whicker:
snoofle:
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;

In Java, GregorianCalendar.get(Calendar.MONTH) returns 0..11
wtf?!


It makes sense when you think about code like this:

String months[] = {"January", "February" /* etc */ };
String thisMonth = months[i];

This is however no excuse for inflicting it on an API as allegedly high-level as Calendar.

Calendar is a world of WTF.

Re: Maybe NOT_TODAY

2007-03-28 10:54 • by snoofle
129071 in reply to 129057
BlueMoon:
Hmm there is no checking if the input string has the correct length (10 characters) and if the YYYY, MM and DD characters are digit characters

Using subString on a string that is to short is a ArrayIndexOutOfBoundsException

Using parseInt on a non digit is a NumberFormattingException


A well they probably do catch (Exception e) somewhere in the webserver

Blue

Wait a minute - fair is fair: the first line does say ASSUME the format is ...

Re: Maybe NOT_TODAY

2007-03-28 10:55 • by Matt (unregistered)
129073 in reply to 129057
BlueMoon:
Hmm there is no checking if the input string has the correct length (10 characters) and if the YYYY, MM and DD characters are digit characters

Using subString on a string that is to short is a ArrayIndexOutOfBoundsException

Using parseInt on a non digit is a NumberFormattingException


A well they probably do catch (Exception e) somewhere in the webserver

Blue


We don't get to see the method signature. Perhaps this is a private method and the format is checked prior to passing the input.

Also, the exceptions could be thrown and handled at a higher level, but it's difficult to know without the signature.

Either way, not the cleanest way to do it.

Re: Maybe NOT_TODAY

2007-03-28 10:56 • by htg (unregistered)
129074 in reply to 129061
Dates are messed up in Java, it's as if the date class was coded on a Friday afternoon with no thought or peer review. Then they tried to fix it with the Calendar system to actually be useful and consistent, but everyone still uses Date objects at some point.

So instead of a Date object that defaults to UTC that handles its calendar internally, with useful methods and conversions between timezones, we get Calendar, that has fields (in capitals no less) so we can do a lookup via Calendar.get(Calendar.YEAR) rather than Calendar.getYear(), and so on. WTF. (yeah, ignore my code above, it should have been return ((c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))...). The Java compiler can turn a simple getter method into inline code, but this won't work as I presume internally it has a switch statement in the get(int type) method.

Anyway, mix on the SQL Date, DateTime, etc classes on top, and enjoy Java's time and date handling disfunctionality.

Re: Maybe NOT_TODAY

2007-03-28 10:57 • by htg (unregistered)
129075 in reply to 129067
Crint: Yeah, got a bit excited there.

Re: Maybe NOT_TODAY

2007-03-28 11:01 • by snoofle
129076 in reply to 129074
htg:
Dates are messed up in Java, it's as if the date class was coded on a Friday afternoon with no thought or peer review. Then they tried to fix it with the Calendar system to actually be useful and consistent, but everyone still uses Date objects at some point.

So instead of a Date object that defaults to UTC that handles its calendar internally, with useful methods and conversions between timezones, we get Calendar, that has fields (in capitals no less) so we can do a lookup via Calendar.get(Calendar.YEAR) rather than Calendar.getYear(), and so on. WTF. (yeah, ignore my code above, it should have been return ((c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))...). The Java compiler can turn a simple getter method into inline code, but this won't work as I presume internally it has a switch statement in the get(int type) method.

Anyway, mix on the SQL Date, DateTime, etc classes on top, and enjoy Java's time and date handling disfunctionality.

Actually, the implementation of Calendar uses an array of fields, with the constants (i.e.: Calendar.MONTH) used to index into the array.

Re: Maybe NOT_TODAY

2007-03-28 11:25 • by Dr. Jones, Sr. (unregistered)
129081 in reply to 129055
In Latin, "Jehovah" begins with an "i".

Re: Maybe NOT_TODAY

2007-03-28 11:31 • by Thorin (unregistered)
129085 in reply to 129038
T$:
That's not so bad actually, at least it's easily readable.

String checkMe = date.trim();


Why is this here, didn't it already say must be in the format YYYY?MM?DD


A user would never put in extra characters or anything.....

Re: Maybe NOT_TODAY

2007-03-28 11:32 • by wick (unregistered)
If you are dealing with Strings this would work.
getTime() guarantees you get the system date.
format() will return a String.
equals() will not fail, even if it is passed a null, just return false so that protects format errors.

But truth be told, Java Dates are one giant ball of WTF's and that is why the entire API is basically deprecated.

String ret = "NOT_TODAY";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy?MM?dd");
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
String dateStr = sdf.format(today);
if (dateStr.equals(date)){
ret = "TODAY";
}
return ret;

Re: Maybe NOT_TODAY

2007-03-28 11:45 • by Markp
Hmmm...one line solution?


return IDataUtil.getString( cursor, "date" ).replaceAll("[^0-9]", new String() ).equals( new SimpleDateFormat ("yyyyMMdd").format( new GregorianCalendar().getTime() ) );


All complete with less hazards than the previous solution.

Re: Maybe NOT_TODAY

2007-03-28 11:57 • by Licky Lindsay
129099 in reply to 129087
wick:

Calendar cal = Calendar.getInstance();
Date today = cal.getTime();


Date today = new Date();

Re: Maybe NOT_TODAY

2007-03-28 11:59 • by T$ (unregistered)
129101 in reply to 129085
Thorin:
T$:
That's not so bad actually, at least it's easily readable.

String checkMe = date.trim();


Why is this here, didn't it already say must be in the format YYYY?MM?DD


A user would never put in extra characters or anything.....


I'm assuming the fact that it said to assume the string coming in was valid and correctly formatted would presume that we don't have to test for it again. Same reason we don't have to walk through the string one character at a time to test that the YYYY MM and DD are numeric.

Re: Maybe NOT_TODAY

2007-03-28 12:11 • by rufio (unregistered)
129107 in reply to 129060
snoofle:
Zonkers:
This code is not that bad. I'm curious why one is being added to currentMonth though.
  int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;

In Java, GregorianCalendar.get(Calendar.MONTH) returns 0..11


well, it can actually return 12.
You knew that if you read some source files

Re: Maybe NOT_TODAY

2007-03-28 12:13 • by Licky Lindsay
I suspect that if Calendar were designed post-Java 1.5.0, they would have made things like JANUARY and MONDAY enums instead of ints, to make sure nobody even tries to use them for anything like this.

In Java, code refactors you.

Re: Maybe NOT_TODAY

2007-03-28 12:15 • by alejo0121 (unregistered)
The real WTF is that he is using Strings and numbers taken from the dates instead of using dates directly and using methods such as "DaysBetween" or "Equals"(Not sure about this one)...

it could be a lot more easier this way

Re: Maybe NOT_TODAY

2007-03-28 12:16 • by Licky Lindsay
129111 in reply to 129107
rufio:
well, it can actually return 12.
You knew that if you read some source files


You don't need to RTFS, just plain old fashioned RTFM. The javadocs for Calendar state that there is another month UNDECIMBER that comes after DECEMBER.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#UNDECIMBER

Re: Maybe NOT_TODAY

2007-03-28 12:30 • by Murf (unregistered)
129116 in reply to 129110
alejo0121:
The real WTF is that he is using Strings and numbers taken from the dates instead of using dates directly and using methods such as "DaysBetween" or "Equals"(Not sure about this one)...

it could be a lot more easier this way


The Real WTF (TM) is people commenting on APIs they know nothing about.

Re: Maybe NOT_TODAY

2007-03-28 12:47 • by Will (unregistered)
129122 in reply to 129110
alejo0121:
The real WTF is that he is using Strings and numbers taken from the dates instead of using dates directly and using methods such as "DaysBetween" or "Equals"(Not sure about this one)...

it could be a lot more easier this way


Remember that the Date class isn't just a date; it's a date and time, down to the millisecond. Date.equals() checks the time too, so it would not be appropriate here.

There is no DaysBetween (or daysBetween, or whatever) on the Date class.

Re: Maybe NOT_TODAY

2007-03-28 12:50 • by meissnersd (unregistered)
There is a small race condition at midnight.
If it is Tuesday with a millisecond to go to Wednesday,
it may be Wednesday by the time the caller gets the result.
Maybe thats okay. Shrug.

But...provided the parse exceptions are handled by the callers, I would not call this a WTF.

Re: Maybe NOT_TODAY

2007-03-28 12:51 • by Blame (unregistered)
It seems inefficient to extract the year, extract the month and extract the day, then get the current year, get the current month and get the current day and then compare them.

Depending on the application, it's probably better to extract the year, get the current year and compare them and return if they're different without looking at the month and day.

If it's more likely that the day will vary, then check that first.

Re: Maybe NOT_TODAY

2007-03-28 12:54 • by CynicalTyler (unregistered)
This function is incomplete: other acceptable return values are MAYBE_NOT_TOMORROW and BUT_SOME_DAY.

Re: Maybe NOT_TODAY

2007-03-28 12:58 • by Loopy (unregistered)
Shoulda used python.

/evil grin

Re: Maybe NOT_TODAY

2007-03-28 13:02 • by mbessey
129127 in reply to 129074
htg:
Dates are messed up in Java, it's as if the date class was coded on a Friday afternoon with no thought or peer review. Then they tried to fix it with the Calendar system to actually be useful and consistent, but everyone still uses Date objects at some point.


No kidding. I'd forgotten how stupid date handling in Java can be - it's been quite a while since I've written any Java code.

Re: Maybe NOT_TODAY

2007-03-28 13:24 • by Ann Coulter (unregistered)
129136 in reply to 129087
wick:
If you are dealing with Strings this would work.
getTime() guarantees you get the system date.
format() will return a String.
equals() will not fail, even if it is passed a null, just return false so that protects format errors.

But truth be told, Java Dates are one giant ball of WTF's and that is why the entire API is basically deprecated.

String ret = "NOT_TODAY";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy?MM?dd");
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
String dateStr = sdf.format(today);
if (dateStr.equals(date)){
ret = "TODAY";
}
return ret;


This code will almost always return "NOT_TODAY" unless if statement is run at midnight exactly.

I'm not too sure about using "?" as a wildcard character in the SimpleDateFormat but I have to look that one up.

Re: Maybe NOT_TODAY

2007-03-28 13:38 • by Loopy (unregistered)
129139 in reply to 129126
I THINK this will work...

def isNumber(x):

try: x+1
return x
except TypeError:
return "switch"

def isToday(checkThisDate):
myToday=today()
rawDate=checkThisDate
nowData=""
dateList=[]
for index in range(len(checkThisDate)-1, -1, -1):
if isNumber(index)=="switch":
dateList.append(nowData)
else:
nowData+=range
if dateList=today():
return "TODAY!HUZZAH!"
else
dateString="dateList(0)+"/"+dateList(1)+"/"+datelist(2)
return dateString



Re: Maybe NOT_TODAY

2007-03-28 13:41 • by Loopy (unregistered)
129140 in reply to 129139
Crap. Probably would help to turn the numbers into variables before testing to see if they're numbers, huh?

Re: Maybe NOT_TODAY

2007-03-28 13:42 • by chb

===================
//input param date must be in format YYYY?MM?DD
//where ? might be any character
//returns "TODAY" or "NOT_TODAY"

String date = IDataUtil.getString( cursor, "date" );

This IDataUtil and cursor variables worry me a bit.

reminds me of fread(FH,...)

Also:
From the java doc
GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems...

Shouldn't it be called JulianAndGregorianCalendar then?

Addendum (2007-03-28 14:41):
How about..

perl -e"print((shift=~m/^`date +%Y.%m.%d`/)?'':q/NOT_/,qq/TODAY\n/)" "2007?03?28"

*ducks*

Re: Maybe NOT_TODAY

2007-03-28 13:42 • by Loopy (unregistered)
129142 in reply to 129140
Er. I mean turn them into integers. I'll go kill myself now if you don't mind.

Re: Maybe NOT_TODAY

2007-03-28 13:43 • by Loopy (unregistered)
Er I mean integers. Gah.

Re: Maybe NOT_TODAY

2007-03-28 13:44 • by S|i(3_x (unregistered)
129145 in reply to 129065
Gedoon:
Booo todays wtf! Hooray yesterdays wtf!


Buried for alluding to Red Stripe commercials.
« PrevPage 1 | Page 2Next »

Add Comment