使用C的库函数获取当前本地时间
2012-12-30 14:23
393 查看
1. tm结构体
struct tm {
inttm_sec;
/* seconds after the minute [0-60] */
int tm_min;/* minutes after the hour [0-59] */
int tm_hour;/* hours since midnight [0-23] */
int tm_mday;/* day of the month [1-31] */
int tm_mon;/* months since January [0-11] */
int tm_year;/* years since 1900 */
int tm_wday;/* days since Sunday [0-6] */
int tm_yday;/* days since January 1 [0-365] */
int tm_isdst;/* Daylight Savings Time flag */
long tm_gmtoff;/* offset from CUT in seconds */
char *tm_zone;/* timezone abbreviation */
};
2. 使用localtime获取本地时间,具体代码如下
//获取当前时间,采用C的库函数,返回值不需要外部释放,效率方面比使用OC的NSDate类高效3-4倍
- (struct tm*)getTime
{
//时间格式
struct timeval ticks;
gettimeofday(&ticks, nil);
time_t now;
struct tm* timeNow;
time(&now);
timeNow = localtime(&now);
timeNow->tm_gmtoff = ticks.tv_usec/1000; //毫秒
timeNow->tm_year += 1900; //tm中的tm_year是从1900至今数
timeNow->tm_mon += 1; //tm_mon范围是0-11
return timeNow;
}
struct tm {
inttm_sec;
/* seconds after the minute [0-60] */
int tm_min;/* minutes after the hour [0-59] */
int tm_hour;/* hours since midnight [0-23] */
int tm_mday;/* day of the month [1-31] */
int tm_mon;/* months since January [0-11] */
int tm_year;/* years since 1900 */
int tm_wday;/* days since Sunday [0-6] */
int tm_yday;/* days since January 1 [0-365] */
int tm_isdst;/* Daylight Savings Time flag */
long tm_gmtoff;/* offset from CUT in seconds */
char *tm_zone;/* timezone abbreviation */
};
2. 使用localtime获取本地时间,具体代码如下
//获取当前时间,采用C的库函数,返回值不需要外部释放,效率方面比使用OC的NSDate类高效3-4倍
- (struct tm*)getTime
{
//时间格式
struct timeval ticks;
gettimeofday(&ticks, nil);
time_t now;
struct tm* timeNow;
time(&now);
timeNow = localtime(&now);
timeNow->tm_gmtoff = ticks.tv_usec/1000; //毫秒
timeNow->tm_year += 1900; //tm中的tm_year是从1900至今数
timeNow->tm_mon += 1; //tm_mon范围是0-11
return timeNow;
}
相关文章推荐
- 使用C的库函数获取当前本地时间
- C#获取时间戳的封装方法函数+使用获取当前时间时间戳
- localtime()函数:获取当前时间和日期并转换为本地时间
- localtime()函数:获取当前时间和日期并转换为本地时间
- C语言localtime()函数:获取当前时间和日期并转换为本地时间
- C语言localtime()函数:获取当前时间和日期并转换为本地时间
- php使用gettimeofday函数返回当前时间并存放在关联数组里
- 如何获取本地时间信息,并把getDay()函数获取到的日期改为日一二三四五六
- 使用Smarty 获取当前日期时间和格式化日期时间的方法详解
- 使用thymeleaf在页面直接获取当前时间并格式化输出
- VC++获取当前系统时间日期 GetSystemTime()函数的用法
- VC++获取当前系统时间日期 GetSystemTime()函数的用法
- MySQL获取系统当前时间的函数
- Android初学之使用IntentService或Service获取当前时间
- C或C++调用Windows系统函数 实现延时 或 获取当前时间的处理
- 代码空间项目 -- 获取当前时间之前的某一天-Calender类的使用
- Windows获取时间函数(使用GetLocalTime,GetSystemTime,SystemTimeToTzSpecificLocalTime,GetFileTime API函数
- 使用脚本得到当前的时间函数
- C# 使用定时任务 之 获取当前时间及格式化串
- 编写一个程序,使用getpid函数来获取当前进程的进程ID