微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐