zoukankan      html  css  js  c++  java
  • c 时间转移函数

                  /*
                     @param       date
                     @param       formart  of date
                     @return      time_t
                     @author      yangqijun@outlook.com
                   */
                  time_t strtotime(char* const date,char* const format="%Y%m%d%H%M%S")
                  {
                          struct tm tm;
                          strptime(date,format, &tm) ;
                          time_t ft=mktime(&tm);
                          return ft;
                  }



    string   timetodate(time_t const timer)
                  {
                          struct tm *l=localtime(&timer);

                          char buf[128];
                          snprintf(buf,sizeof(buf),"%04d-%02d-%02d %02d:%02d:%02d",l->tm_year+1900,l->tm_mon+1,l->tm_mday,l->tm_hour,l->tm_min,l->tm_sec);
                          string s(buf);
                          return s;


                  }
    //该代码片段来自于: http://www.sharejs.com/codes/cpp/5675

    #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 ", 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;
    }

  • 相关阅读:
    SpringCloud组件
    Lambda遍历Map集合
    转换为base64格式
    控制器注解参数判空
    SpringBoot项目使用环境变量当做端口号
    使用控制台占位符输出日志, 使用占位符Plus
    Java 传无限参数
    将yyyy-MM-dd hh:mm:ss转换为时间戳
    HTML使用svg,定义.svg格式
    演示js异步,同步请求,响应解码
  • 原文地址:https://www.cnblogs.com/i80386/p/5168997.html
Copyright © 2011-2022 走看看