DateTime range問題 |
尚未結案
|
erutluv
一般會員 發表:7 回覆:7 積分:7 註冊:2003-04-22 發送簡訊給我 |
|
furbylin
一般會員 發表:6 回覆:29 積分:12 註冊:2003-08-29 發送簡訊給我 |
引言: 問個time range 問題 int srcDateTime = 20030821220918; //(yyyymmddhhnnss) int destDateTime = 20030903010301; //(yyyymmddhhnnss) BCB 有沒有可以算出 srcDateTime ~ destDateTime 期間經過了?天?小時?分?秒的method您好: 在此修正一個錯誤, 在BCB中, 一個int占4byte, 若是unsigned的話, 可表示的最大值為 2^32 = 4294967296, 已經不符你的需求了, 除非改成__int64型別, 不過還是建議你使用String型態, 以下是我的作法, 不知是否符合你的需求? void ShowPassDateTime(TDateTime src, TDateTime dest); void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString srcDateTime = "20030821220918"; //(yyyymmddhhnnss) AnsiString destDateTime = "20030903010301"; //(yyyymmddhhnnss) AnsiString srcDate = srcDateTime.SubString(1,4) "/" srcDateTime.SubString(5,2) "/" srcDateTime.SubString(7,2); AnsiString srcTime = srcDateTime.SubString(9,2) ":" srcDateTime.SubString(11,2) ":" srcDateTime.SubString(13,2); AnsiString destDate = destDateTime.SubString(1,4) "/" destDateTime.SubString(5,2) "/" destDateTime.SubString(7,2); AnsiString destTime = destDateTime.SubString(9,2) ":" destDateTime.SubString(11,2) ":" destDateTime.SubString(13,2); TDateTime src = StrToDate(srcDate) StrToTime(srcTime); TDateTime dest = StrToDate(destDate) StrToTime(destTime); ShowPassDateTime(src, dest); } //--------------------------------------------------------------------------- void ShowPassDateTime(TDateTime src, TDateTime dest) { double passtime = dest - src; double fraction; int days,hours=0, minutes=0, seconds=0; if (passtime < 0) passtime = -passtime; days = (int) passtime; fraction = passtime - days; if (fraction != 0) { hours = (int)(fraction * 24); fraction = (fraction*24) - hours; if (fraction != 0) { minutes = (int)(fraction * 60); fraction = (fraction*60) - minutes; if (fraction != 0) { seconds = (int)(fraction * 60); fraction = (fraction*60) - seconds; } } } ShowMessage("經過了" IntToStr(days) "天" IntToStr(hours) "小時" IntToStr(minutes) "分" IntToStr(seconds) "秒"); }在此必須注意的是StrToDate及StrToTime, 最好加個try{} catch(...){} 或者是你的日期時間字串符合 |
gemi0305
版主 發表:81 回覆:564 積分:629 註冊:2003-05-11 發送簡訊給我 |
針對這個int的資料溢位,我做個補充,
Remarks All data transmitted on the network during a remote procedure call must resolve to a base type or predefined type. MIDL supports the following base types: boolean, byte, char, double, float, handle_t, hyper, long, short, small, and void *. The keywords signed and unsigned can be used to qualify integer and character types. MIDL also provides the predefined types error_status_t and wchar_t. Base types can appear as type specifiers in const declarations, typedef declarations, general declarations, and as parameter type specifiers in function declarators. The base and predefined types have the following default signs and default sizes: Base type Default sign Description boolean unsigned 8-bit data item byte - (not applicable) 8-bit data item char unsigned 8-bit unsigned data item double - 64-bit floating-point number float - 32-bit floating-point number handle_t - Primitive handle type hyper signed 64-bit signed integer long signed 32-bit signed integer short signed 16-bit signed integer small signed 8-bit signed integer void * - 32-bit context handle pointer type wchar_t unsigned 16-bit unsigned data item Any other types in the interface must be derived from these base or predefined types. This requirement has the following two important effects: The type int cannot appear in remote functions without a size qualifier such as short, small, long or hyper. The type void * cannot appear in remote functions except when it is used to define a context handle. DCE IDL compilers do not recognize the keyword signed. You can only use the keyword unsigned when you use the MIDL compiler in DCE-compatibility mode.在bcb中有個hyper可以用,所以當你的int不夠用的時候, 可以換這個64bit試試 hyper srcDateTime = 20030821220918; ShowMessage(IntToStr(srcDateTime));國泰平安 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |