loading

All you will need is a RaspberryPi, any model will do.

This program was written to help reacquaint me with string handling in C. The RaspberryPi got me interested in programming again after not doing it in about 20 years.

If you have never used the C compiler included in Raspian now is a good time to try it.

A thank you goes out to PeterO from the RaspberryPi forum for helping me with getting the system time.

    Step 1: Testing

    The program was tested on a 1 GHz model B and on the new model 2B.

    Step 2: The Program

    Copy this program onto your RaspberryPi.

    Compile it with the command: gcc -o clock clock.c

    And run it with the command: ./clock

    /*********************************************************
     * Filename: clock.c
     *
     * A natural language clock program for the RaspberryPi.
     *
     * Compile with the command:
     *   gcc -o clock clock.c
     *
     * Execute with the command: ./clock
     *
     **********************************************************/
    
    #include <time.h>
    #include <stdio.h>
    
    #define clrscr() printf("\e[1;1H\e[2J")
    
    char hours[24][7] = {"Twelve",
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Eleven",
        "Twelve",
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Eleven"};
    
    char minutes[60][13] = {"zero",
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
        "Six",
        "Seven",
        "Eight",
        "Nine",
        "Ten",
        "Eleven",
        "Twelve",
        "Thirteen",
        "Fourteen",
        "Fifteen",
        "Sixteen",
        "Seventeen",
        "Eighteen",
        "Nineteen",
        "Twenty",
        "Twenty-one",
        "Twenty-two",
        "Twenty-three",
        "Twenty-four",
        "Twenty-five",
        "Twenty-six",
        "Twenty-seven",
        "Twenty-eight",
        "Twenty-nine",
        "Thirty",
        "Thirty-one",
        "Thirty-two",
        "Thirty-three",
        "Thirty-four",
        "Thirty-five",
        "Thirty-six",
        "Thirty-seven",
        "Thirty-eight",
        "Thirty-nine",
        "Forty",
        "Forty-one",
        "Forty-two",
        "Forty-three",
        "Forty-four",
        "Forty-five",
        "Forty-six",
        "Forty-seven",
        "Forty-eight",
        "Forty-nine",
        "Fifty",
        "Fifty-one",
        "Fifty-two",
        "Fifty-three",
        "Fifty-four",
        "Fifty-five",
        "Fifty-six",
        "Fifty-seven",
        "Fifty-eight",
        "Fifty-nine",};
    
    char dow[7][10] = {"Sunday","Monday","Tuesday","Wednesday",
                       "Thursday","Friday","Saturday"};
    
    char months[12][10] = {"January","February","March","April",
                           "May","June","July","August","September",
                           "October","November","December"};
    
    char days[32][14] = {"zero",
        "First",
        "Second",
        "Third",
        "Forth",
        "Fifth",
        "Sixth",
        "Seventh",
        "Eighth",
        "Ninth",
        "Tenth",
        "Eleventh",
        "Twelfth",
        "Thirteenth",
        "Fourteenth",
        "Fifteenth",
        "Sixteenth",
        "Seventeenth",
        "Eighteenth",
        "Nineteenth",
        "Twentieth",
        "Twenty-first",
        "Twenty-second",
        "Twenty-third",
        "Twenty-forth",
        "Twenty-fifth",
        "Twenty-sixth",
        "Twenty-seventh",
        "Twenty-eighth",
        "Twenty-ninth",
        "Thirtieth",
        "Thirty-first"};
    
    struct tm tm2;
    
    int timeh, timem, times;  // Current hours and minutes and seconds.
    int date, month, year, wday;
    
    /*********************************************************
     * gettime() function - Loads the current time into the 
     * global variables. It puts the hours in the variable 
     * timeh and the minutes in timem, and so on.
     *********************************************************/
    void gettime()
    {
      time_t now;
      char *str;
    
      now = time(NULL);                       // Get the time.
      str = ctime(&now);                      // Convert it to a string.
      strptime(str,"%a %b %d %H:%M:%S %Y",&tm2); // Convert from the string version.
    
      timeh = tm2.tm_hour;                    // Copy hours, minutes and seconds
      timem = tm2.tm_min;                     // into global variables.
      times = tm2.tm_sec;
      date = tm2.tm_mday;
      month = tm2.tm_mon;
      year = tm2.tm_year;
      wday = tm2.tm_wday;
    }
    
    /*********************************************************
     * dspsec() function - Displays the seconds in the 
     * variable times. Seconds uses the same array as minutes.
     *********************************************************/
    void dspsec(T2)
    {
      if(times>0) printf("and %s seconds ", *minutes+(T2*13));
    }  
    
    /*********************************************************
     * dsptime() function - Clears the screen and displays the 
     * time in natural language. It uses the hours in the
     * variable timeh and the minutes in timem.
     *********************************************************/
    void dsptime()
    {
      int timem2, timeh2, times2; 
      printf("\n\n\n\n\n\n\n\n\n           The time is now:\n");
      switch(timem)
      {
        case 0:
          printf(           "%s O'clock\n",*hours+(timeh*7));
          dspsec(times);    
          break;
    
        case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:
        case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20:
        case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
        case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39: case 40:
        case 41: case 42: case 43: case 44: case 45:
          if(timem==1) printf("           %s minute ",*minutes+(timem*13));
          else printf("           %s minutes ",*minutes+(timem*13));
          dspsec(times);    
          printf("past %s o'clock",*hours+(timeh*7));
          break;
    
        case 46: case 47: case 48: case 49: case 50:
        case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59:
          timem2 = 60-timem;
          timeh++;
          if(timem2==1) printf("           %s minute ",*minutes+(timem2*13));
          else printf("           %s minutes ",*minutes+(timem2*13));
          dspsec(60-times);
          printf("until %s o'clock",*hours+(timeh*7));
          break;
      }
    
      if(timeh<13) printf(" AM\n");
      else printf(" PM\n");
    
      printf("           %s, %s %s %d\n\n\n\n\n\n\n", *dow+(wday*10), *months+(month*10), *days+(date*14) ,(1900+year));
    }
    
    /**************************************************************
     * main() function
     **************************************************************/
    main()
    {
      int ptime = 70; // previous time after gettime()
    
       while(1)
      {
        gettime();
        if(ptime != times)
        {
          clrscr();
          ptime = times;
          dsptime();
        }
      }
    }
    <p>Please check out my RaspberryPi collection located here:<br></p><p><a href="https://www.instructables.com/id/RaspberryPi-1/" rel="nofollow">https://www.instructables.com/id/RaspberryPi-1/</a></p><p>If you like this please vote for it in the RaspberryPi contest.</p>
    <p>Glad to see that there are still people who prefer to use C over Python. By the way, since I found your idea interesting I decided to make some enhancements to your code.</p><p>By using ncurses I converted your program to a full screen terminal application. Now the screen doesn't scroll down every time the clock refreshes. Also, when you press 'q' (or 'Q') the program terminates and you return to the same point you were before running it.</p><p>To compile it you need to use the following command:</p><p>gcc -o clock clock.c -lncurses</p><p>The code can be found <a href="https://gist.github.com/magkopian/070c3137b0a0e11a54fc" rel="nofollow">here</a>.</p>
    <p>I forgot to say that you need to have installed the <strong>libncurses5-dev</strong> package too, otherwise you won't be able to compile the code. If you don't just run:</p><blockquote>sudo apt-get update &amp;&amp; sudo apt-get install libncurses5-dev</blockquote>
    <p>Nice addition to my instructable. If you hurry you can enter it in the Re-mix 2.0 contest. The contest ends today (9/21/15).</p>
    <p>I don't think that such a small addition, worths making a whole new instructable just to enter a contest, but thanks anyway.</p>

    About This Instructable

    584views

    5favorites

    License:

    Bio: Most of my instructables will be tutorials for Atmel microcontrollers, Arduino, or Raspberrypi. I try to show concepts that you can use in your own ... More »
    More by JRV31:MoonLight 7400 Quad Nand Gate Simulation RaspberryPi: set-up WIFI in Jessie-Lite 
    Add instructable to: