c语言时间函数状态,C语言的时间函数

下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:

This page is part of release 3.35 of the Linux man-pages project.

#include

char *asctime(const struct tm *tm);

char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);

char *ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);

struct tm *gmtime_r(const time_t *timep, struct tm *result);

struct tm *localtime(const time_t *timep);

struct tm *localtime_r(const time_t *timep, struct tm *result);

time_t mktime(struct tm *tm);

Broken-down time is stored in the structure tm which is defined in

as follows:

struct tm {

int tm_sec; /* seconds */

int tm_min; /* minutes */

int tm_hour; /* hours */

int tm_mday; /* day of the month */

int tm_mon; /* month */

int tm_year; /* year */

int tm_wday; /* day of the week */

int tm_yday; /* day in the year */

int tm_isdst; /* daylight saving time */

};

The members of the tm structure are:

tm_sec The number of seconds after the minute, normally in the range

0 to 59, but can be up to 60 to allow for leap seconds.

tm_min The number of minutes after the hour, in the range 0 to 59.

tm_hour The number of hours past midnight, in the range 0 to 23.

tm_mday The day of the month, in the range 1 to 31.

tm_mon The number of months since January, in the range 0 to 11.

tm_year The number of years since 1900.

tm_wday The number of days since Sunday, in the range 0 to 6.

tm_yday The number of days since January 1, in the range 0 to 365.

tm_isdst A flag that indicates whether daylight saving time is in

effect at the time described. The value is positive if day‐

light saving time is in effect, zero if it is not, and nega‐

tive if the information is not available.

下面是一个程序,输入某个日期(年份不要太久远,否则超出time_t的范围),输出哪个日期是星期几,还有下个星期一是哪天:

#include

#include

#include

using namespace std;

struct Date{

int day;

int month;

int year;

};

Date& add(Date& date,int day,int month,int year){

//这个函数什么也没考虑,仅仅为了测试

date.day+=day;

date.month+=month;

date.year+=year;

return date;

}

int tellWeekday(int mon,int day,int year){

struct tm ct;

//bzero(&ct,sizeof(ct));

memset(&ct,,sizeof(ct));//take care of pointer

ct.tm_year=year-;//maybe ct->tm_year is read only if ct is pointer

ct.tm_mon=mon-;

ct.tm_mday=day;

//ct.tm_sec=ct->tm_min=ct->tm_hour=ct->tm_wday=ct->tm_yday=ct->tm_isdst=0;

time_t ctt=mktime(&ct);

if(ctt==-) return -;

struct tm* ans=localtime(&ctt);

cout<tm_wday<

}

int nextMondayDate(int mon,int day,int year){

struct tm ct;

int delta;

//bzero(&ct,sizeof(ct));

memset(&ct,,sizeof(ct));

ct.tm_year=year-;

ct.tm_mon=mon-;

ct.tm_mday=day;

time_t ctt=mktime(&ct);

if(ctt==-) return -;

struct tm* tm2=localtime(&ctt);

if(tm2->tm_wday==) delta=;

else delta=-tm2->tm_wday;

ctt+=delta**;

tm2=localtime(&ctt);

cout<tm_mon+<tm_mday<tm_year+<

}

int main(){

Date today,otherDay;

int mm,dd,yy;

time_t rawTime,inTime;

struct tm* timeInfo;

time(&rawTime);//get secs from 1970.1.1,save to rawTime

timeInfo=localtime(&rawTime);

cout<

today.year=timeInfo->tm_year+;

today.month=timeInfo->tm_mon+;//starts from 0

today.day=timeInfo->tm_mday;

otherDay=add(today,,,);

cout<

<

<

cout<

cin>>mm>>dd>>yy;

tellWeekday(mm,dd,yy);

cout<

cin>>mm>>dd>>yy;

nextMondayDate(mm,dd,yy);

system("pause");

return ;

}

常用C语言time时间函数

常见的时间函数有time( ).ctime( ).gmtime( ).localtime( ).mktime( ).asctime( ).difftime( ).gettimeofday( ).set ...

c语言随机函数&amp&semi;&amp&semi;时间函数

c语言中的随机函数为rand(),但是rand生成的值得大小主要相对一个变量才产生的一定有含义的数,这个相对的变量我们可以再srand()函数中进行设置,srand函数是void类型,内部含一个无符号 ...

R语言日期时间函数

Sys.Date( ) returns today's date. date() returns the current date and time.# print today's datetoday ...

C语言 常用的时间函数

//时间函数的使用 #define _CRT_SECURE_NO_WARNINGS #include #include #include& ...

Loadrunner时间函数、用时间生成订单编号例子

Loadrunner中取时间函数.用时间函数生成订单编号例子: 问题的提出: (1)有时候在Loadrunner中用C语言设计脚本 ...

Oracle日期时间函数大全

ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...

Linux时间函数之gettimeofday&lpar;&rpar;函数之使用方法

1.简介: 在C语言中可以使用函数gettimeofday()函数来得到时间.它的精度可以达到微妙 2.函数原型: #include int gettimeofda ...

php时间函数整理

PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

随机推荐

Hibernate组件映射

Hibernate联合主键映射以及组件映射 在Hibernate中联合主键的形成有两种可能:一种是由多对多映射形成的,多对多映射会形成第三张表,一般来说第三张表的主键是由其他两张表的主键构成的(比如学 ...

【Alpha】Daily Scrum Meeting第五次

一.本次Daily Scrum Meeting主要内容 每个人学习情况 任务安排 界面设计问题,怎样让界面更好看? 二.任务安排 学号尾数 昨天做的任务 今天的任务 明天的任务 612 时间轴控件优化 ...

Excel&colon; Switch &lpar;transpose&rpar; columns and rows

链接:https://support.office.com/en-in/article/Switch-transpose-columns-and-rows-ed1215f5-59af-47e6-953 ...

EasyPusher推流服务接口的&period;NET导出

本文是在使用由 EasyDarwin 团队开发的EasyPusher时导出的C++接口的.NET实现 public class EasyPushSDK { public EasyPushSDK() { ...

【linux操作命令】mysql

1.linux下启动mysql的命令: mysqladmin start /ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: mys ...

X86在逻辑地址、线性地址、理解虚拟地址和物理地址

参考:http://bbs.chinaunix.net/thread-2083672-1-1.html 本贴涉及的硬件平台是X86.假设是其他平台,不保证能一一对号入座.可是举一反三,我想是全然可行的 ...

Git拉取、提交、迁出、合并、删除分之命令

#拉取代码 git clone -b 分之名称 git地址 #提交代码 git add . //:注释,if是第一次提交: $ git add --all . (请注意后面有个英文点(表示是当前目录) ...

PHP把采集抓取网页的html中的的&amp&semi;nbsp&semi;去掉或者分割成数组

日期:2017/11/6 操作系统:windows 今天抓取网页的时候出现 无法替换,经过多次测试,找到了办法;(注意是从网页上抓取到的) 分割 explode("  ",HTML ...

AtCoder &vert; ARC102 &vert; 瞎讲报告

目录 ARC102 前言 正文 传送链接~ ARC102 前言 实在是太菜了....写完第一题就弃疗..感觉T3好歹也是道可做题吧!!然后T2怎么又是进制拆分! 正文 A 题意 给你两个数字\(n,k ...

Centos之命令搜索命令whereis与which

Centos之命令搜索命令whereis与which whereis 命令名 #搜索命令所在路径及帮助文档所在位置 选项: -b :只查找可执行文件位置 -m:只查找帮助文件 [root@localh ...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值