常见字符串时间处理

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 日至今的秒数。

  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;  
    }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值