mysql中:字符串时间和unix时间互相转换
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()))
c语言中:
#include
<
time.h
>
char * asctime ( const struct tm * timeptr ) ;
将结构中的信息转换为真实世界的时间,以字符串的形式显示
char * ctime ( const time_t * timep ) ;
将 timep 转换为真是世界的时间,以字符串显示,它和 asctime 不同就在于传入的参数形式不一样
double difftime ( time_t time1 , time_t time2 ) ;
返回两个时间相差的秒数
int gettimeofday ( struct timeval * tv , struct timezone * tz ) ;
返回当前距离 1970 年的秒数和微妙数,后面的 tz 是时区,一般不用
struct tm * gmtime ( const time_t * timep ) ;
将 time_t 表示的时间转换为没有经过时区转换的 UTC 时间,是一个 struct tm 结构指针
stuct tm * localtime ( const time_t * timep ) ;
和 gmtime 类似,但是它是经过时区转换的时间。
time_t mktime ( struct tm * timeptr ) ;
将 struct tm 结构的时间转换为从 1970 年至今的秒数
time_t time ( time_t * t ) ;
取得从 1970 年 1 月 1 日至今的秒数。
char * asctime ( const struct tm * timeptr ) ;
将结构中的信息转换为真实世界的时间,以字符串的形式显示
char * ctime ( const time_t * timep ) ;
将 timep 转换为真是世界的时间,以字符串显示,它和 asctime 不同就在于传入的参数形式不一样
double difftime ( time_t time1 , time_t time2 ) ;
返回两个时间相差的秒数
int gettimeofday ( struct timeval * tv , struct timezone * tz ) ;
返回当前距离 1970 年的秒数和微妙数,后面的 tz 是时区,一般不用
struct tm * gmtime ( const time_t * timep ) ;
将 time_t 表示的时间转换为没有经过时区转换的 UTC 时间,是一个 struct tm 结构指针
stuct tm * localtime ( const time_t * timep ) ;
和 gmtime 类似,但是它是经过时区转换的时间。
time_t mktime ( struct tm * timeptr ) ;
将 struct tm 结构的时间转换为从 1970 年至今的秒数
time_t time ( time_t * t ) ;
取得从 1970 年 1 月 1 日至今的秒数。
1 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
time_t time_s;
time(&time_s);
//printf("\n-time=%ld---\n",time_s);
struct tm *nowtime,time_num;
nowtime = localtime(&time_s);
// printf("\n--time=%s--\n",asctime(nowtime));
char * time_s_t = "2015-12-11 16:09:40";
strptime(time_s_t,"%Y-%m-%d %H:%M:%S",&time_num);
int unix_t = mktime(&time_num);
printf("\n-%d-\n",unix_t);
return 0;
}
----------------------------------------------------我是分割线-------------------------------------------------------
#include <stdio.h>
#include <time.h>
int strtotime(char datetime[])
{
struct tm tm_time;
int unixtime;
strptime(datetime, "%Y-%m-%d %H:%M:%S", &tm_time);
unixtime = mktime(&tm_time);
return unixtime;
}
另附上几个时间相关的函数,做个笔记:
//当前时间
char* get_curr_time()
{
char* strtime = (char *)malloc(sizeof(char)*20);
memset(strtime, 0, sizeof(char)*20);
time_t now;
time(&now);
strftime(strtime, 20, "%Y-%m-%d %H:%M:%S", gmtime(&now));
printf("Info: current time %s\n", strtime);
return strtime;
}
//当前时间的unix时间戳
int get_curr_unixtime(void)
{
time_t now;
int unixtime = time(&now);
return unixtime;
}
//字符转unix时间戳
int strtotime(char datetime[])
{
struct tm tm_time;
int unixtime;
strptime(datetime, “%Y-%m-%d %H:%M:%S”, &tm_time);
unixtime = mktime(&tm_time);
return unixtime;
}