下面是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 ;
}

最新文章

  1. 如何在Windows中编译Linux Unix的代码(采用cygwin)?
  2. C#语法糖(Csharp Syntactic sugar)大汇总
  3. Android中两种设置全屏的方法
  4. 【 D3.js 高级系列 — 3.0 】 堆栈图
  5. JS实现复制到剪贴板
  6. Elasticsearch强大的聚合功能Facet
  7. 通过 SignalR 类库,实现 ASP.NET MVC 的实时通信
  8. Java线程:新特征-线程池
  9. SDWebImage 在多线程下载图片时防止错乱的策略
  10. centos下添加的端口不能访问(防火墙关闭)
  11. Sonar安装与使用说明
  12. Dom 动态添加元素节点总结
  13. Once More
  14. 史上最全的MSSQL复习笔记
  15. 苹果电脑thunderbolt连接两台电脑启动方法
  16. Oracle中Inventory目录作用以及如何重建此目录 oraInst.loc 文件
  17. get_class
  18. 8-组成n的1的个数
  19. mysql之mof提权详解
  20. Facebook 爬虫

热门文章

  1. SharePoint 2013无代码实现列表视图的时间段动态筛选
  2. Linux 信号详解三(sleep,raise)
  3. Struts2 框架的快速搭建
  4. 我的Logo设计简史
  5. canvas中的碰撞检测笔记
  6. SQL Server中的索引结构与疑惑
  7. 【REST WCF】30分钟理论到实践
  8. UltraEdit 编译输出中文乱码的解决办法
  9. Centos下编译JDK
  10. C程序两则