SIPP 代码学习笔记2- 关于时间

 SIPP关于时间的代码。

char* CStat::formatTime (struct timeval* P_tv, bool microseconds)
{
  static char L_time [TIME_LENGTH];
  struct tm * L_currentDate;

  // Get the current date and time
  L_currentDate = localtime ((const time_t *)&P_tv->tv_sec);

  // Format the time
  if (L_currentDate == NULL)
    {
      memset (L_time, 0, TIME_LENGTH);
    }
  else
    {
 if (microseconds) {
   sprintf(L_time, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d:%03.03f",
       L_currentDate->tm_year + 1900,
       L_currentDate->tm_mon + 1,
       L_currentDate->tm_mday,
       L_currentDate->tm_hour,
       L_currentDate->tm_min,
       L_currentDate->tm_sec,
       (double)P_tv->tv_usec/(double)1000.0);
 } else {
          sprintf(L_time, "%4.4d-%2.2d-%2.2d/t%2.2d:%2.2d:%2.2d:%3.3d/t%10.10d.%6.6d",
       L_currentDate->tm_year + 1900,
       L_currentDate->tm_mon + 1,
       L_currentDate->tm_mday,
       L_currentDate->tm_hour,
       L_currentDate->tm_min,
       L_currentDate->tm_sec,
              (int) (P_tv->tv_usec)/1000,
              (long) (P_tv->tv_sec),
              (long) (P_tv->tv_usec));      
 }
    }
  return (L_time);
} /* end of formatTime */

转:C语言中的时间函数localtime和gmtime

作者:linux0818

localtime和gmtime这两个函数采用了time.h中的一个tm结构体:

struct tm
{
  int tm_sec;           /* Seconds. [0-60] (1 leap second) */
  int tm_min;           /* Minutes. [0-59] */
  int tm_hour;          /* Hours.   [0-23] */
  int tm_mday;          /* Day.     [1-31] */
  int tm_mon;           /* Month.   [0-11] */
  int tm_year;          /* Year - 1900.  */
  int tm_wday;          /* Day of week. [0-6] */
  int tm_yday;          /* Days in year.[0-365] */
  int tm_isdst;         /* DST.     [-1/0/1]*/

#ifdef  __USE_BSD
  long int tm_gmtoff;       /* Seconds east of UTC.  */
  __const char *tm_zone;    /* Timezone abbreviation.  */
#else
  long int __tm_gmtoff;     /* Seconds east of UTC.  */
  __const char *__tm_zone;  /* Timezone abbreviation.  */
#endif
};

这两个函数的原型为:

struct tm *localtime(const time_t *timep);

struct tm *gmtime(const time_t *timep);

 

具体实现为:

localtime.c

---------------------------------------------------------

#include <stdio.h>
#include <time.h>
void cur_time(void);

int main(int argc,char **argv)
{  
cur_time();

return 0;
}

void cur_time(void)
{
    char *wday[]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
    time_t timep;
    struct tm *p;
    time(&timep);
    p=localtime(&timep);  /* 获取当前时间 */
    printf("%d年%02d月%02d日",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
    printf("%s %02d:%02d:%02d/n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
}

---------------------------------------------------------

gcc localtime.c 后运行a.out结果为:

2007年12月26日星期三 11:07:15

 

gmtime.c

---------------------------------------------------------

#include <stdio.h>
#include <time.h>
void cur_time(void);

int main(int argc,char **argv)
{
  cur_time();
  return 0;
}

void cur_time(void)
{
    char *wday[]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
    time_t timep;
    struct tm *p;
    time(&timep);
    p=gmtime(&timep);  /* 获取当前时间 */
    printf("%d年%02d月%02d日",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
    printf("%s %02d:%02d:%02d/n",wday[p->tm_wday],(p->tm_hour+8),p->tm_min,p->tm_sec);
}
-----------------------------------------------------------

gcc gmtime.c 后运行a.out结果为:

2007年12月26日星期三 11:08:34

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值