• 格式化时间 与 时间戳的转换


    2015-08-06 19:37:58

     1 /*!
     2  * <格式化时间与时间戳的转换>
     3  * 
     4  * 2015/08/06 by <felove>
     5  */
     6 #include <stdio.h>
     7 #include <windows.h>
     8 #include <time.h>
     9 
    10 void timestampToFormat(time_t& t_1, tm& tm_1);
    11 time_t formatToTimestamp(int _nYear, int _nMonth, int _nDay, int _nHour, int _nMinute, int _nSecond);
    12 
    13 int main()
    14 {
    15     //时间戳转格式化时间
    16     time_t timep;
    17     time(&timep);
    18     struct tm tm_1;
    19     timestampToFormat(timep,tm_1);
    20     printf("time():%d
    ",timep);
    21     printf("[%04d-%02d-%02d][%02d:%02d:%02d]
    ",tm_1.tm_year + 1900,tm_1.tm_mon + 1,tm_1.tm_mday,tm_1.tm_hour,tm_1.tm_min,tm_1.tm_sec);
    22 
    23     //格式化时间转时间戳
    24     time_t timep1 = formatToTimestamp(2015,8,6,18,6,0);
    25     printf("time()->localtime()->mktime():%d
    ",timep1);
    26 
    27     system("pause");
    28     return 0;
    29 }
    30 
    31 void timestampToFormat(time_t& t_1, tm& tm_1)
    32 {
    33     localtime_s(&tm_1,&t_1);
    34 }
    35 
    36 time_t formatToTimestamp(int _nYear, int _nMonth, int _nDay, int _nHour, int _nMinute, int _nSecond)
    37 {
    38     struct tm tm_1;
    39     tm_1.tm_year = _nYear - 1900;
    40     tm_1.tm_mon = _nMonth - 1;
    41     tm_1.tm_mday = _nDay;
    42     tm_1.tm_hour = _nHour;
    43     tm_1.tm_min  = _nMinute;
    44     tm_1.tm_sec = _nSecond;
    45     tm_1.tm_isdst = -1;
    46     time_t timep1 = mktime(&tm_1);
    47     return timep1;
    48 }
  • 相关阅读:
    Dobbo介绍
    Java反射
    Rocket MQ简介
    logback异步日志
    Java Arrays.asList
    图解Javascript——作用域、作用域链、闭包
    android源码-安卓源码-Android源码下载-安卓游戏源码
    Ghost版Win8.1系统企业版下载
    3dmax详细讲解全套攻略在线视频教程
    网站分析师如何让分析报告更具价值
  • 原文地址:https://www.cnblogs.com/felove2013/p/4708959.html
Copyright © 2020-2023  润新知