SIPP 代码学习笔记2- 关于时间
2008-05-18 22:47
344 查看
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 */
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
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
作者:linux0818localtime和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
相关文章推荐
- SIPP 代码学习笔记3- 关于函数
- RHEL7关于时间的学习笔记
- 5.7学习笔记(关于选取本周的时间)
- PHP学习笔记--php教科书上关于正则表达式实例代码(摘要)
- 高性能Javascript 记录代码运行时间学习笔记
- 关于时间格式的学习笔记
- Unity学习笔记 之 关于 Unity UI 的 Slider 的代码记录
- 黑马程序员学习笔记——关于时间复杂度计算2
- SIPP 代码学习笔记1- compress
- Unity学习笔记 之 关于 Unity UI 的 Slider 的代码记录
- php学习笔记--关于时间戳
- cocos代码研究(6)有限时间动作类(FiniteTimeAction)学习笔记
- 黑马程序员学习笔记——关于时间复杂度计算1
- 《深度探索c++对象模型》 学习笔记 - 1 关于对象
- 【学习笔记04】java面向对象-构造函数、构造代码块
- Java学习笔记----代码块
- Qt学习笔记4-label显示时间
- 关于JQuery 插件Grid的学习使用笔记 (PHP版)
- [转] 关于《Linux常用调试和性能分析工具》的学习笔记
- 【IOS移动开发技术】iOS软件开发中关于屏幕旋转处理相关的学习笔记