- Feature Articles
- CodeSOD
- Error'd
- 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
I beg to differ; I believe that this is the Java API way...
Admin
So you're like most people here, eh?
Admin
Not to be completely pedantic here but...
There are calendars that have a variable number on months. I could be wrong but I believe that the Jewish calendar in particular adds an extra month from time to time to correct for the accumulation of leap days. There are probably others as well.
Relevant fact? Not really but I thought I'd throw it out there anyway.
Admin
If your going to use an example do some homework... there's no such thing as a "Jewish Calandar" - you'll find its called a Hebrew Calandar.
I bet you think all Jews originate from Israel dont you.
Admin
I take offense to that!
-Anonymous clownee #57
Admin
<FONT style="BACKGROUND-COLOR: #efefef"><FONT color=#808080 size=2>
int days(int m, int y) // m = 1..12
{
return</FONT><FONT size=2> (30+(m&1)^!!(m&8))-(m==2)*(2-(!(y%4)&&(y%100)||!(y%400)));</FONT>
<FONT size=2>}
</FONT></FONT>Admin
Has no one pointed out that not only is this daft but also wrong. Shoulden't the last line be:
<FONT color=#0000ff>return</FONT> --$daysInMonth;
<FONT color=#000000>Because as I see it the function will return 32 for months with 31 days, 31 for 30 month days and 30/29 for feb.</FONT>
<FONT color=#000000>Or rather: </FONT>
<FONT color=#0000ff>function</FONT> daysInMonth($thisMonth,$thisYear) {
$daysInMonth=31;
<FONT color=#0000ff>while</FONT> not (<FONT color=#0000ff>checkdate</FONT>($thisMonth,$daysInMonth,$thisYear)):
$daysInMonth--;
<FONT color=#0000ff>endwhile</FONT>;
<FONT color=#0000ff>return</FONT> $daysInMonth;
}
_Neil
Admin
Re: "If your going to use an example do some homework... there's no such thing as a "Jewish Calandar" - you'll find its called a Hebrew Calandar.
I bet you think all Jews originate from Israel dont you."
I'm guessing someone just pissed of a jew.. It's neat to be proud of your heritage, but this forum is about programming. Get over it.
Admin
(Oh, Dear....How best to respond? Let's give it a shot...)
1) Um...What?
2) Yes, I do use the words Jewish and Hebrew synonymously. If that gives offense, I meant no slight.
3) How does my knowledge of a calendar imply my thoughts on immigration? How does my knowledge of a calendar imply my thoughts on any other topic? No, you lost me on that one.
4) Contractions require an apostrophe to indicate the ellision. Interrogatives require a question mark. If you can't sound smart, you should at least look smart.
5) Born and raised in New York, moved to LA, and married my wife in Beverly Hills. I've been to temple, played dreidel and exchanged gifts at Hannakah (even got the blue wrapping paper with the Mogen Davids on it which I thought was a nice touch). I've met somes jews I liked; met some others I didn't. You, not so much.
6) Don't presume to tell me what I think. It insults me and belittles you.
7) If you have something worthwhile to say you ought to be man enough to put your name to it. Grow a pair.
8) Shalom, ya putz!
Admin
The multiplicity of your anonimosties has been dull-y noted.
No beer for you!
Admin
Could people (esp. anonymous ones) please READ the posts before stating the same thing over again. I figured out the 'returns one day too much error' when reading the 4th post, then went through and saw that James (I think) posted that particular flaw. I didn'y feel the need to post it again, and again, and again.
<FONT style="BACKGROUND-COLOR: #efefef">Please read the posts before you state something that has been said before without making any extra contribution with your statement.</FONT>
<FONT style="BACKGROUND-COLOR: #efefef">Drak (annoyednymous)</FONT>
Admin
Why are you correcting a Java example with C#? How is that any easier?
But I suppose you think it's probably easier to scrap my entire code line and switch to Microsoft's flavor of the month, eh?
Admin
Wow, this thread has gotten testy... between the proud jews and the proud java programmers.......
Who cares if corrections or changes are in a different language than the OP? The point is the logic in the procedure, not the syntatic idioms of ANY particular language. Even VB. :)
Admin
"idioms" should be "idiosyncracies".
Some day, perhaps we can edit our posts. :)
Admin
I managed to make the constant smaller. Here's my version...
#!/usr/bin/perl -w
my @list = (28, 31, 30, 31, 30, 31, 31, 30, 31, 30);
my $bits = 0;
foreach (reverse @list) {
$bits = ($bits << 2) + 31 - $;
}
print "c = $bits;\n";
foreach my $year (2003, 2004) {
foreach my $month (1 .. 12) {
printf("%d-%02d: %s\n", $year, $month, get_month_length($year, $month));
}
}
sub get_month_length {
my ($year, $month) = @;
my $leap = ($year % 4 == 0) && !($year % 100 == 0) || ($year % 400 == 0);
return 31 - (($bits - $leap << 4 >> ($month << 1)) & 3);
}
Since I encode the 31 - x difference that becomes 0 for January (and December) I can drop 2 bits on the start and end.
The constant thus becomes 278803. On leap years, the constant is 278802, which is pretty.
Perl has no real booleans, values are either "" or "1". I abuse the fact by substracting $bits - $leap which just works.
Other than that it's probably self-explanatory apart from being really pretty.
--
Alan, the mad perl hacker
Admin
First time I'm visiting, but I just had to comment on this discussions: Most of you are missing one fundamental problem: What if this function needs to be able to handle historical dates? Without context you don't know, and IF it does, most of your solutions will FAIL MISERABLY.
Most of the world converted to the Gregorian calendar between the 1500's and the mid 1900's. Most countries thus had one points in that timeperiod where they had shorter or longer months that you think.
If you go further back the question is whether or not you'll use the Julian or Gregorian calendar for the older dates. In the latter case it'll be far more complicated.
So sure, the function was crappy, but almost all of you fell in the trap of making assumptions about the requirements for the code that may not be sufficient...
Vidar
Admin
While you are correct regarding historical dates, I would bet that about 0.5% of all cases that use date functions are attempting to figure historical dates. Anyone coding such routines would(should, at least!) be aware of the issue and code accordingly.
Also, I do agree with you in regards to the various calendars in current use.
Admin
"I wonder what the functional specifications discussion was like for debating whether or not to include date_sunrise() [image] and date_sunset() [image]."
Actually there is a good reason behind that. PHP is written by an Israeli company called Zen. In the Jewish religion (as well as other religions in the area) sunrise and sunset times are particularly critical.
I meen I think it would be obvious after you saw the function jdtojewish() (FYI that stands for Julian Date to Jewish) and before anyone get's all offended, there's also a JDToFrench()
Finnally the correct funtion is: cal_days_in_month()
Admin
I thought it was "Zend"
Admin
Just as long as you're always using the Gregorian calendar.
That's not as stupid as it sounds; my current project will be used in Thailand as well as the UK, and they use the Thai Buddhist calendar there. Fortunately all this means in Java is playing by the rules with no silly hacks like manipulating dates as strings or hardcoding like the above. Use the API provided and it all works fine even for calendar systems you've never heard of.
Pete
Admin
Yeesh, this forum sucks. The preview didn't show the HTML.
Admin
no becuase she is a fucking slut
Admin
If you really wanted to optimize you would start at 31 and go bacwkards
Admin
Hey wat'z up! I read your little resumae and I really want time to know you and get time to talk to the reall person behind those wounderfull words.
Admin
How about leap years? - ugh.
Code4Life