Introduction: Natural Language Clock for RaspberryPi

About: Jack passed away May 20, 2018 after a long battle with cancer. His Instructables site will be kept active and questions will be answered by our son-in-law, Terry Pilling. Most of Jack's instructables are tuto…

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();
        }
      }
    }

    Attachments

    Raspberry Pi Contest

    Participated in the
    Raspberry Pi Contest