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


    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 }
  • 相关阅读:
    素数
    超级素数
    SUMMARIZE 6.1
    广度优先搜索与八字码问题
    poj2352
    poj1198
    康托展开
    STL里的内存池实现
    构造函数,C++内存管理,内存泄漏定位
    内联函数,宏定义,内存对齐,类型转换
  • 原文地址:https://www.cnblogs.com/felove2013/p/4708959.html
Copyright © 2020-2023  润新知