基于tm结构输出日期和月份

#include <stdio.h>
#include <time.h>

int main(void)
{
   const char *day[7] = {
      "Sunday"  , "Monday", "Tuesday", "Wednesday",
      "Thursday", "Friday", "Saturday"
   };/*www  . j a v a2 s  . co  m*/
   const char *month[12] = {
      "January",   "February", "March",    "April",
      "May",       "June",     "July",     "August",
      "September", "October",  "November", "December"
   };
   const char *suffix[] = { "st", "nd", "rd", "th" };
   enum sufindex { st, nd, rd, th } sufsel = th;       // Suffix selector

   struct tm ourT;                                     // The time structure
   time_t tVal = time(NULL);                           // Calendar time

   if (!localtime(&tVal))                      // Populate time structure
   {
      fprintf(stderr, "Failed to populate tm struct./n");
      return -1;
   }
   switch (ourT.tm_mday)
   {
   case 1: case 21: case 31:
      sufsel = st;
      break;
   case 2: case 22:
      sufsel = nd;
      break;
   case 3: case 23:
      sufsel = rd;
      break;
   default:
      sufsel = th;
      break;
   }

   printf("Today is %s the %d%s %s %d. ", day[ourT.tm_wday],
      ourT.tm_mday, suffix[sufsel], month[ourT.tm_mon], 1900 + ourT.tm_year);
   printf("The time is %d : %d : %d./n",
      ourT.tm_hour, ourT.tm_min, ourT.tm_sec);
   return 0;
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/266738.html

(0)
上一篇 2022年6月7日 23:28
下一篇 2022年6月7日 23:28

相关推荐

发表回复

登录后才能评论