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. 原生js,jquery通过ajax获得后台json数据动态新增页面元素

    一.原生js通过ajax获取json数据 因为IE浏览器对ajax对象的创建和其他浏览器不同,为了兼容全部浏览器,我用下面的代码: function createXMLHttpRequest(){ t ...

  2. Python Requests-学习笔记(4)-定制请求头和POST

    定制请求头 如果你想为请求添加HTTP头部,只要简单地传递一个 dict 给 headers 参数就可以了. import jsonurl = 'https://api.github.com/some ...

  3. logback日志实战

    <?xml version="1.0" encoding="UTF-8" ?> <!-- <configuration> < ...

  4. web网页html基础

    列表标签 ul:定义一个列表 li:定义列表中的条目 图片标签 Img:用来向网页中插入一张图片 Src:指定图片所在的路径 Width:指定图片宽度 (% px) Height:指定图片高度 (% ...

  5. AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮

    AJ分享,必须精品 一:效果图 自定义tabBar实现最下面中间的添加按钮 二:思路 首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这 ...

  6. svg整体缩放至指定大小

    一.问题 svg画面跑在分辨率低的电脑上,导致不能完全显示. 二.要求 svg要能够根据电脑的屏幕大小自动缩放至适配电脑的尺寸. 三.实现 1.获取本机窗口高度.宽度 let clientWidth ...

  7. Daily Scrum 1/6/2015

    Process: Zhaoyang: Complete the speech API test and do some UI upgrade. Yandong: Help zhaoyang to do ...

  8. E - Roaming Atcoder

    题解:https://blog.csdn.net/qq_40655981/article/details/104459253 题目大意:n个房间,,每个房间都有一个人,一共k天,在一天,一个人可以到任 ...

  9. K - Two Contests

    题目连接:https://atcoder.jp/contests/agc040/tasks/agc040_b 大佬题解:https://blog.csdn.net/duanghaha/article/ ...

  10. 详解 Hashtable

    至于HashTable,本人只想说,除了它们各自的特点是截然相反外,其余性质 以及 用法和HashMap的性质几乎一样, (有关Map集合的基本性质,请观看本人博文-- <详解 Map集合> ...