程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Ctime函數 在ACM中的應用

Ctime函數 在ACM中的應用

編輯:C++入門知識

[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.

樣例輸入
0
1111111111
1363592469
2147483647
樣例輸出
1970-01-01 00:00:00
2005-03-18 01:58:31
2013-03-18 07:41:09
2038-01-19 03:14:07
提示
無來源
Minary題意: 輸入從1970-01-01 00:00:00 到現在的秒數  問現在的時間是什麼


[cpp] view plaincopyprint?/*
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; 

/*
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 時區名稱,如果不能得到時區名稱則返回空字符。
%% 百分號


[cpp]
#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; 

#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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved