Thread: Another Calendar thread :O

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    106

    Another Calendar thread :O

    Hi Guys,

    It’s been a while since posting here, but I came by an old closed thread while Googling:
    Writing a program to make a calendar

    I’m trying to write a calendar program in BASIC that receives year, month,day, and also the progressive input of a tick per day from a clock to increment each of the 3 date variables as appropriate.
    I’m not concerned at all about formatting of the calendar for printing.

    It occurs to me that no matter, I still have to iterate days of year, etc.
    taking more time to run this function as time goes by...


    Code:
    int day_of_month(int month, int year)
    {
        /* 1-1-1 was a monday */
        for(number of years)
        {
            day =  (day + days_in_year(year) )% 7;
        }
        for(number of months)
        {
            day = (day + days_in_month[month])%7;
            /* days_in_month array is updated according to the current 'year' variable in days_in_year()*/
        }
        return day;
    }
    It seems no matter how it’s done, if it were part of a clock that also updated a HH:MM:SS display, it would always glitch the display at the stroke of midnight each day.
    I suppose on modern platforms this could be recalculated and stored in RAM,
    but could anyone shed some light on how it was done on old computers if the platform had no real time clock?
    Or did they just not keep the time/date if they lacked a real time clock chip?

    Cheers, Art.

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by xArt View Post
    <snip>

    I’m trying to write a calendar program in BASIC that receives year, month,day, and also the progressive input of a tick per day from a clock to increment each of the 3 date variables as appropriate.

    <snip>
    This subforum is about C programming and I see no question relating to C in your post, so I think you are in the wrong forum buddy.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jimmy
    This subforum is about C programming and I see no question relating to C in your post, so I think you are in the wrong forum buddy.
    Indeed.

    *moved to Tech Board*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Ok C then (because it makes no difference).

    I solved my own problem best I think I can.
    You only have to run all the routines when the date is first set.

    From there you only have to check leap year and populate number of days array when the year changes.
    When the month changes you have to move the days in month index to look at days in the next month.
    When the day changes you only have to check it didn’t exceed the days in the month,
    and increment/reset the day of the week.
    Last edited by xArt; 01-09-2015 at 04:15 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you have a criterion for deciding if a year is a leap year, you can also implement some function that computes the number of times 29 Feb occurs between two dates (without necessarily looping).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    It didn’t turn out near as slow as I thought.
    I ended up just running the entire algo for every time the day ticks over.

    The only problem encountered is I’m counting the total days into a 16 bit value,
    so the range is about 175 years or so before the word overflows for finding the day of the week.
    I limited the year range from 1990 (which also starts on Monday) and 2150.

    If I were that keen I could get a small table of years that also start on a Monday to
    begin iteration from, but it should last longer than I live as it is


    Quote Originally Posted by grumpy View Post
    If you have a criterion for deciding if a year is a leap year, you can also implement some function that computes the number of times 29 Feb occurs between two dates (without necessarily looping).

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    So something like this:

    Code:
    ' increment date if day changed over
    '
    if DayChanged = 1 then
    calday = calday + 1
    if calday > caldim[calmonth-1] then
    calday = 1
    calmonth = calmonth + 1
    endif
    if calmonth > 12 then
    calmonth = 1
    calyear = calyear + 1
    endif
    '
    DayChanged = 0
    gosub setcal
    endif ' DayChanged
    '
    ‘
    goto main
    ‘



    This is the calendar that is called as a subroutine called “setcal”:


    Code:
    '
    ' ******************* set calendar date and find day of week *******************
    '
    setcal:						' find leap year status
    caldim[0] = 31				' set default days in month table
    caldim[1] = 28				' days in February is changed to 29 by the program
    caldim[2] = 31				'
    caldim[3] = 30				'
    caldim[4] = 31				'
    caldim[5] = 30				'
    caldim[6] = 31				'
    caldim[7] = 31				'
    caldim[8] = 30				'
    caldim[9] = 31				'
    caldim[10] = 30				'
    caldim[11] = 31				'
    '
    calleap = 0					' clear leap year status
    if calyear // 4 = 0 then	'
    calleap = 1					' set leap year status
    if calyear // 100 = 0 then	'
    calleap = 0					' clear leap year status
    if calyear // 400 = 0 then	'
    calleap = 1					' set leap year status
    endif						'
    endif						'
    endif						'
    '
    if calleap = 1 then			'
    caldim[1] = 29				'
    endif						'
    '
    calbadentry = 0				' reset calendar bad entry status
    if calmonth = 0 then		' check for invalid date input
    calbadentry = 1				'
    calmonth = 1				'
    endif						'
    if calmonth > 12 then		'
    calbadentry = 1				'
    calmonth = 1				'
    endif						'
    if calday = 0 then			'
    calbadentry = 1				'
    calday = 1					'
    endif						'
    if calday > caldim[calmonth-1] then
    calbadentry = 1				'
    calday = 1					'
    endif						'
    if calyear < 1800 then		' implement lower year range restriction
    calbadentry = 1				' prevent far away dates slowing program down
    calyear = 1800				'
    endif						'
    if calyear > 2300 then		' implement higher year range restriction
    calbadentry = 1				' in case of errors far into the future
    calyear = 2300				'
    endif						' the hardware probably won't last that long
    '
    ' day of week
    '
    xwork = 2310					' find best epoch year to iterate from
    cacnt = 12						'
    while xwork >= calyear			'
    gosub epochyears				'
    cacnt = cacnt - 1				'
    wend							'
    '
    '
    cwork = 0 ' working variable for days
    '
    for cacnt = xwork to calyear - 1' iterate years from epoch year
    calleapx = 0					' clear leap year status
    if cacnt // 4 = 0 then			'
    calleapx = 1					' set leap year status
    if cacnt // 100 = 0 then		'
    calleapx = 0					' clear leap year status
    if cacnt // 400 = 0 then		'
    calleapx = 1					' set leap year status
    endif							'
    endif							'
    endif							'
    '
    if calleapx = 0 then			' add days over the years from epoch
    cwork = (cwork + 365)//7		'
    else							'
    cwork = (cwork + 366)//7		'
    endif							'
    '
    next cacnt						'
    '
    if calleap = 1 then				' fix day of month table for current year
    caldim[1] = 29					'
    else							'
    caldim[1] = 28					'
    endif							'
    '
    cacnt = calmonth-1				' add days for elapsed months of current year
    while cacnt > 0					'
    xwork = caldim[cacnt-1]			'
    cwork = (cwork + xwork)//7		'
    cacnt = cacnt - 1				'
    wend							'
    '
    cacnt = calday-1				' add days elapsed of current month
    xwork = 0						'
    while cacnt > 0					'
    xwork = xwork + 1				'
    cacnt = cacnt - 1				'
    wend							'
    cwork = (cwork + xwork)//7		'
    '
    '
    caldow = cwork + 1				' shift range for display so 0=Sunday, 6=Saturday.
    if caldow = 7 then caldow = 0	'
    return
    '
    '
    epochyears:
    lookup2 cacnt,[1798,1849,1900,1951,1990,2035,2052,2091,2125,2153,2198,2227,2244],xwork
    return
    '

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2011, 12:01 AM
  2. Calendar in C
    By Prolapsis in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 10:56 AM
  3. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  4. C++ calendar HELP!!!
    By chocha7 in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2004, 08:06 AM
  5. calendar
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-10-2002, 04:10 PM