Calender

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gobinath
    New Member
    • Jul 2007
    • 2

    Calender

    Is it possible to program for an calender? if it is possible,give me an idea.
  • ravenspoint
    New Member
    • Jul 2007
    • 111

    #2
    Date calculations are notoriously difficult, with many frustrating special cases that will catch you if you do not test your code to death with every possible date.

    Do not undertake this lightly!

    That said, a good place to start is

    Code:
    	__time64_t long_time;
            struct tm myTime
    
    	_time64( &long_time );                /* Get time as long integer. */
    	myTime = *_localtime64( &long_time ); /* Convert to local time. */

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Presumably, you would have 12 months where months were 28,29,30 or 31 days based on the year.

      Can you see that a year is a container of months and a month is a container of days?

      Try to write something on paper before attempting to code.

      Comment

      • ravenspoint
        New Member
        • Jul 2007
        • 111

        #4
        Originally posted by weaknessforcats
        Presumably, you would have 12 months where months were 28,29,30 or 31 days based on the year.

        Can you see that a year is a container of months and a month is a container of days?

        Try to write something on paper before attempting to code.
        Gosh, weaknessforcats , you are so very modern with your containers of containers! Then you spoil it all by calling for paper and pencil!

        Who contains the years, I wonder? And how does poor old febuary know whether it is in a leap year or not?

        I have found that it is better to create the days, one at a time, as they are needed - just like real life, really. The "struct tm" is a handy little thing for keeping track of all the pesky details.

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by ravenspoint
          Gosh, weaknessforcats , you are so very modern with your containers of containers! Then you spoil it all by calling for paper and pencil!

          Who contains the years, I wonder? And how does poor old febuary know whether it is in a leap year or not?

          I have found that it is better to create the days, one at a time, as they are needed - just like real life, really. The "struct tm" is a handy little thing for keeping track of all the pesky details.
          if (year % 4) == 0
          nextday = feb 29
          else
          nextday = mar 1

          Comment

          • ravenspoint
            New Member
            • Jul 2007
            • 111

            #6
            Originally posted by TRScheel
            if (year % 4) == 0
            nextday = feb 29
            else
            nextday = mar 1
            Yes, this works if the month knows what year it is contained by. Which is why I asked about just that.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by ravenspoint
              Gosh, weaknessforcats , you are so very modern with your containers of containers! Then you spoil it all by calling for paper and pencil!

              Who contains the years, I wonder? And how does poor old febuary know whether it is in a leap year or not?
              Yes, indeed. Who does contain the years? Are there years?? What kind calendar it is it?? Gregorian? Tibetan? Astronomical? Julian?

              You need to desgin before coding. That iswhy things like UML are in existence in the first place.

              A struct tm may not work in a particular application or operating system.

              Comment

              • ravenspoint
                New Member
                • Jul 2007
                • 111

                #8
                Originally posted by weaknessforcats
                A struct tm may not work in a particular application or operating system.
                Really? As I understand it, it is part of the C run time library. I believe that a compliant C compiler toolset should provide it.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  It is not required that C be supported by C++. In several areas C code just doesn't compile in C++.

                  Comment

                  • Darryl
                    New Member
                    • May 2007
                    • 86

                    #10
                    Originally posted by TRScheel
                    if (year % 4) == 0
                    nextday = feb 29
                    else
                    nextday = mar 1
                    What's the day after Febuary 28, 1900?

                    Comment

                    • TRScheel
                      Recognized Expert Contributor
                      • Apr 2007
                      • 638

                      #11
                      Originally posted by Darryl
                      What's the day after Febuary 28, 1900?
                      Throw in % 100 to check those years. Or make a method that returns leap year or not.

                      So that would be something like

                      [code=cpp]
                      bool LeapYear()
                      {
                      return currentYear % 4 == 0 && currentYear % 100 != 0;
                      }
                      [/code]

                      Make that accessible to the months, particularly February, and February checks that before going to the 29th. If all months inherit a base, you could override the next day function on February to just check that, while every other month is oblivious to the leap year. Sadly, this is a model where the month does have to be aware of what year it is in.

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        I think the leap year check is that the leap year is divisible by 4 and not by 400.

                        1600 and 2000 were not leap years.

                        If your calendar doesn't need to go out that far, the 400 check isn't worth it.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          For the Gregorian calendar, Zeller's congruence method comes in extremely
                          handy; google for it.

                          kind regards,

                          Jos

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Originally posted by JOsAH
                            Zeller's congruence
                            What is that!! I have seen you pull all sorts of rabbits like this out of your hat.

                            Where did you learn all of this stuff? :)

                            Comment

                            • weaknessforcats
                              Recognized Expert Expert
                              • Mar 2007
                              • 9214

                              #15
                              2,620 hits on Google for Zeller's Congruence.

                              What else have I failed to learn?

                              Comment

                              Working...