【问题标题】:Encode Time Issue in Delphi 2010Delphi 2010 中的编码时间问题
【发布时间】:2011-02-28 13:45:32
【问题描述】:

当我们使用 EncodeTime 函数 EncodeTime(wHour, wMinute, wSecond, wMilliseconds) 时,它不会将毫秒值分配给 Result。

我们使用下面来编码日期和时间

Result := EncodeDate(wYear, wMonth, wDay) +
  EncodeTime(wHour, wMinute, wSecond, wMilliseconds);

我们要解析为 DateTime 的字符串的值为 Apr 10 2008 7:21:31:460PM,但编码后我们得到的输出为 10/04/2008 07:21:31

结果仅包含 HH:MM:SS 值,而不包含毫秒值。

请让我们知道是否有格式化值并将其与毫秒一起存储在变量中。 *******************我正在尝试的功能*************

function DateTimeParser(theString :string):TDateTime;
var wYear,wMonth,wDay,wHour, wMinute, wSecond,wMilliseconds : Word  ;
Date,Month,Med :String;
Time : TDateTime;
testtime,testtime1 : TSystemTime;
var  myDateTime : TDateTime;
begin
 Month := Copy(theString,1,3) ;
 if Month ='Jan' then wMonth := 01
     else if  Month ='Feb' then  wMonth := 02
     else if  Month ='Mar' then  wMonth := 03
     else if  Month ='Apr' then  wMonth := 04
     else if  Month ='May' then  wMonth := 05
     else if  Month ='Jun' then  wMonth := 06
     else if  Month ='Jul' then  wMonth := 07
     else if  Month ='Aug' then  wMonth := 08
     else if  Month ='Sep' then  wMonth := 09
     else if  Month ='Oct' then  wMonth := 10
     else if  Month ='Nov' then  wMonth := 11
     else if  Month ='Dec' then  wMonth := 12
     else ShowMessage('Not a Valid Month');
wYear           :=  StrToInt(Copy(theString,8,4)) ;
wDay            :=  StrToInt(Copy(theString,5,2)) ;
wHour           :=  StrToInt(Copy(theString,13,2)) ;
wMinute         :=  StrToInt(Copy(theString,16,2)) ;
wSecond         :=  StrToInt(Copy(theString,19,2)) ;
wMilliseconds   :=  StrToInt(Copy(theString,22,3)) ;

ShowMessage(IntToStr(wMilliseconds));

{if Copy(theString,25,2)= 'PM' then
 wHour := wHour+12;}

Result := DateUtils.EncodeDateTime(wYear, wMonth, wDay,wHour, wMinute, wSecond, wMilliseconds);
//Result := Result+DateUtils.EncodeTime(wHour, wMinute, wSecond, wMilliseconds div 100);

 myDateTime:= EncodeDate(2009,11,28)+EncodeTime(14,23,12,001);
 ShowMessage(DatetimetoStr(myDateTime));
testtime1 := testtime;


Time :=EncodeTime(wHour, wMinute, wSecond, wMilliseconds);
            ShowMessage(DateTimeToStr(Result));

**********************************************************************


end;

有什么想法吗?

【问题讨论】:

  • 感谢 Adrian,我确实尝试使用该选项,但仍然没有存储毫秒值。
  • DateTimeParser 的结果确实包含毫秒。但是您的测试字符串Apr 10 2008 7:21:31:460PM 不起作用。您需要用 0 Apr 10 2008 07:21:31:460PM 填充小时。
  • 在这里聚会迟到了,但您可能想要的是 AsSQLTimeStamp (docwiki.embarcadero.com/Libraries/en/…)

标签: delphi delphi-2010 dbexpress tdatetime


【解决方案1】:

我可能误解了这里的问题,但也许它正在被存储但你没有看到它。调试器不显示毫秒,DateTimeToStr 也不显示。 FormatDateTime 带有格式字符串。

var
    Date: TDateTime;
begin
    Date := EncodeDateTime(2011, 02, 28, 20, 43, 10, 12);

    //DateTimeToStr does not show milliseconds
    ShowMessage(DateTimeToStr(Date));

    //Use FormatDateTime with Format string
    ShowMessage(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz' , Date));
end;

数据库

您的dbexpress 标签表明您正在尝试将datetime 存储在数据库中。我不了解 dbexpress,但 ADO 会从 datetime 中截断毫秒。 To save with milliseconds in SQL Server with ADO 您必须自己构建插入语句。它可能与 dbexpress 相同。

这是一些 ADO 代码,可以在 SQL Server 中以毫秒为单位保存 datetime

ADOCommand1.CommandText := 'insert into DateTbl values ('''+
    FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz' , Date)+''')';
ADOCommand1.Execute;

datetime 在 SQL Server 中的精度为 3.33 毫秒。这与 Delphi 中的不同,因此当我保存 2011-02-28 20:43:10.012 时,它在 SQL Server 中另存为 2011-02-28 20:43:10.013。这对你来说可能是个问题。

您的一个解决方案可能是将datetime 的毫秒部分存储在单独的整数列中。这样,您将始终存储在 Delphi 中编码的相同值,而不必构建自己的插入语句。

DBExpress

我已经对 DBX 组件进行了一些测试,但它们也截断了毫秒。

【讨论】:

  • 嗨,格式化后,发现db express在发送到数据库时正在修剪毫秒部分。我从数据库中收到以下错误消息。例如:'日期与通过日期不匹配:01/03/2011 10:42:05.460 现有日期应为:01/03/2011 10:42:05.463'
  • @SSE - 改用 AsSQLTimeStamp。这会保留毫秒。
【解决方案2】:

使用这种格式 HH:MM:SS.ZZZ

干杯

【讨论】:

    【解决方案3】:

    这对您来说可能并不明显,但在默认的日期和时间格式中,秒和毫秒通常用点分隔 (.)。您在问题 Apr 10 2008 7:21:31:460PM 中显示的示例字符串在该位置有一个冒号 (:)。这很可能会导致毫秒数减少。

    【讨论】:

    • 嗨,格式化后,发现db express在发送到数据库时正在修剪毫秒部分。我从数据库中收到以下错误消息。例如:'日期与通过日期不匹配:01/03/2011 10:42:05.460 现有日期是:01/03/2011 10:42:05.463' 有什么办法可以用 db express
    • @user637761: 对 dbexpress 的了解不够透彻,无法说出一种或另一种方式,但它可能是正在进行调整的数据库。我确实知道,例如 SQL 服务器确实会丢弃传递给它的毫秒数。您可以尝试使用 TIMESTAMP 列而不是 DATETIME(不确定确切名称)。也许 TIMESTAMP 列在您正在使用的数据库中确实具有毫秒分辨率。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 2011-01-08
    • 2013-11-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多