StrToTimeDef - C++ Builder

C++ Builder 参考手册 ➙ System::Sysutils ➙ StrToTimeDef


字符串转时间

头文件:#include
命名空间:System::Sysutils
函数原型:

System::TDateTime __fastcall StrToTimeDef(
    const System::UnicodeString S,
    const System::TDateTime Default);

System::TDateTime __fastcall StrToTimeDef(
    const System::UnicodeString S,
    const System::TDateTime Default,
    const TFormatSettings &AFormatSettings);

参数:

  • S:字符串,时间;
  • Default:默认值;
  • AFormatSettings:地区格式;

返回值:

  • 日期时间类型数值,如果转换失败,函数返回参数 Default 的值;
  • 字符串 S 为时间,时间可以是12小时或24小时制;
  • 如果没有 AFormatSettings 参数,
    时间分隔符必须是 FormatSettings.TimeSeparator,
    秒和毫秒之间的小数点必须使用 FormatSettings.DecimalSeparator,
    注意这个小数点同时应用在浮点数和货币类型的小数点显示和类型转换,
    上午可以用 "AM" 也可以用 FormatSettings.TimeAMString (在中国为 "上午"),
    下午可以用 "PM" 也可以用 FormatSettings.TimePMString (在中国为 "下午"),
    如果 FormatSettings 被修改,可以用 Sysutils::GetFormatSettings(); 恢复默认值为本地格式,
    请参考本文例子及 StrToDateTime 的例子;
  • 如果有 AFormatSettings 参数,
    时间分隔符必须是 AFormatSettings.TimeSeparator,
    秒和毫秒之间的小数点必须使用 AFormatSettings.DecimalSeparator,
    上午可以用 "AM" 也可以用 AFormatSettings.TimeAMString (在中国为 "上午"),
    下午可以用 "PM" 也可以用 AFormatSettings.TimePMString (在中国为 "下午"),
    请参考本文例子及 StrToDateTime 的例子;
  • 日期和时间格式可以参考 System::Sysutils::FormatDateTime;
  • 函数 StrToTime、StrToTimeDef 和 TryStrToTime 的区别:
    • StrToTime 转换失败抛出异常;
    • StrToTimeDef 转换失败返回默认值;
    • TryStrToTime 转换结果通过参数返回,函数返回值返回是否转换成功;
  • 没有 AFormatSettings 参数的函数不是线程安全的,因为使用了全局变量 FormatSettings 作为默认的地区格式;带有 AFormatSettings 参数的函数是线程安全的。

例:

void TForm1::ShowTime(TDateTime dt)
{
    Memo1->Lines->Add(FormatDateTime(L"hh:nn:ss.zzz", dt)); // 显示时间
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TDateTime dtDefault = Sysutils::GetTime(); // 使用当前时间作为默认值

    ShowTime(StrToTimeDef(L"1:14:15.678", dtDefault));
    ShowTime(StrToTimeDef(L"13:14:15.678", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 AM", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 PM", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 上午", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 下午", dtDefault));

    Sysutils::FormatSettings.TimeSeparator = L'.';    // 把默认的时间分隔符改成 '.'
    Sysutils::FormatSettings.DecimalSeparator = L','; // 把小数点改成逗号 ','
    Sysutils::FormatSettings.TimeAMString = L"㊤";
    Sysutils::FormatSettings.TimePMString = L"㊦";

    ShowTime(StrToTimeDef(L"1.14.15,678", dtDefault));
    ShowTime(StrToTimeDef(L"13.14.15,678", dtDefault));
    ShowTime(StrToTimeDef(L"1.14.15,678 AM", dtDefault));
    ShowTime(StrToTimeDef(L"1.14.15,678 PM", dtDefault));
    ShowTime(StrToTimeDef(L"1.14.15,678 ㊤", dtDefault));
    ShowTime(StrToTimeDef(L"1.14.15,678 ㊦", dtDefault));

    Sysutils::GetFormatSettings(); // 格式恢复默认值 - 当前地区 (中国) 的格式

    ShowTime(StrToTimeDef(L"1:14:15.678", dtDefault));
    ShowTime(StrToTimeDef(L"13:14:15.678", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 AM", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 PM", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 上午", dtDefault));
    ShowTime(StrToTimeDef(L"1:14:15.678 下午", dtDefault));
}

运行结果:

运行结果

相关:

  • System::Sysutils::DateTimeToStr
  • System::Sysutils::DateTimeToString
  • System::Sysutils::DateToStr
  • System::Sysutils::FormatDateTime
  • System::Sysutils::StrToBool
  • System::Sysutils::StrToBoolDef
  • System::Sysutils::StrToCurr
  • System::Sysutils::StrToCurrDef
  • System::Sysutils::StrToDate
  • System::Sysutils::StrToDateDef
  • System::Sysutils::StrToDateTime
  • System::Sysutils::StrToDateTimeDef
  • System::Sysutils::StrToFloat
  • System::Sysutils::StrToFloatDef
  • System::Sysutils::StrToInt
  • System::Sysutils::StrToIntDef
  • System::Sysutils::StrToInt64
  • System::Sysutils::StrToInt64Def
  • System::Sysutils::StrToTime
  • System::Sysutils::StrToTimeDef
  • System::Sysutils::StrToUInt
  • System::Sysutils::StrToUIntDef
  • System::Sysutils::StrToUInt64
  • System::Sysutils::StrToUInt64Def
  • System::Sysutils::TimeToStr
  • System::Sysutils::TryStrToBool
  • System::Sysutils::TryStrToCurr
  • System::Sysutils::TryStrToDate
  • System::Sysutils::TryStrToDateTime
  • System::Sysutils::TryStrToFloat
  • System::Sysutils::TryStrToInt
  • System::Sysutils::TryStrToInt64
  • System::Sysutils::TryStrToTime
  • System::Sysutils::TryStrToUInt
  • System::Sysutils::TryStrToUInt64
  • System::Sysutils
  • std::atof, std::_ttof, std::_wtof
  • std::_atold, std::_ttold, std::_wtold
  • std::atoi, std::_ttoi, std::_wtoi
  • std::atol, std::_ttol, std::_wtol
  • std::atoll, std::_ttoll, std::_wtoll
  • std::_atoi64, std::_ttoi64, std::_wtoi64
  • std::strtof, std::_tcstof, std::wcstof
  • std::strtod, std::_tcstod, std::wcstod
  • std::strtold, std::wcstold, std::_strtold, std::_tcstold, std::_wcstold
  • std::strtol, std::_tcstol, std::wcstol
  • std::strtoll, std::_tcstoll, std::wcstoll
  • std::strtoul, std::_tcstoul, std::wcstoul
  • std::strtoull, std::_tcstoull, std::wcstoull

C++ Builder 参考手册 ➙ System::Sysutils ➙ StrToTimeDef

你可能感兴趣的:(StrToTimeDef - C++ Builder)