線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1705
推到 Plurk!
推到 Facebook!

localtime問題

答題得分者是:syntax
show
初階會員


發表:57
回覆:48
積分:31
註冊:2003-05-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-12-05 11:25:58 IP:140.126.xxx.xxx 訂閱

[code cpp]
struct tm *my_time,*old_time;
long tmp1=0,tmp2=0;

tmp1=time(NULL)-(Now().DayOfWeek()-1)*86400;
tmp2=time(NULL)-(Now().DayOfWeek()-1)*86400-604800;

my_time=localtime(&tmp1);
old_time=localtime(&tmp2);

OutputDebugStringA(IntToStr(my_time->tm_mon).c_str());
OutputDebugStringA(IntToStr(my_time->tm_mday).c_str());

OutputDebugStringA(IntToStr(old_time->tm_mon).c_str());
OutputDebugStringA(IntToStr(old_time->tm_mday).c_str());

[/code]

為什麼my_time跟old_time的日期都會一樣呢???我給不同的秒數,但出來日期卻是一樣...
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-12-05 14:32:17 IP:61.64.xxx.xxx 訂閱

1. 你應該要學習如何除錯,這是程式設計師的基本要求
2. 你應該要學習如何閱讀 Help,尤其是 BCB 的線上說明,這也是程式設計師的基本能力

如果以上兩點,你都有做到,今天你就不會問這個問題

也表示你的層次又往上提升了一層

那為什麼會這樣呢?看 localtime Help 最後一行:
localtime returns a pointer to the structure containing the time elements. This structure is a static that is overwritten with each call.

英文如果看不懂 (哪很不好喔!除非你超過 40 歲),那意思是說,傳回值只是一個指標,指向一個用來儲存資料的結構,後面的資料會覆蓋前面的


所以你看到的,都是是最後一個 tmp2 的時間,tmp1 的時間被覆蓋掉了

改成這樣:


[code cpp]
struct tm my_time, old_time;
time_t tmp1, tmp2;

tmp1=time(NULL)-(Now().DayOfWeek()-1)*86400;
tmp2=time(NULL)-(Now().DayOfWeek()-1)*86400-604800;

my_time=*localtime(&
tmp1);
old_time=*localtime(&
tmp2);

[/code]

===================引 用 show 文 章===================


[code cpp]
struct tm *my_time,*old_time;
long tmp1=0,tmp2=0;

tmp1=time(NULL)-(Now().DayOfWeek()-1)*86400;
tmp2=time(NULL)-(Now().DayOfWeek()-1)*86400-604800;

my_time=localtime(&tmp1);
old_time=localtime(&tmp2);

OutputDebugStringA(IntToStr(my_time->tm_mon).c_str());
OutputDebugStringA(IntToStr(my_time->tm_mday).c_str());

OutputDebugStringA(IntToStr(old_time->tm_mon).c_str());
OutputDebugStringA(IntToStr(old_time->tm_mday).c_str());

[/code]

為什麼my_time跟old_time的日期都會一樣呢???我給不同的秒數,但出來日期卻是一樣...
系統時間:2024-05-08 4:05:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!