LoginSignup
0
1

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > datetime > 2つの日時の秒の差分 > SecondsBetween() / SecondSpan() > 両方とも0以上の値になる

Last updated at Posted at 2016-01-11
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

やりたいこと

  • 2つの日時を比較する (dt1, dt2)
  • (dt2 >= dt1) && (dt2 < (dt1 + 10)) の時にフラグを立てる

使う関数として以下の2つを調べた

  • SecondsBetween()
  • SecondSpan()

コード

Unit1.cpp
void __fastcall TForm1::Button1Click(TObject *Sender)
{

    TDateTime dt1 = VarToDateTime("2016/01/11 09:00:00");
    TDateTime dt2 = VarToDateTime("2016/01/11 09:00:10");

    // 1.
    __int64 ibtw;

    ibtw = SecondsBetween(dt1, dt2);
    String msg28 = IntToStr((int)ibtw);
    OutputDebugString(msg28.c_str());

    ibtw = SecondsBetween(dt2, dt1);
    String msg31 = IntToStr((int)ibtw);
    OutputDebugString(msg28.c_str());

    // 2.
    double dbtw;

    dbtw = SecondSpan(dt1, dt2);
    String msg37 = FloatToStr(dbtw);
    OutputDebugString(msg37.c_str());

    dbtw = SecondSpan(dt2, dt1);
    String msg44 = FloatToStr(dbtw);
    OutputDebugString(msg44.c_str());
}
結果
デバッグ出力: 10 プロセス Project1.exe (3404)
デバッグ出力: 10 プロセス Project1.exe (3404)
デバッグ出力: 9.99999982304871 プロセス Project1.exe (3404)
デバッグ出力: 9.99999982304871 プロセス Project1.exe (3404)

上記関数使用では2つの日時の差分は常に0以上となる。

...

これらの関数を使わずに、IncSecond(dt1, 10)した値で比較するほうがよさそうだ。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1