Recent

Author Topic: How to write Date and Time string in the same format as it was read?  (Read 2968 times)

alpine

  • Hero Member
  • *****
  • Posts: 1387
Re: How to write Date and Time string in the same format as it was read?
« Reply #15 on: January 18, 2024, 01:22:53 pm »
@Zvoni
I'll absolutely agree with you, the only right thing that can be done is for OP to put a date-stamp format config into, either on  the application level or for a specific import source. And to include a mandatory dialog asking for it.

Thus, the TRon stance (which we all share, I believe) and the wp explaination, are coming to explain to the OP  (part) of the difficulties working with date/times in programs.

Not to mention that there are issues with such a strings even into the RTL ScanDateTime function, namely z format character - it scans ambiguously numbers of length 1 and 2: https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl-objpas/src/inc/dateutil.inc#L2697

The moral for the OP is: be very careful how you process date/time data in you program and prefer to ask the user instead of pretending to be a smart-ass.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: How to write Date and Time string in the same format as it was read?
« Reply #16 on: January 18, 2024, 04:03:52 pm »
The only and yes, the only correct way to store a date and time in a database as a string is in YYMMYYYY format and in UTC. For databases that support is, store the datetime as double. You correct the date and time to your local format o read. Really funny that nobody gave the correct answer....
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zvoni

  • Hero Member
  • *****
  • Posts: 2964
Re: How to write Date and Time string in the same format as it was read?
« Reply #17 on: January 18, 2024, 04:40:13 pm »
The only and yes, the only correct way to store a date and time in a database as a string is in YYMMYYYY format and in UTC. For databases that support is, store the datetime as double. You correct the date and time to your local format o read. Really funny that nobody gave the correct answer....

Funny how Remy gave that answer already, and TS explained why he can't use it
The best solution would be to use a locale-independent format, like ISO-8601, eg: 2023-12-30T22:09:08
File CSV is obtained from external system which is out of my control. I can't set ISO-8601 date format in it.
The same with my output file: it is supplied for further processing by customer. Format should be preserved.

Sure, you could use FormatDateTime(), if you know which format you want to use. That means you need to parse the input string to determine its formatting.  Standard routines won't help you with that.
I see, thanks.
« Last Edit: January 18, 2024, 04:43:53 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018