time()函数返回自1970年1月1日0点以来经过的秒数,每秒变化一次?

time()函数定义在头文件<time.h>中,原型是:

time_t time(time_t *arg);

如果arg不是空指针,那么函数返回time_t类型的calendar time,并且把结果保存在arg指向的对象;
如果arg == NULL,那么函数只是返回一个值,值不能存储在空指针指向的对象。

之前不明白为什么要设计一个参数arg,直接返回一个值就好了啊?

有大神说,这是因为:

很久很久以前,据说time_t是个struct,那时候c语言不支持函数返回struct,所以只能用指针传进去。

那么time_t到底是什么类型呢?

看看cppreference.com的定义:

The encoding of calendar time in time_t is unspecified, but most systems conform to POSIX specification and return a value of integral type holding the number of seconds since the Epoch.Implementations in which time_t is a 32-bit signed interger(many historical implementations) fail in the year 2038.

就是说:C标准委员会并没有定义time_t的精度,也没有指定标准的Epoch,所以这取决于你的operating system以及你的compiler。

如果你的系统支持POSIX标准(包括很多类Unix系统、Windows系统),那么time_t是一个signed int 32,最大表示范围是2147483647秒,标准Epoch是1970年1月1日0点,所以最终时间就是2038年1月19日,这就是著名的2038年问题

那么这个函数的实现,GNU C Library是这么写的:

#include <stddef.h>                /* For NULL.  */
#include <time.h>
#include <sys/time.h>
/* Return the current time as a `time_t' and also put it in *T if T is
not NULL. Time is represented as seconds from Jan 1 00:00:00 1970. */
time_t
time (time_t *t)
{
struct timeval tv;
time_t result;
if (__gettimeofday (&tv, (struct timezone *) NULL))
result = (time_t) -;
else
result = (time_t) tv.tv_sec;
if (t != NULL)
*t = result;
return result;
}
libc_hidden_def (time)

如果返回time_t类型的值,说明调用成功;
如果返回(time_t)(-1),说明无法取得现在的时间,调用失败。

举个栗子,获得当前时间:

#include <stdio.h>
#include <time.h>
#include <stdlib.h> int main(void)
{
time_t current = time(NULL);
char* string; /*把日期和时间转为字符串*/
string = ctime(&current);
if (current == (time_t)-)
{
printf("Fail to get the current time!\n");
} printf("The current time is %s", string);
printf("(%d seconds since the Epoch)\n",current);
}

运行结果:

Function-time()的更多相关文章

  1. 通过百度echarts实现数据图表展示功能

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

  2. jsp中出现onclick函数提示Cannot return from outside a function or method

    在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...

  3. JavaScript function函数种类

    本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通函数:介绍普通函数的特性:同名覆盖.arguments对象.默认返回值等. 2. 匿名函数:介绍匿名函数的特性:变量匿名函数.无名称匿名函数. ...

  4. 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()

    1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...

  5. jquery中的$(document).ready(function() {});

    当文档载入时执行function函数里的代码, 这部分代码主要声明,页面加载后 "监听事件" 的方法.例如: $(document).ready( $("a") ...

  6. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  7. 转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38

    转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38http://space.itpub. ...

  8. [Xamarin] 透過Native Code呼叫 JavaScript function (转帖)

    今天我們來聊聊關於如何使用WebView 中的Javascript 來呼叫 Native Code 的部分 首先,你得先來看看這篇[Xamarin] 使用Webview 來做APP因為這篇文章至少講解 ...

  9. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  10. javascript中的Function和Object

    写的很好,理解了很多,特此转发记录 转自:http://blog.csdn.net/tom_221x/archive/2010/02/22/5316675.aspx 在JavaScript中所有的对象 ...

随机推荐

  1. Loop Unrolling 循环展开

    在csapp第五章5.2中提到了循环展开(loop unrolling).这里展开一下为什么循环展开可以提升程序的效率. 以书中计算数组和的两段代码为例: 1.未展开: void psum1(floa ...

  2. 史上最详细mac安装Qt教程

    史上最详细mac安装Qt教程,小白看过来! 这是一篇非常适合Qt入门小白的的安装Qt教程,因为这学期我们小组的一个关于高速救援的项目要用到Qt与web进行交互式展现相关的图像,由于没有MSVC这个插件 ...

  3. template_共享模板

    方法: 定义一个基本框架html文件  举例:定义{标题.内容.页尾}区块   定义相应的html文件实现区块的具体样式或内容   定义具体静态网页html文件时调用这些区块html文件, 实现公共元 ...

  4. scala_spark实践4

    SparkStreaming中foreachRDD SparkStreaming是流式实时处理数据,就是将数据流按照定义的时间进行分割(就是“批处理”).每一个时间段内处理的都是一个RDD.而Spar ...

  5. web.xml中通过contextConfigLocation的读取spring的配置文件

    web.xml中通过contextConfigLocation的读取spring的配置文件 博客分类: web.xml contextConfigLocationcontextparamxmlvalu ...

  6. iOS线程数量监控工具

    简单却强大的线程监控工具 KKThreadMonitor :当线程过多或瞬间创建大量子线程(线程爆炸),控制台就打印出所有的线程堆栈.便于分析造成子线程过多或线程爆炸的原因. /******* 线程爆 ...

  7. L4文本预处理

    文本预处理 timemachine.txt数据下载地址 链接:https://pan.baidu.com/s/1RO2OLyTRQZ90HJUW7V7BCQ 提取码:bjox NLTK数据集下载 链接 ...

  8. Daily Scrum 12/24/2015

    Process: Zhaoyang: Some UI change and compile the Caffe in the IOS. Yandong: Do some code integratio ...

  9. 【arithmetic】搜索插入位置

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置 可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输出: ...

  10. vue中解决时间在ios上显示NAN的问题

    最近在用vue,遇到倒计时在ios上显示为NAN的问题. 因为做的是倒计时支付,思路是获取服务器时间和下单时间,再转成秒级时间戳做差值. 在网上找到说是ios 不支持例如2018-09-01 10:0 ...