當前位置:首頁 » 自動清洗 » C語言怎樣定義不同時間
擴展閱讀
如何判斷貓不孕的原因 2024-10-03 00:23:39
電動汽車後減震怎樣卸 2024-10-02 23:34:27
上海脫毛多少錢 2024-10-02 23:05:45

C語言怎樣定義不同時間

發布時間: 2023-05-26 22:19:41

① C語言 怎樣定義日期

偶爾會用到,老忘記怎麼用。

把我保存的一個頁面粘過來了,一起分享。

---------------------------------------------
1 time
時間,日期和本地化函數
點擊數:1005 發布日期:2005-2-22 15:21:00
【評論】 【列印】 【編程愛好者論壇】 【關閉】

標准庫函數除了定義一些處理日期和時間的函數外,還定義了帆改處理與程序有關的地理信息的函數。對這些函數討論如下。

時間和日期函數需要頭部<time.h>。這個頭部定義了三種與時間有關的類型:clock_t,time_t蛅m。類型clock_t和time_t可以用長整數表示系統時間和日期,稱為日歷時。結構類型tm保存分解為相應元素的日期和時間。tm結構包含下列成員:

int tm_sec; 秒,0~59
int tm_min; 分,0~59

int tm_hour; 時,0~23
int tm_mday; 日,1~31
int tm_mon; 月,0~11
int tm_year; 年(從1900年開始)
int tm_wday; 星期(從周日開始),0~6
int tm_yday; 天數(從1月開始),0~365
int tm_isdst; 夏令時指示器

實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負,ANSI C標准稱這種表示為分解時(broken-down time)。

此外,<time.h>中定義了宏CLOCK_SPER_SEC,它表示系統時鍾每秒嘀嗒的次數。

地理環境函數需要頭部<locale.h>,它定義了結構lconv.

1.asctime

#inlcude <time.h>
char *asctime(const struct tm *ptr);

函數返回指向一個串的指針,其中保存ptr所指結構中存儲的信息變換形式,具體格式如下:
day month date hour:minutes:seconds year\n\0
例如:Fri Apr 15 12:05:34 2005

由ptr指向的結構一般是通過調用localtime()或gmttime()香到的。

保存asctime()返回的格式化時間串空間是靜態變數。因此每次調用asctime()時都用新串沖掉該靜態字元數組中的原值。希望保存以前的結物豎果時,應該把它復制到別處。

例態螞判子:顯示系統定義的本地時間。

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm *ptr;
time_t lt;

lt =time(NULL);
ptr=localtime(<);
printf(asctime(ptr));

return 0;

}

2. clock

#include <time.h>
clock_t clock(void);

函數clock()返回近似行於調用程序運行時間量的值,該值除以CLOCKS_PER_SEC後轉換為秒數。返回-1值表示無法取得系統時間。

例子:顯示程序調用它所需的當前執行時間。

void elapsed_time()
{
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);
}

3. ctime

# include <time.h>
char *ctime(const time_t *time);

如果指針指向日歷時間的話,則ctime()返回一指針到形式為如下所示的串:
day month year hours:minutes:seconds year \n\0

日歷時一般通過調用time()取得。
ctime()放置格式化的輸出字元串所用的緩沖區是靜態分配的字元數組,每次該函數調用時都重寫。如果希望保存該串的內容,應該把它拷貝到其它地方。

例子:顯示系統定義的本地時間

#include <time.h>
#include <stdio.h>

int main(void)
{
time_t lt;

lt = time(NULL);
printf(ctime(<);

return 0;

}

4.difftime

#include <time.h>
double difftime(time_t time2,time_t time1);

函數difftime()以秒為單位,返回time2-time1的差.

例子:統計5000000次空的FOR循環的時間,單位為秒。

#include <time.h>
#include <stdio.h>

int main(void)
{
time_t start,end;
volatile long unsigned t;

start = time(NULL);
for(t= 0;t<5000000;t++);
end = time(NULL);
printf("Loop used %f seconds.\n",difftime(end,start));

return-;
}

5.gmtime

#include <time.h>
struct tm *gmtime(const time_1 *time);

函數gmtime()返回一個指針,指針指向以tm結構形式的分解形式time。時間用UTC即格林尼治時間表示,time4指針一般通過調用time()取得。如果系統不支持UTC,則該函數返回空指針。

作者: 221.254.228.* 2006-4-19 15:15 回復此發言

--------------------------------------------------------------------------------

2 time
gmtime()用來保存分解時間的結構變數是靜態分配的,每次調用gmtime()時都重寫。希望保存結構的內容時,必須把它拷貝到其他地方。

例子:既輸出系統的本地時間,也輸出系統的UTC時間。

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm *local,*gm;
time_t t;

t=time(NULL);
local = localtime(&t);
printf("Local time and date:%s\n",asctime(local));
gm=gmtime(&t);
printf("CUT and date:%s",asctime(gm));

return 0;
}

6.localeconv

#include <locale.h>
struct lconv *localeconv(void);

函數localeconv()返回指向結構類型lconv的指針,其中包含大量的地理環境信息,這些信息與格式化的道路號有關。

例子:顯示當前定域所使用權用的十進制小數點字元。

#include <stdio.h>
#include <locale.h>

int main()
{
struct lconv lc;

lc= *localeconv();
pirntf("Decimal symbol is:%s\n",lc.decimal_point);

return 0;
}

7.localtime

#include <time.h>
struct tm *localtime(const time_t *time);

函數localtime()返回指向以tm結構形式的分解形式time的一個指針。該時間表示為本地時間。time指針一般通過調用 time()取得。
localtime()用於保存分解時間的結構變數是靜態分配的,每次調用時都重寫。因此希望保存這個結構的內容時,應該在再次調用 之前先拷貝到其他地方。

例子:輸出系統的本地時間和UTC時間。

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm *local;
time_t t;

t=timw(NULL);
local=localtime(&t);
printf("Local time and date :%s\n",asctime(local));
local=gmtime(&t);
printf("UTC time and date :%s\n",asctime(local));

return 0;
}

8.mktime

#include <time.h>
time_t mktime(struct tm *time);

mktime()返回time所指的結構變數中找到的分解時間等價的日歷時。無素tm_wday和tm_yday由該函數設置,因此調用時不必定義。
如果 mktime()不能把信息表示為合法日歷時,則返回-1。

例子:顯示2005年1月3日是星期幾。

#include <time.h>
#include <stdio.h>

int main(void)
{
stuct tm t;
time_t t_of_day;

t.tm_year=2005-1900;
t.tm_mon=0;
t.tm_mday=3;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;

t_of_day=mktime(&t);
printf(ctime(&t_of_day));

return 0;
}

9.setlocale

#include <locale.h>
char *setlocale(int type,const char *locale);

函數setlocale()用於查詢和設置某些參數,它們與和序執行的地理環境有關。
如果 locale為空指針,則setlocale()返回一個指針,指向當前本地化信息串;否則,setlocale()試用locale串中的本地化信息按type的說明設置本地化參數。
要指定標準的 C的場所(local),請使用串"C";要指定本地環境,請使用空串" "。關於它所支持的本地化串,請參閱自已的編譯程序文檔。
調用 時,type必須取下列的宏之一(在<locale.h>中定義):

LC_ALL
LC_COLLATE
LC_CTYPE
LC_MONETARY
LC_NUMERIC
LC_TIME

LC_ALL代表所有本地化類別。LC_COLLATE影響函數strcoll()的操作。LC_CTYPE改變字元函數的工作方法。LC_MONETARY確定貨幣的格式。LC_NUMERIC改用輸出/輸入函數所用的十進制小數點符號。LC_TIME決定函數strftime()的的行為。
函數setlocale()返回一個指針,指向與type參數關聯的一個串。

例子:顯示當前的本地設置

#include <locale.h>
#include <stdio.h>

int main(void)
{
printf(setlocale(LC_ALL," "));

return 0;

}

10. strftime

#include <time.h>
size_t strftime(char *str,size_t maxsize,const char *fmt,const struct tm *time);

作者: 221.254.228.* 2006-4-19 15:15 回復此發言

--------------------------------------------------------------------------------

3 time

函數strftime()根據fmt指向的串中的格式命令以及使用time指向的分解時間,把時間,日期信息與其他信息放在str指向串中。最多向str中放maxsize個字元。
在 C99中,str、fmt及time由restrict修飾。
函數 strftime()的操作有些類似於sprintf():識別以百分號(%)開始的格式命令集合,格式化輸出結果放在一個串中。格式化命令說明串str各種日期和時間信息的確切表示方法。格式串中的其他字元原樣放進串中。日期和時間性按本地時間顯示。格式命令列在下表中,顯然它們是區分大小寫的。

命令 替換為
%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 每周的第幾天,星期一為第一天
%U 第年的第幾周,星期日為第一天
%V 每年的第幾周,使用基於周的年
%w 十進製表示的星期幾
%W 每年的第幾周,星期一為第一天
%x 標準的日期串
%X 標準的時間串
%y 不帶世紀的十進制年份
%Y 帶世紀部分的十制年份
%z
與UTC的偏差
%Z 時區名稱
%% 百分號

C99允許用E或修改某些格式strftime()命令。E可以修改c,C,x,X,y,Y,d,e和H,O可以修改I,m,M,S,u ,U,V,w,W和y.這些修飾符必變了時間和/或日期的顯示方式。
基於周的年用於%g,%G和%V格式命令。根據這一表示方法,星期一為每周的第一天,而第年的第一周必須包括1月4日。

例子:假設LTIME指向的包含「10:00:00 AM」的結構,以下代碼將輸出「it is now 10AM".

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm *ptr;
time_t lt;
char str[80];

lt=time(NULL);
ptr=localtime(<);

strftime(str,100,"It is now %H %p",ptr);
printf(str);

return 0;

}

11 time

#include <time.h>
time_t time(time_t *time);

函數time()返回系統的當前日歷時間,如系統和丟失時間設置,函數返回-1。
對函數 time_t的調用,既可用空指針,也可以使用指向time_t類型變數的指針。如果使用字母,則該變數也可賦予日歷時間。

例子:顯示系統定義的本地時間。

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm *ptr;
time_t lt;

lt = time(NULL);
ptr=localtime(<);
printf(asctime(ptr));

return 0;

}

② c語言 輸入時間顯示不同時間段

#include <stdio.h>


int main()

{

//int a[10];

int time;

char c;

int m;

printf("請輸時間: ");

scanf("%d%c%d",&time,&c,&m);

if(0<=time&&time<=5)

{

if(0<=m&&m<9)

printf("凌晨好,現在是%d%c0%d",time,c,m);

printf("凌晨好,現在是%d%c%d",time,c,m);

}

乎槐if(5<time&&time<=8)

{

if(0<=m&&m<9)

printf("早上好,現在是%d%c0%d",time,c,m);

printf("早上好,現在是%d%c%d",time,c,m);

}

if(8<time&&time<=11)

{

瞎枯 if(0<=m&&m<9)

printf("上午好,現在是%d%c0%d",time,c,m);

printf("上午好,現在是%d%c%d",time,c,m);

}

if(11<time&&time<=13)

{

if(0<=m&&m<9)

printf("中午好,現在是%d%c0%d",time,c,m);

printf("中午好,現在是%d%c%d",time,c,m);

}

if(13<time&&time<=18)

{

if(0<=m&&m<9)

printf("下午好,現在是%d%c0%d",time,c,m);

printf("下午好,現在是%d%c%d",time,c,m);

}

if(18<time&&time<24)

{

if(0<=m&&m<9)

printf("晚上磨頃洞好,現在是%d%c0%d",time,c,m);


printf("晚上好,現在是%d%c%d",time,c,m);

}

return 0;

}


ok

③ 怎麼用C語言定義時間

樓主世辯,我這段代碼是讀取時間的,不知道是不是你想衫碧要的:
#include <stdio.h>
#include <time.h>
int main(void)
{time_t now = time(NULL);
tm *t = localtime(&now);
printf("%d-%d-%d %d:%d:%d\或返舉n", t->tm_year+1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
getchar();
return 0;}

④ C語言編程如何設置時間

struct tm *tm; time_t t;
char timebuf[20]
memset(timebuf,0,sizeof(timebuf));
time(&t);
tm = localtime(&t);

strftime(timebuf,15,"雹猜%Y%m%d%H%M%S",tm);
printf("current time is [%s]"源判型沖孝,timebuf);

⑤ C語言中怎麼將一天分為凌晨早晨上午中午等時段

#includeint main(){ int hour,minute; char ch; printf("請輸入現在的時刻: "); while(scanf("%d:%d",&hour,&minute)!=2||hour=24||minute=60) { printf("您的輸入有誤,請雀猛尺重新輸入!!!\n"); while((ch=getchar())!='\知改n') continue; printf("請輸入現頃高在的時刻: "); } getchar(); if(hour>=0&&hour<6) printf("凌晨好!!!\n"); else if(hour<12) printf("早上好!!!\n"); else if(hour<18) printf("下午好!!!\n"); else if(hour<24) printf("晚上好!!!\n"); return 0; }