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

你可能感兴趣的:(linux,Date,timezone,struct,null,语言)