System clock

This is a discussion on System clock within the C Programming forums, part of the General Programming Boards category; Hey guys im trying to exstract the hour from the system clock and then trying to fill it in the ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    System clock

    Hey guys im trying to exstract the hour from the system clock and then trying to fill it in the clock2 array so i can display it like an actual clock. But when i display the clock it has to print
    'H' on the hour and 'm' for the minutes. Iv done something similar just before but im having trouble with doing it from the system time.

    So far the function shows this..

    for example it prints

    Sat Feb 17 15:17:10 EST 2007

    then it displays a round clock with hours 1 -12

    [code


    say if its one oclock it will print 2 HH on it i have to do same thing for minutes



    what can i do to fill the array with the system hour and minutes not sure if this is correct
    its not working but i thought maybe i could copy ptr into hour but only copy %I which is the hour then put that in the array

    /* updating the system hour within the clock array */
    /* strftime(hour, 100, "%I", ptr); */


    clock2[hr[hour]][hc[hour]] = hs;
    clock2[hr[hour]][hc[hour]+ 1] = hs;


    [/code]

    Code:
    void displayClock2(int * optionStats)
    {
       /*declarations*/
       struct tm *ptr; 
       time_t lt;
       int i, j;
       int tm_min;
       int tm_hour;
       char str[80];
       char hour[30];
       char minutes[30];
       char clock2[9][20] = { /* The array to hold the hard-coded clock */
            "        12         ",
            "   11        01    ",
            " 10            02  ",
            "                   ",
            "09      .       03 ",
            "                   ",
            " 08            04  ",
            "   07        05    ",
            "        06         "
          };
    
       /* Row and column numbers in clock array for hour positions. For
       * example, the "06" in the clock display is stored at position
       * [8, 8] in the clock matrix (see above) */
    
       const int hr[13] = {0, 1, 2, 4, 6, 7, 8, 7, 6, 4, 2, 1, 0};
       const int hc[13] = {0, 13, 15, 16, 15, 13, 8, 3, 1, 0, 1, 3, 8};	
        
       /* Hour and minute codes */
       const char hs = 'H', ms = 'm';
    
       lt = time(NULL);
       ptr = localtime(&lt);
       strftime(str, 100, "%a %b %d %X %Z %Y", ptr); 
    
       /* copies ptr time struct into str string then 
       formats string to day month day of month time
       then time zone then year*/
       printf("\n");
       printf(str);
       printf("\n");
    
       /* once the struct gets copied into our formatted string
       it can get printed out to the screen*/
    
       /* updating the system hour within the clock array*/
      /* strftime(hour, 100, "%I", ptr); */
       
    
       clock2[hr[hour]][hc[hour]] = hs;
       clock2[hr[hour]][hc[hour]+ 1] = hs;
       
       /* Display the clock */
        for (i = 0; i < 9; i++)
        {
            for (j = 0; j < 19; j++)
        	    printf("%c", clock2[i][j]);
            printf("\n");
        }
        printf("\n");
    
    
    }
    Last edited by bazzano; 03-27-2007 at 06:51 AM.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Hi..

    Hey can you also give the example of what you are expecting.. im' a bit confused.. about your requirement

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > strftime(str, 100, "&#37;a %b %d %X %Z %Y", ptr);
    Rather than formatting it as a string, only to sscanf / strtod it back again, why not just do

    hr = ptr->tm_hour;
    etc
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    thankyou i tried that and i now get

    i get an inconpatible types in assignment error? And also if i use tm_hour i get the whole 24 hour clock could i make it only between 1-12
    Last edited by bazzano; 03-27-2007 at 08:40 AM.

  5. #5
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    iv done that and now i get a segmentation fault trying to exstract the hour then place it into the clock array and overrite it with the 'H' which is what hs is; char hs = 'h' for example

    Code:
    clock2[hr[ptr->tm_hour]][hc[ptr->tm_hour]] = hs;
       clock2[hr[ptr->tm_min]][hc[ptr->tm_min]] = hs;

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    This reminds me a lot of the last thread, where I told you 3 times that minutes goes from 0 to 59 and your hc/hr array only is of size 13:

    segmentation fault with strtod
    segmentation fault with strtod

  7. #7
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Yehhh i thought it was because of that but then how i would use ptr->tm_hour to only get hours between 1-12 and not the whole 24 hour. If i can somehow only give out 1-12 i shouldnt seg fault but when i use ptr->tm_hour it will say something like 23 for hours because of the 24 hour clock. How can i just make it 11 oclock instead of 23

    thanks for helping too

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what about ptr->tm_hour &#37;12
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, to transform an 24hour system into an 12hour system, you can use:

    int hour = ptr->tm_hour &#37;13;

    That will give you values between 0 and 12, or

    int hour = ptr->tm_hour %12;

    if you want between 0 and 11.

    This solution won't work for the minutes though ...

  10. #10
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    kewl ill give it a try

  11. #11
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    modula for clock

    Hey guys iv done the modular and it does print HH on the hour however it prints it on the wrong out. For example if the system time is two oclock it will print the HH on one oclock instead.


    Code:
    void displayClock2(int * optionStats)
    {
       /*declarations*/
       struct tm *ptr; 
       time_t lt;
       int i, j;
       int tm_min;
       int tm_hour;
       int hour = tm_hour &#37; 12;
       int a;
       char str[80];
      
       char clock2[9][20] = { /* The array to hold the hard-coded clock */
            "        12         ",
            "   11        01    ",
            " 10            02  ",
            "                   ",
            "09      .       03 ",
            "                   ",
            " 08            04  ",
            "   07        05    ",
            "        06         "
          };
    
       /* Row and column numbers in clock array for hour positions. For
       * example, the "06" in the clock display is stored at position
       * [8, 8] in the clock matrix (see above) */
    
       const int hr[13] = {0, 1, 2, 4, 6, 7, 8, 7, 6, 4, 2, 1, 0};
       const int hc[13] = {0, 13, 15, 16, 15, 13, 8, 3, 1, 0, 1, 3, 8};	
        
       /* Hour and minute codes */
       const char hs = 'H', ms = 'm';
       
       lt = time(NULL);
       ptr = localtime(&lt);
       
       strftime(str, 100, "%a %b %d %X %Z %Y", ptr); 
       /*hr = ptr -> tm_hour;*/
    
       /* copies ptr time struct into str string then 
       formats string to day month day of month time
       then time zone then year*/
       
       printf("\n");
       printf(str);
       printf("\n");
      
       /* once the struct gets copied into our formatted string
       it can get printed out to the screen*/
    
       /* updating the system hour within the clock array*/
       /*
       strftime(hour, 100, "%I", ptr); 
       strftime(minutes, 100, "%M", ptr);
       hour = (int) strtod(*ptr, hour);
       printf(hour);
       printf(minutes);
       */
       
       
       
       clock2[hr[hour]][hc[hour]+1] = hs;
       clock2[hr[hour]][hc[hour]] = hs;
    
       /* Round m to the nearest hour position */
       a = ptr->tm_min % 5;
       if( a >= 3)
       {
         a = ptr->tm_min / 5 + 1;
                      
       }
       else
       {
         a = ptr->tm_min / 5;
         clock2[hr[a]][hc[a]+ 1] = hs;
         clock2[hr[a]][hc[a]] = ms;
       }
       /* getting seg fault here array not large enough for what im
          trying to do*/
       if(a == 0)
       {
          a =12;
       }
       if(a==60)
       {
          a = 12;
       }
       clock2[hr[a]][hc[a]] = ms;
       clock2[hr[a]][hc[a]+ 1] = ms;
    
    
       /*
       
       clock2[hr[hour]][hc[hour]] = hs;
       clock2[hr[hour]][hc[hour]+ 1] = hs;*/
       
       /* Display the clock */
        for (i = 0; i < 9; i++)
        {
            for (j = 0; j < 19; j++)
        	    printf("%c", clock2[i][j]);
            printf("\n");
        }
        printf("\n");
    
    
    }
    Last edited by bazzano; 03-27-2007 at 09:30 PM. Reason: adjust code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Error in Clock program
    By SVXX in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2009, 01:12 AM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 04:56 PM
  4. New system build wont boot
    By lightatdawn in forum Tech Board
    Replies: 7
    Last Post: 12-02-2005, 06:58 AM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21