下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:
This page is part of release 3.35 of the Linux man-pages project.
#include <time.h>
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
<time.h> 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<iostream>
#include<ctime>
#include<string.h>
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<<"The day of the week is(0~6): "<<ans->tm_wday<<endl;
}
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<<tm2->tm_mon+<<' '<<tm2->tm_mday<<"th "<<tm2->tm_year+<<endl;
}
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<<"Now the time is: "<<asctime(timeInfo);//has already '\n'
today.year=timeInfo->tm_year+;
today.month=timeInfo->tm_mon+;//starts from 0
today.day=timeInfo->tm_mday;
otherDay=add(today,,,);
cout<<"After 1 year 1 month and 1 day,it will be:"<<endl
<<otherDay.month<<' '<<otherDay.day<<"th,"<<otherDay.year
<<endl;
cout<<"Input the date(mm dd yy):";
cin>>mm>>dd>>yy;
tellWeekday(mm,dd,yy);
cout<<"Input date(mm dd yy),return the next Monday:";
cin>>mm>>dd>>yy;
nextMondayDate(mm,dd,yy);
system("pause");
return ;
}

C语言的时间函数的更多相关文章

  1. [转帖]C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义

    C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义 https://blog.csdn.net/willyang519/article/d ...

  2. 常用C语言time时间函数

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

  3. c语言随机函数&&时间函数

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

  4. R语言日期时间函数

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

  5. C 语言 时间函数使用技巧(汇总)

    time.h 头文件 是 C 语言中 有关 时间的函数所储存的头文件 #include <time.h> 在介绍时间函数用法之前,我们首先要了解在 time.h 头文件中已经声明了的一个结 ...

  6. C语言 常用的时间函数

    //时间函数的使用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include& ...

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

    Loadrunner中取时间函数.用时间函数生成订单编号例子: <如要转载,请注明网络来源及作者:Cheers_Lee> 问题的提出: (1)有时候在Loadrunner中用C语言设计脚本 ...

  8. Oracle日期时间函数大全

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

  9. Linux时间函数之gettimeofday()函数之使用方法

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

随机推荐

  1. 代码整洁--使用CodeMaid自动程序排版

    在项目开发的过程中,如果只是验证命名规则.而没有统一程序排版,项目中很容易就会出现类似下列范例的程序代码产出.这样的产出,虽然能够正常地提供项目功能.并且符合微软的命名规则,但是因为程序排版凌乱的问题 ...

  2. SQL Server存储过程中使用表值作为输入参数示例

    这篇文章主要介绍了SQL Server存储过程中使用表值作为输入参数示例,使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程或函数)发送多行数据,这样 ...

  3. Oracle Coherence应用部署到Jboss EAP 6.x 时 NoClassDefFoundError: sun/rmi/server/MarshalOutputStream 的解决办法

    今天将一个web应用从weblogic 10.3迁移到jboss EAP 6.3上,该应用使用oracle coherence做为缓存,部署上去后,启动时一直报如下错误:     at java.ut ...

  4. OFFSET约束(OFFSET IN 和OFFSET OUT)

    OFFSET 的意思是偏移.对于同步时序电路来说,数据和时钟之间的偏移量是必须要关注的.OFFSET IN和OUT分别对应的是输入和输出FPGA数据和时钟之间的偏移关系,本文将分析这一种关系.阅读本文 ...

  5. iis7配置网站容易出现的问题(转)

    来源: http://www.cnblogs.com/5426z/articles/4865022.html 1.64位操作系统 access数据库提示:未在本地计算机上注册"Microso ...

  6. 前端人员一定要掌握的PS技巧

    一.PS与前端知多少 一般我们会认为PS是用来修改图片的,这些工作是美工人员做的事不是前端人员做的,其实这样想你就错了,因为在前端人员也是要学会一些简单的关于PS的技巧的,这样就不会应为一点点小小的需 ...

  7. IIS7.5开启GZip压缩

    在IIS7.5选择要开启GZip压缩的网站,在功能视图中找到并双击"压缩"图标,在压缩界面中钩选"启用静态内容压缩"和"启用动态内容压缩", ...

  8. kill 根据PID终止进程

    根据PID终止进程 kill [option] PID-list kill 通过向一个或多个进程发送信号来终止进程.除超级用户外,只有进程的所有者才可以对进程执行kill 参数 PID-list为ki ...

  9. 如何设置unobtrusive的语言包

    场景:网站是用的validate.unotrusive.js验证的,网站的语言已经切换到繁体了,但是提示语言还是英文. 环境:asp.net mvc4,jquery.validate.unotrusi ...

  10. [cross domain] four approachs to cross domain in javascript

    four approachs can cross domain in javascript 1.jsonp 2.document.domain(only in frame and they have ...