Function-time()
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_tis 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 whichtime_tis 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(¤t);
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()的更多相关文章
- 通过百度echarts实现数据图表展示功能
现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...
- jsp中出现onclick函数提示Cannot return from outside a function or method
在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...
- JavaScript function函数种类
本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通函数:介绍普通函数的特性:同名覆盖.arguments对象.默认返回值等. 2. 匿名函数:介绍匿名函数的特性:变量匿名函数.无名称匿名函数. ...
- 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()
1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...
- jquery中的$(document).ready(function() {});
当文档载入时执行function函数里的代码, 这部分代码主要声明,页面加载后 "监听事件" 的方法.例如: $(document).ready( $("a") ...
- Function.prototype.toString 的使用技巧
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...
- 转: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. ...
- [Xamarin] 透過Native Code呼叫 JavaScript function (转帖)
今天我們來聊聊關於如何使用WebView 中的Javascript 來呼叫 Native Code 的部分 首先,你得先來看看這篇[Xamarin] 使用Webview 來做APP因為這篇文章至少講解 ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- javascript中的Function和Object
写的很好,理解了很多,特此转发记录 转自:http://blog.csdn.net/tom_221x/archive/2010/02/22/5316675.aspx 在JavaScript中所有的对象 ...
随机推荐
- Java中toString()与new String()
Java中toString()与new String(): 1.错误演示: FileInputStream fileIn = new FileInputStream(filePath);byte[] ...
- 鬼吹灯之龙岭迷窟百度云迅雷BT在线观看免费全集
看视频搜索微信公众号:qyw1091 还记得去年5月11日在高家堡举行的<鬼吹灯之龙岭迷窟>开机仪式吗?时隔数月,这部网剧于4月1日将在腾讯视频全网独播了! . <鬼吹灯之龙岭迷窟& ...
- python 爬虫之 urllib库
文章更新于:2020-03-02 注:代码来自老师授课用样例. 一.初识 urllib 库 在 python2.x 版本,urllib 与urllib2 是两个库,在 python3.x 版本,二者合 ...
- d3.js v4曲线图的拖拽功能实现Zoom
zoom缩放案例 源码:https://github.com/HK-Kevin/d...:demo:https://hk-kevin.github.io/d3...: 原理:通过zoom事件来重新绘制 ...
- O - Employment Planning HDU - 1158
题目大意: 第一行一个n,表示共n个月份,然后第二行分别表示一个工人的聘请工资,月薪水,解雇工资.第三行是n个月每个月需要的工人的最少数目.然后求最少花费 题解: dp[i][j] 表示第i个月聘请j ...
- 功能测试--聊天功能测试&微信聊天
微信聊天功能测试 发送对象 普通用户.公众号.群.其他特殊主体 衍生功能 转发.语音转文字.删除等 消息发送 单聊.群聊.语音.文字.图片.表情.链接.字符及长度 消息管理 发布通知.接受通知.发文件 ...
- gitbook命令
安装gitbook命令 前提:已经安装nodejs npm install -g gitbook-cli 查看版本号 gitbook -V gitbook命令 gitbook -h Usage: gi ...
- BUUOJ [极客大挑战 2019]Secret File
[极客大挑战 2019]Secret File 0X01考点 php的file伪协议读取文件 ?file=php://filter/convert.base64-encode/resource= 0X ...
- GIT生成ssh(window7系统)——git工具篇
由于本地git仓库和github仓库直接的传递需要ssh加密的,所以必须要生成ssh,下面是具体的操作步骤: 1.打开GIT命令行,输入命令:ssh-keygen -t rsa -C "yo ...
- js中string转map的方法
例如: var r = "{'msg':'你好'}" ; var map = eval("("+r+")"); //r为String类型的数 ...