linux调用一个函数失败 打印错误,linux下settimeofday函数调用失败,何故?解决办法...
当前位置:我的异常网» C语言 » linux下settimeofday函数调用失败,何故?解决办法
linux下settimeofday函数调用失败,何故?解决办法
www.myexceptions.net 网友分享于:2014-11-29 浏览:0次
linux下settimeofday函数调用失败,何故?
GCC成功,可以运行,但就是该函数调用失败,返回值-1;
我用ROOT帐号GCC,再执行,也是这样。
--------------------------------------------------------------
#include
#include
#include
#include
#include "stdlib.h"
int main(void)
{
struct tm *t_tm;
struct timeval t_timeval;
time_t t_timet;
t_timet=time(NULL);
t_tm=localtime(&t_timet);
printf("\n\ncurrent system UTC time:%s\n", asctime(t_tm));
t_tm->tm_hour=18;
t_tm->tm_min=18;
t_tm->tm_sec=18;
t_tm->tm_year=18;
t_tm->tm_mon=5; //月份(从一月开始,0代表一月) - 取值区间为[0,11]
t_tm->tm_mday=18; //年份,其值从1900开始
t_timet=mktime(t_tm);
t_timeval.tv_sec=t_timet;
t_timeval.tv_usec=0;
int rec = settimeofday(&t_timeval,NULL);
printf("modifyed time:%s\nsettime return code:%d\n\n", asctime(t_tm),rec);
//printf("local time:%s\n", ctime(&tvt));
return 0;
}
------解决思路----------------------
检查下errno。
------解决思路----------------------
单步调试看看 t_timet和rec的值。
------解决思路----------------------
GETTIMEOFDAY
Section: Linux Programmer's Manual (2)
Updated: 2004-05-27
--------------------------------------------------------------------------------
NAME
gettimeofday, settimeofday - get / set time
SYNOPSIS
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
DESCRIPTION
The functions gettimeofday and settimeofday can get and set the time as well as a timezone. The tv argument is a timeval struct, as specified in :
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
and gives the number of seconds and microseconds since the Epoch (see time(2)). The tz argument is a timezone :
struct timezone {
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
The use of the timezone struct is obsolete; the tz_dsttime field has never been used under Linux - it has not been and will not be supported by libc or glibc. Each and every occurrence of this field in the kernel source (other than the declaration) is a bug. Thus, the following is purely of historic interest.
The field tz_dsttime contains a symbolic constant (values are given below) that indicates in which part of the year Daylight Saving Time is in force. (Note: its value is constant throughout the year - it does not indicate that DST is in force, it just selects an algorithm.) The daylight saving time algorithms defined are as follows :
DST_NONE /* not on dst */
DST_USA /* USA style dst */
DST_AUST /* Australian style dst */
DST_WET /* Western European dst */
DST_MET /* Middle European dst */
DST_EET /* Eastern European dst */
DST_CAN /* Canada */
DST_GB /* Great Britain and Eire */
DST_RUM /* Rumania */
DST_TUR /* Turkey */
DST_AUSTALT /* Australian style with shift in 1986 */
Of course it turned out that the period in which Daylight Saving Time is in force cannot be given by a simple algorithm, one per country; indeed, this period is determined by unpredictable political decisions. So this method of representing time zones has been abandoned. Under Linux, in a call to settimeofday the tz_dsttime field should be zero.
Under Linux there is some peculiar `warp clock' semantics associated to the settimeofday system call if on the very first call (after booting) that has a non-NULL tz argument, the tv argument is NULL and the tz_minuteswest field is nonzero. In such a case it is assumed that the CMOS clock is on local time, and that it has to be incremented by this amount to get UTC system time. No doubt it is a bad idea to use this feature.
The following macros are defined to operate on a struct timeval :
#define timerisset(tvp)\
((tvp)->tv_sec
------解决思路----------------------
(tvp)->tv_usec)
#define timercmp(tvp, uvp, cmp)\
((tvp)->tv_sec cmp (uvp)->tv_sec
------解决思路----------------------
\
(tvp)->tv_sec == (uvp)->tv_sec &&\
(tvp)->tv_usec cmp (uvp)->tv_usec)
#define timerclear(tvp)\
((tvp)->tv_sec = (tvp)->tv_usec = 0)
If either tv or tz is null, the corresponding structure is not set or returned.
Only the super user may use settimeofday.
RETURN VALUE
gettimeofday and settimeofday return 0 for success, or -1 for failure (in which case errno is set appropriately).
ERRORS
EFAULT
One of tv or tz pointed outside the accessible address space.
EINVAL
Timezone (or something else) is invalid.
EPERM
The calling process has insufficient privilege to call settimeofday; under Linux the CAP_SYS_TIME capability is required.
NOTE
The prototype for settimeofday and the defines for timercmp, timerisset, timerclear, timeradd, timersub are (since glibc2.2.2) only available if _BSD_SOURCE is defined (either explicitly, or implicitly, by not defining _POSIX_SOURCE or compiling with the -ansi flag).
Traditionally, the fields of struct timeval were longs.
CONFORMING TO
SVr4, BSD 4.3. POSIX 1003.1-2001 describes gettimeofday() but not settimeofday().
SEE ALSO
date(1), adjtimex(2), time(2), ctime(3), ftime(3), capabilities(7)
--------------------------------------------------------------------------------
文章评论
linux调用一个函数失败 打印错误,linux下settimeofday函数调用失败,何故?解决办法...相关推荐
- linux调用一个函数失败 打印错误,linux系统调用出错时的处理函数
在进行linux的系统调用, 要判断调用的成功与否, 调用失败的情况下就要进行一定的处理,除了打印出消息, 还可以打印系统调用的出错信息, 一般性的错误不必退出程序, 要是致命性的错误就终止整个程序 ...
- linux调用堆栈函数,使用 backtrace 获得 Linux 函数调用栈
一.源码 使用 backtrace 获得 Linux 函数调用栈 源码来自:man backtrace #include #include #include #include void myfunc3 ...
- 【C语言】函数:实现一个函数,打印乘法口诀表
文章目录 1.条件概述 2.代码实现 1.条件概述 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出99口诀表,输入12,输出1212的乘法口诀表. 2.代码实现 //实现一个函 ...
- Linux下数据库连接超时时长,关于.Net Core 部署在Linux下连接SqlServer数据库超时解决办法...
.Net Core 在 Linux 下连接 SqlServer 需要 SqlServer2008 SP3或以上版本,或SqlServer2012,或SqlServer2014. 如果SqlServer ...
- (笔记)Linux Root下的.gvfs出现异常解决办法
(笔记)Linux Root下的.gvfs出现异常解决办法 参考文章: (1)(笔记)Linux Root下的.gvfs出现异常解决办法 (2)https://www.cnblogs.com/tdyi ...
- 百度站长HTML添加301错误,搜索资源平台(百度站长)添加网站 使用文件验证 验证失败,原因:未知原因:301的解决办法...
搜索资源平台(百度站长)添加网站 使用文件验证 验证失败,原因:未知原因:301的解决办法 之前使用过百度站长后台添加新的网站时,从没遇到过问题,一般都是选择第一种验证方式(文件验证),文件验证应该是 ...
- mysql数据库连接过多的错误,可能的原因分析及解决办法
mysql数据库连接过多的错误,可能的原因分析及解决办法 来源:网络采集 作者:未知 系统不能连接数据库,关键要看两个数据: 1.数据库系统允许的最大可连接数max_connections.这个参数是 ...
- oracle11g ora 29927,listagg函数 ORA-01489 result of string concatenation is too long的解决办法 【博森瑞】...
listagg函数 ORA-01489: result of string concatenation is too long的解决办法 概述 listagg 函数是Oracle 11g推出的一个分组 ...
- VB6程序运行错误Run-time error 339: Tabctl32.ocx的解决办法
运行错误Run-time error 339: Tabctl32.ocx的解决办法 [问题] 最近一段时间与教研室的各位老师在做一个山东省一级OFFICE模拟软件时,前面的开发工作一切顺利,却在后面的 ...
最新文章
- CPU 的一些基本知识总结
- emeditor利用书签功能导出匹配结果到新文件
- rhel6系统中,mysql 5.6复制新特性下主从复制配置[基于GTID]
- 使用ADF绑定创建视图对象行CreateInsert操作
- OnLongClickListener长按事件设置墙纸
- Windows Azure Cloud Service (41) 修改云服务IIS托管管道模式为4.0经典模式
- (20)FPGA多路选择器设计(第4天)
- linux可配置哪些服务,不可不知 十大热门Linux服务器配置
- 迅雷不及掩耳 山寨版iPhone 5令人瞠目
- 《吴忠与富平》之四:汉三水属国(北地属国、安定属国)
- Python词云库wordcloud 显示中文 !!!
- 访问samba服务器提示无权限使用网络资源
- 支付宝转账支付宝转卡(H5飞行模式)
- 微博视频号搬砖项目,单号月入1000+!
- html——form表单提交方法submit和button
- QQ浏览器网页版微信抓包和IPAD微信抓包 Wireshark
- 优秀课程案例|如何用scratch画扇形统计图
- 使用burp-suite对投票系统进行攻击
- vue下的@change事件
- sql和python还有c语言_TIOBE 4 月排行榜:SQL 进入前十,Python 继续攀升