• (unregistered) in reply to
    :
    Here's my way :

    (it's in Java, but its easy to code in whatever)

    The month here is zero based... That's kind of odd, but that's the Java API way...




    <font face="Lucida Console, Courier" size="2">
    /**
    * Returns the actual maximum number of days in a month (0 based).
    *
    * @param iYear the Year
    * @param izMonth the zero based month number
    * @return the maximum number of days for the month
    */
    public static int getMaximumDays(int iYear, int izMonth) {
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int i = daysInMonth[izMonth];

    if (isLeapYear(iYear) && i == 28) i++; // Feb 29

    return i;
    }

    /**
    * Returns true if the year is a leap year.
    *
    * @param year the year
    * @return true if the year is a leap year, false otherwise
    */
    public static final boolean isLeapYear(int year) {
    if ((year % 100) == 0)
    return ((year % 400) == 0);

    return ((year % 4) == 0);
    }
    </font>
     








    I beg to differ; I believe that this is the Java API way...
    <font face="Lucida Console, Courier" size="2">/**
    * Returns the actual maximum number of days in a month (0 based).
    *
    * @param year the Year
    * @param month the zero based month number
    * @return the maximum number of days for the month
    */
    public static int getMaximumDays(int year, int month) {
    Calendar cal = new GregorianCalendar(year, month, 1);
    return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    </font>


  • (cs) in reply to
    :

    I'm one of those anonymous guys who doesn't want to get clowned.

    I have absolutely nothing to contribute to this conversation...but I wish I did. This stuff is way over my head.

    I don't even understand this particular WTF.

    How embarassing is that?

    [Bold]I just felt like posting and taking up space in this forum.[/Bold]

    So you're like most people here, eh?

  • (cs) in reply to

    :
    Can you explain 13-month issue more? Is this a business logic or localization issue?

    Schneider

    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.

  • (unregistered)

    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.

     

  • (unregistered) in reply to Bustaz Kool

    Bustaz Kool:
    So you're like most people here, eh?

    I take offense to that!

    -Anonymous clownee #57

  • (unregistered)

    <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>
  • (unregistered)

    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

  • (cs) in reply to

    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.

  • (cs) in reply to
    :

    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.

    (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!

  • (cs) in reply to
    :

    To the anonymous poster who doesn't understand the WTF:

    When you visit this site and look at the code, do you understand any of it? I mean, does your mind start to swim and your thoughts get cloudy when you see a line of code?

    It really shouldn't hurt your brain to look at code and dissect it for its meaning. Most of the programmers in here like to take apart code and see how it works.

    Based on your comments, it sounds like you weren't meant to program. You sound like the type of person who would find for loops "confusing"...

    If you search the internet you may be able to find some kiddie programmer sites to visit and contribute to. Some where you won't get clowned on.

    Cause you sure clowned yourself here! hahaha

    The multiplicity of your anonimosties has been dull-y noted.

    No beer for you!

  • (cs) in reply to Free

    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>

  • (unregistered) in reply to KraGiE

    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?

  • (cs) in reply to

    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. :)


  • (cs) in reply to Blue

    "idioms" should be "idiosyncracies".

    Some day, perhaps we can edit our posts.  :)

  • (unregistered) in reply to

    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

  • (unregistered)

    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

  • (cs) in reply to

    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.


  • (unregistered)

    "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() 

  • (cs) in reply to

    I thought it was "Zend"

  • (unregistered) in reply to

    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

  • (unregistered) in reply to

    Yeesh, this forum sucks. The preview didn't show the HTML.

  • bo (unregistered) in reply to JamesCurran

    no becuase she is a fucking slut

     

  • Dagur (unregistered) in reply to
    Anonymous:
    *sigh*

    You mean start at 29.  28 always returns true and thus is a useless test.  Of course, other elements would need to be adjusted.


    If you really wanted to optimize you would start at 31 and go bacwkards
  • GLory (unregistered) in reply to

    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.

  • Code4Life (unregistered) in reply to KraGiE

    How about leap years? - ugh.

     

    Code4Life

     

     

     

Leave a comment on “Looking for a Date”

Log In or post as a guest

Replying to comment #:

« Return to Article