Recent

Author Topic: StrToDateTime covert error ..  (Read 7656 times)

todhuk

  • New Member
  • *
  • Posts: 15
StrToDateTime covert error ..
« on: August 06, 2017, 09:17:56 am »
In the example: db_Export_Import
An error occurred while virtual mode exporting from db to excel.
StrToDateTime EConvertError: "г."  is not a valid time ?

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: StrToDateTime covert error ..
« Reply #1 on: August 06, 2017, 11:37:22 am »
Thanks for reporting, but this report is pretty useless since it does not give instructions how to reproduce the error - the demo offers a lot of parameters.

todhuk

  • New Member
  • *
  • Posts: 15
Re: StrToDateTime covert error ..
« Reply #2 on: August 06, 2017, 02:13:23 pm »
I mean, the function StrToDateTime returns this error.
When for examplе:
value = "24.8.2016 г.";
lCell.DateTimeValue := StrToDateTime(VarToStr(value), Workbook.FormatSettings);

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: StrToDateTime covert error ..
« Reply #3 on: August 06, 2017, 02:32:56 pm »
Certainly. But I don't know how to reproduce this error because it does not occur here. The demo has a pagecontrol with three pages. On which page does the error occur? How are the controls on that page set when the error happens? What is your OS? Which Lazarus/fpc/fpspreadsheet versions are you using?

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: StrToDateTime covert error ..
« Reply #4 on: August 06, 2017, 02:43:38 pm »
Also note that "r." is afaik not a valid formatsetting for stringtodatetime....... So the error is correct.
Specialize a type, not a var.

todhuk

  • New Member
  • *
  • Posts: 15
Re: StrToDateTime covert error ..
« Reply #5 on: August 06, 2017, 02:57:53 pm »
Thaddy,
You're right. To avoid this date format, I need to set another custom date format when generating the dbf table.

I use: FPSpreadsheet 1.8.4; Lazarus 1.8RC3; Win 10, 64 bits
« Last Edit: August 06, 2017, 03:05:41 pm by todhuk »

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: StrToDateTime covert error ..
« Reply #6 on: August 06, 2017, 04:30:56 pm »
I still don't know what you are doing. I am a very poor mind-reader...

todhuk

  • New Member
  • *
  • Posts: 15
Re: StrToDateTime covert error ..
« Reply #7 on: August 06, 2017, 06:58:22 pm »
wp, Explain ..

I test the example: db_Export_Import

1. Create a database with random records
 When inserting the date fields, they are formatted by default in Bulgarian.
 (see my_default_date_format.png)
2. Export database table to spreadsheet file
 Now only when option Virtual mode (memory saving) is selected, we have an error.
 The error is when we convert the date fields here:
 lCell.DateTimeValue := StrToDateTime(VarToStr(value), Workbook.FormatSettings);
 (see date_err1.png)

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: StrToDateTime covert error ..
« Reply #8 on: August 06, 2017, 07:19:39 pm »
Still too many options, I don't know where the error happens.

Ok, let's do it step by step...
  • You compile and run the demo db_Export_Import.
  • You don't change anything.
  • Page "1-Create database" says "Record count 5000", "Database type dbf". You don't change this?
  • Page "2-Export to spreadsheet": Which file format did you select?
  • If I understand correctly, the error only occurs when "Export methid" is "Virtual mode (memory saving)".
Is all this correct?

Which unit is raising the error? Can you post a stack trace? This means: When the error happens and the exception dialog comes up click "Break", in Lazarus select "View" > "Debug Windows" > "Call stack". Click "Copy All" and paste the text into your post.

Then, setup a new console program and use this code:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   fpstypes, fpspreadsheet;
  7.  
  8. var
  9.   workbook: TsWorkbook;
  10. begin
  11.   workbook := TsWorkbook.Create;
  12.   WriteLn('workbook.FormatSettings');
  13.   WriteLn('  ShortDateFormat: ', workbook.FormatSettings.ShortDateFormat);
  14.   WriteLn('  LongDateFormat:  ', workbook.Formatsettings.LongDateFormat);
  15.   WriteLn('  ShortTimeFormat: ', workbook.Formatsettings.ShortTimeFormat);
  16.   WriteLn('  LongTimeFormat:  ', workbook.FormatSettings.LongTimeFormat);
  17.   WriteLn('  DateSeparator:   ', workbook.FormatSettings.DateSeparator);
  18.   WriteLn('  TimeSeparator:   ', workbook.FormatSettings.TimeSeparator);
  19.   workbook.Free;
  20. end.

Post the output of this program because I would like to know exactly what your format strings look like.
« Last Edit: August 06, 2017, 07:31:33 pm by wp »

todhuk

  • New Member
  • *
  • Posts: 15
Re: StrToDateTime covert error ..
« Reply #9 on: August 06, 2017, 07:31:42 pm »
In page 2: No matter what export format I choose. The error is always present and does not depend on the export format.

Call Stack
#0 fpc_raiseexception at :0
#1 SYSUTILS_$$_STRTOTIME$ANSISTRING$TFORMATSETTINGS$$TDATETIME at :0
#2 SYSUTILS_$$_STRTODATETIME$ANSISTRING$TFORMATSETTINGS$$TDATETIME at :0
#3 WRITEVIRTUALCELLS(0x14f49f0, 0x15b0180, 0x1565c70) at ..\..\source\common\xlscommon.pas:4758
#4 INTERNALWRITETOSTREAM(0x14f49f0, 0x15b0180) at ..\..\source\common\xlsbiff8.pas:2167
#5 WRITETOFILE(0x14f49f0, 0x150f8a8 'D:\w2\LS\fpspreadsheet-1.8.4\examples\db_import_export\data\people.xls', true, 0) at ..\..\source\common\xlsbiff8.pas:2212
#6 WRITETOFILE(0x151a9d0, 0x150f8a8 'D:\w2\LS\fpspreadsheet-1.8.4\examples\db_import_export\data\people.xls', 2, true, 0) at ..\..\source\common\fpspreadsheet.pas:8295
#7 WRITETOFILE(0x151a9d0, 0x150f8a8 'D:\w2\LS\fpspreadsheet-1.8.4\examples\db_import_export\data\people.xls', SFEXCEL8, true, 0) at ..\..\source\common\fpspreadsheet.pas:8267
#8 EXPORTUSINGVIRTUALMODE(0x1517630, 0x150f8a8 'D:\w2\LS\fpspreadsheet-1.8.4\examples\db_import_export\data\people.xls') at main.pas:501
#9 BTNEXPORTCLICK(0x1517630, 0x1523c10) at main.pas:216

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: StrToDateTime covert error ..
« Reply #10 on: August 06, 2017, 07:54:00 pm »
Is this program working correctly? Please post the output.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   s: String;
  10.   dt: TDateTime;
  11.   fs: TFormatSettings;
  12.  
  13. begin
  14.   fs := DefaultFormatSettings;
  15.  
  16.   s := DateTimeToStr(now);
  17.   WriteLn(s);
  18.   dt := StrToDateTime(s);
  19.  
  20.   WriteLn(fs.ShortDateFormat + ' ' + fs.LongTimeFormat);
  21.  
  22.   s := DateTimeToStr(dt, fs);
  23.   WriteLn(s);
  24.   dt := StrToDateTime(s, fs);
  25.  
  26.   ReadLn;
  27. end.

todhuk

  • New Member
  • *
  • Posts: 15
Re: StrToDateTime covert error ..
« Reply #11 on: August 06, 2017, 08:03:27 pm »
console program ...
var
  workbook: TsWorkbook;
begin
  workbook := TsWorkbook.Create;
  WriteLn('workbook.FormatSettings');
  WriteLn('  ShortDateFormat: ', workbook.FormatSettings.ShortDateFormat);
  WriteLn('  LongDateFormat:  ', workbook.Formatsettings.LongDateFormat);
  WriteLn('  ShortTimeFormat: ', workbook.Formatsettings.ShortTimeFormat);
  WriteLn('  LongTimeFormat:  ', workbook.FormatSettings.LongTimeFormat);
  WriteLn('  DateSeparator:   ', workbook.FormatSettings.DateSeparator);
  WriteLn('  TimeSeparator:   ', workbook.FormatSettings.TimeSeparator);
  workbook.Free;
end.

todhuk

  • New Member
  • *
  • Posts: 15
Re: StrToDateTime covert error ..
« Reply #12 on: August 06, 2017, 08:09:58 pm »
begin
  fs := DefaultFormatSettings;
 
  s := DateTimeToStr(now);
  WriteLn(s);
  dt := StrToDateTime(s); // --> The same error!
 
  WriteLn(fs.ShortDateFormat + ' ' + fs.LongTimeFormat);
 
  s := DateTimeToStr(dt, fs);
  WriteLn(s);
  dt := StrToDateTime(s, fs);
 
  ReadLn;
end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: StrToDateTime covert error ..
« Reply #13 on: August 06, 2017, 08:46:49 pm »
Code: Pascal  [Select][+][-]
  1.   s := DateTimeToStr(now);
  2.   WriteLn(s);
  3.   dt := StrToDateTime(s); // --> [b]The same error![/b]
If that is the case, it is a bug. But I can't reproduce it.
The call should evaluate symmetrical.

We are still missing a lot of information, like FPC version and Laz version and platform and OS and OS version....to name a few....
« Last Edit: August 06, 2017, 08:49:57 pm by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: StrToDateTime covert error ..
« Reply #14 on: August 06, 2017, 10:22:05 pm »
I can confirm if I add extra characters to the time format string even if they are quoted:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   SysUtils;
  6. var
  7.   s: String;
  8.   dt: TDateTime;
  9. begin
  10.   DefaultFormatSettings.LongTimeFormat := '"xx "hh:nn:ss';
  11.  
  12.   s := TimeToStr(now);     // TimeToStr uses format "tt" which is LongTimeFormat
  13.   WriteLn(s);
  14.   dt := StrToTime(s);
  15. end.  

As an alternative, try ScanDateTime from unit DateUtils:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils, DateUtils;
  3.  
  4. var
  5.   s: String;
  6.   dt: TDateTime;
  7. begin
  8.   DefaultFormatSettings.ShortDateFormat := 'dd.mm.yyyy".r"';
  9.   dt := EncodeDate(2017, 12, 12) + 0.5;
  10.   s := DateTimeToStr(dt);
  11.   WriteLn(s);
  12.   dt := ScanDateTime('dd.mm.yyyy.r hh:nn:ss', s);
  13. end.
« Last Edit: August 07, 2017, 12:15:30 am by wp »

 

TinyPortal © 2005-2018