Ctime函数 在ACM中的应用

来源:互联网 发布:心动网络客服电话 编辑:程序博客网 时间:2024/06/01 11:12
  • 咱们从一个题目开始进行导入
  • http://acm.nbut.cn/Contest/view/id/38/problem/C.xhtml

  • [C] Minary Fights with Time

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • Minary is busy now. As a result, she's fighting with time.
    To serialize every time point she marked, she decide to make every time point to an integer.

    You can imagine that 1970-01-01 00:00:00 GTM(Greenwich Mean Time) is the start. Give you an integer that indicates the total seconds from that time, you should output the time it stands for.

  • 输入
  • This problem contains several cases, input until EOF.
    Every case is an integer(-2^31 ~ 2^31-1) that indicates the total seconds from 1970-1-1 00:00:00 GTM.
  • 输出
  • For every case, you should output its time.
  • 样例输入
  • 0111111111113635924692147483647
  • 样例输出
  • 1970-01-01 00:00:002005-03-18 01:58:312013-03-18 07:41:092038-01-19 03:14:07
  • 提示
  • 来源
  • Minary

题意: 输入从1970-01-01 00:00:00 到现在的秒数  问现在的时间是什么

/*struct tm{    int tm_sec;  秒,正常范围0-59, 但允许至61    int tm_min;  分钟,0-59    int tm_hour; 小时, 0-23    int tm_mday; 日,即一个月中的第几天,1-31    int tm_mon;  月, 从一月算起,0-11  1+p->tm_mon;    int tm_year;  年, 从1900至今已经多少年 1900+ p->tm_year;    int tm_wday; 星期,一周中的第几天, 从星期日算起,0-6    int tm_yday; 从今年1月1日到目前的天数,范围0-365    int tm_isdst; 日光节约时间的旗标};需要特别注意的是,年份是从1900年起至今多少年,而不是直接存储如2011年,月份从0开始的,0表示一月,星期也是从0开始的, 0表示星期日,1表示星期一。*/#include <iostream>#include <cstdio>#include <cstring>#include <ctime>using namespace std; int main(){    time_t stamp;/*time_t 这种类型就是用来存储从1970-01-01 00:00:00到现在经过了多少秒 这个系统函数就是从1970-01-01 00:00:00开始计算的 不可以修改   由于精度问题造成*/    tm* a;    while(scanf("%d", &stamp)!=EOF)    {        a = gmtime(&stamp);//将time_t表示的时间转换为tm结构体的形式        printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",a->tm_year + 1900,            a->tm_mon + 1,            a->tm_mday,            a->tm_hour,            a->tm_min,            a->tm_sec        );    }         return 0;}


注意 ctime函数表示的时间范围有限 CTime对象仅能被用来表示1970-1-1至2038-1-18之间的日期





%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号

#include<stdio.h>#include<string.h>#include<time.h>int main( void ){    struct tm *newtime;    char tmpbuf[128];    time_t lt1;time( <1 );//获得当前时间    newtime=localtime(<1);//转换成当地时间strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);    printf(tmpbuf);return 0;}




原创粉丝点击