Thread: Printing weekday

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question Printing weekday

    With my program below, i want in the do_day finction to print out the day of the week....

    so for example, the user types in "d 5 9 2002"
    i want the program to print out "5 September is a Thursday"

    Can anyone help me with this???

    Cheers.



    #include <stdio.h>
    #include <time.h>
    #include <sys/time.h>
    #include "date.h"

    /* true for leap years */

    /***************************DATE.C***************** *********/

    int leap_year (int year) {


    return year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
    }

    /* the number of days in month of year */



    int days_in_month (int month, int year) {


    switch (month) {
    case 9: case 4: case 6: case 11:
    return 30;
    case 2:
    return leap_year(year) ? 29 : 28;
    default:
    return 31;
    }
    }

    /* the weekday (0 = Sunday) of a given date
    accurate for dates in the Gregorian calendar
    which began on 14 September 1752 */



    int week_day (int day, int month, int year) {


    int d = 14; /* The modern calendar began on 14 ... */
    int m = 9; /* ... September ... */
    int y = 1752; /* ... 1752 ... */
    int n = 4; /* ... which was a Thursday */

    while (year > y || month > m || day > d) {
    n++;
    day--;
    if (day < 1) { month--; day = days_in_month(month, year); }
    if (month < 1) { year--; month = 12; }
    }
    return n % 7;
    }

    struct timeval tv;



    int current_day () {


    gettimeofday(&tv, 0);
    return localtime(&tv.tv_sec)->tm_mday;
    }



    int current_month () {


    gettimeofday(&tv, 0);
    return localtime(&tv.tv_sec)->tm_mon + 1;
    }



    int current_year () {


    gettimeofday(&tv, 0);
    return localtime(&tv.tv_sec)->tm_year + 1900;
    }

    /************************MY PROGRAM**********************/


    int do_day ( char *cmd ) {

    int day, month, year;
    int res;
    res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year );
    if ( res == 3 ) {
    switch(month){
    case 1:
    printf("%d January %d is a ", day, year);
    week_day(day, month, year);
    break;
    case 2:
    printf("%d February %d is a ", day, year);
    break;
    case 3:
    printf("%d March %d is a ", day, year);
    break;
    case 4:
    printf("%d April %d is a ", day, year);
    break;
    case 5:
    printf("%d May %d is a ", day, year);
    break;
    case 6:
    printf("%d June %d is a ", day, year);
    break;
    case 7:
    printf("%d July %d is a ", day, year);
    break;
    case 8:
    printf("%d August %d is a ", day, year);
    break;
    case 9:
    printf("%d September %d is a ", day, year);
    break;
    case 10:
    printf("%d October %d is a ", day, year);
    break;
    case 11:
    printf("%d November %d is a ", day, year);
    break;
    case 12:
    printf("%d December %d is a ", day, year);
    break;
    default :
    printf("Invalid month\n");
    return 0;
    }}
    else{
    printf( "Bad day command parameters\n" );
    }
    }


    int do_month ( char *cmd ) {

    int month, year;
    int res;
    res = sscanf( cmd, "%*c %d %d", &month, &year );
    if ( res == 2 ) {
    switch(month){
    case 1:
    printf("January\n");
    break;
    case 2:
    printf("February\n");
    break;
    case 3:
    printf("March\n");
    break;
    case 4:
    printf("April\n");
    break;
    case 5:
    printf("May\n");
    break;
    case 6:
    printf("June\n");
    break;
    case 7:
    printf("July\n");
    break;
    case 8:
    printf("August\n");
    break;
    case 9:
    printf("September\n");
    break;
    case 10:
    printf("October\n");
    break;
    case 11:
    printf("November ");
    break;
    case 12:
    printf("December\n");
    break;
    default :
    printf("Invalid month\n");
    return 0;
    }
    }
    else {
    printf( "Bad month command parameters\n" );
    }}


    void do_year ( char *cmd ) {

    int year;
    int res;
    res = sscanf( cmd, "%*c %d", &year );
    if ( res == 1 ) {
    printf("%d \n", year);
    }
    else {
    printf( "Bad year command parameters\n" );
    }
    }


    int first_day_of_month ( int year, int month ) {

    struct tm t = { 0 };
    struct tm *tp;
    time_t now;
    t.tm_mday = 1;
    t.tm_mon = month-1;
    t.tm_year = year-1900;
    now = mktime(&t);
    tp = localtime(&now);
    return tp->tm_wday;
    }


    int main ( ) {

    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
    switch ( buff[0] ) {
    case 'd':
    do_day ( buff );
    return 0;
    case 'm':
    do_month ( buff );
    return 0;
    case 'y':
    do_year ( buff );
    return 0;
    default:
    printf( "Unknown command \n", buff[0] );
    return 0;
    }
    }
    {
    int i;
    for ( i = 1 ; i <= 12 ; i++ ) {
    int day = first_day_of_month( 2002, i );
    printf( "First day of %d/%d is %d\n", 2002, i, day );
    }
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Good little program, fixed it up heres the file, it only works for january at the moment though I'll let you fix the rest up, partly cos Im lazy and partly cos it'll help you understand. Also you should with program here put the declarations of all the functions at the top. Ive put a required function defition before the do_day but it should be moved to the top with the rest of the function to be defined.

    cheers
    kwigibo

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question only works for 2002?

    The code there, only works for 2002... i need it to work for other years as well...how do i go about doing that?
    cheers once again!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    search these boards for "calender". It may also be in cprogs source section too.
    Ages ago i knocked up a calender app to illustrate zellers algorithm. this will return the day for a given date in the gregorian calender. look at that piece of code. feel free to use bits of it. It will help you do what you want.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Next time, use the code tags (see the vB Code help when you reply), and press the "Preview" before "Submit"

    Code:
    #include <stdio.h>
    #include <time.h>
    
    // "5 September is a Thursday"
    void do_day( char *cmd ) {
        int day, month, year;
        int res;
        res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year );
        if ( res == 3 ) {
            char       buff[100];
            struct tm  t1 = { 0 };
            struct tm *tp;
            time_t now;
    
            t1.tm_mday = day;
            t1.tm_mon = month-1;
            t1.tm_year = year-1900;
            now = mktime(&t1);
            tp = localtime(&now);
            strftime( buff, sizeof(buff), "%d %B is a %A", tp );
            printf( "%s\n", buff );
        } else {
            printf( "Bad day command parameters\n" );
        }
    }
    
    void do_month ( char *cmd ) {
    }
    void do_year ( char *cmd ) {
    }
    
    int main( ) {
        char buff[BUFSIZ];
        while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
            if ( buff[0] == 'q' ) break;
            switch(buff[0]) {
            case 'd':
                do_day( buff );
                break;
            case 'm':
                do_month( buff );
                break;
            case 'y': 
                do_year( buff );
                break;
            default:
                printf( "Unknown command %c\n", buff[0] );
                break;
            }
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    51
    Just a quick note to say thankyou for everyones help
    I really appreciated it, and youve helped me learn.
    Thanks !!!!

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question below 1902 not working

    Any year entered before 1902 doesnt work. Anyone tell me why?? cheers.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, I don't know which code your using, but if you've got a "struct tm", then the element tm_year is years since 1900. Maybe that's your problem? If not, so us a snippet.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question

    Yeah i am using the tm code.... how do i get around it so that it will do years before 1902? i need it to do at least back to 1752.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    simple! use zellers algo as i told u a few days ago. The code linked shows an example of zellers algo in use. Google will throw up about 100000 links for zellers algorithm which will go into it in more detail than my comments do but i believe you should be able to understand whats going on by reading thru that code.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    51

    Question

    Thanx stoned coder, i completely missed that post about the zellers algo. Just one question about it. The function bool isleapyear() doesnt work for me, i didnt think C could handle boolean values, only 1 and 0. Can you please explain this a little bit to me, and how i make it work for me. This code is really helping me learn how it all works. Thanx for your help.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    2 options.... change bool to int or add a typedef....

    typedef int bool;
    bool TRUE=1;
    bool FALSE=0;

    and away you go.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing using DrawText() or TextOut()
    By Eversman in forum Windows Programming
    Replies: 1
    Last Post: 05-24-2004, 12:12 PM