time_t到struct tm的转换:

#include <time.h>
struct tm *localtime(const time_t *timep);

struct tm到time_t的转换:

#include <time.h>
time_t mktime(struct tm *tm);

time_t timep = time(NULL);能够获得从此刻距1970-01-01 00:00:00 +0000 (UTC)时间点的秒数。

演示样例程序;

#include <stdio.h>
#include <stdlib.h>
#include <time.h> int main (int argc, char **argv)
{
int i;
time_t timer[4];
struct tm tm, *p; tm.tm_year = 2015;
tm.tm_mon = 2;
tm.tm_mday = 23;
tm.tm_hour = 8;
tm.tm_min = 22;
tm.tm_sec = 0;
for(i = 0; i < 4; i++){
timer[i] = mktime(&tm);
printf ("timer[%d] = %ld\n", i, timer[i]);
p = localtime(&timer[i]);
printf("%d-%d-%d-%d-%d-%d\n",
p->tm_year,
p->tm_mon,
p->tm_mday,
p->tm_hour,
p->tm_min,
p->tm_sec);
} return 0;
} /* ----- End of main() ----- */

by fulinux <fulinux@sina.com>

blog: blog.csdn.net/fulinus

time_t和struct tm之间的转换的更多相关文章

  1. 【转】C/C++中的日期和时间 TIME_T与STRUCT TM转换——2013-08-25 16

    http://www.cnblogs.com/Wiseman/archive/2005/10/24/260576.html 摘要: 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的 ...

  2. C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换

    使用gmtime函数或localtime函数将time_t类型的时间日期转换为structtm类型: 使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年.月.日 ...

  3. struct tm 和 time_t 时间和日期的使用方法(转

    关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) .概念 在C/C++中,对字符串的操作有很多值得注意的问题,同样,C ...

  4. 时间操作(struct tm、time_t)求指定日期 前n天的日期

    1.在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值 ...

  5. SYSTEMTIME 与 time_t 之间的转换,计算2个SYSTEMTIME的时间差

    time_t systemtime_to_time_t(const SYSTEMTIME& st) { struct tm gm = {st.wSecond, st.wMinute, st.w ...

  6. FlyCapture2 fc2Image OpenCV IplImage Conversion 两种图像格式之间的转换

    fc2Image是FlyCapture SDK的C语言库中的图片格式,由于在Windows上的MinGW无法编译FlyCapture2的C++库,只能使用C语言库,所以当我们在同时使用OpenCV的图 ...

  7. CString-int-string-char-BSTR之间的转换

    一.CString, int, string, char*之间的转换 string 转 CString CString.Format("%s", string.c_str());c ...

  8. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写

    #include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...

  9. C++四种类型之间的转换

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a. C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cas ...

随机推荐

  1. JniHelper 含安卓推送

    using System; using System.Runtime.CompilerServices; using UnityEngine; internal static class JniHel ...

  2. 【无聊放个模板系列】HDU 3068 MANACHER

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  3. Memcached总结一:memcached简介及适用和不适应场景

    Memcached是免费的,开源的,高性能的,分布式内存对象的缓存系统(键/值字典),旨在通过减轻数据库负载加快动态Web应用程序的使用. Memcached是由布拉德·菲茨帕特里克(Brad Fit ...

  4. 排序算法-冒泡排序(Bubble Sort)

    package com.wangzhu.sort; /** * 冒泡排序算法 * @ClassName: BubbleSort * @Description: TODO * @author wangz ...

  5. eCos驱动分析 之 ISR是如何与硬件中断联系起来的?

    http://keendawn.blog.163.com/blog/static/8888074320116205833478/

  6. jacob访问ocx控件方法和遇到的问题

    最近在进行摄像机的二次开发,摄像机厂商提供了使用C++开发的ocx控件:所以尝试使用jacob来进行访问. 操作步骤如下: 1, 从官网(http://sourceforge.net/projects ...

  7. SQLite入门与分析(七)---浅谈SQLite的虚拟机

    写在前面:虚拟机技术在现在是一个非常热的技术,它的历史也很悠久.最早的虚拟机可追溯到IBM的VM/370,到上个世纪90年代,在计算机程序设计语言领域又出现一件革命性的事情——Java语言的出现,它与 ...

  8. ANDROID_MARS学习笔记_S02_009_Animation_Interpolator

    public class MainActivity extends Activity { private Button button = null; private ImageView imageVi ...

  9. 测试ODBC与OLE

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data. ...

  10. Visual Studio Solution Configuration

    https://msdn.microsoft.com/en-us/library/bb166577.aspx Solution configurations store solution-level ...