I am storing a TDateTime in an ini file. I live in America where Christmas
is 12/25/2002. Users elsewhere in the world write it as 25/12/2002. When
my program reads in the DateTime (12/25/2002) in a country that uses the
other format, they get errors.
My question: What format should I store the DateTime in?
Thanks,
Rob
I use to "normalize" the date, when I store it in a string. I use the format
'yyyymmdd'.
DateString := FormatDateTime('yyyymmdd', Date);
-> can be stored in Inifile
and back to TDateTime
DateVar := EncodeDate(StrToInt(Copy(DateString, 1, 4)),
StrToInt(Copy(DateString, 5, 2)), StrToInt(Copy(DateString, 7, 2)));
Hope that helped.
Regards, Werner
"Rob Decker" <r...@XXrdcomp.net> schrieb im Newsbeitrag
news:3d2d484d_2@dnews...
In your application you can set the ShortDateFormat global variable to
what you like to use. ShortDateFormat is initiated to the value from
Regional settings, but you are free to set it explicitly.
HTH,
Daniel
HTH,
Daniel
shure this is easier, but I prefer to set ShortDateFormat to a constant
format for screen output only. I don´t like to switch it frequently for
Inistorage and back for screen-output, but that is only my personal flavour.
And by the way: my solution ain´t *that* complicated, is it?
Werner
"Daniel Hansen" <daniel...@gecapital.com> schrieb im Newsbeitrag
news:3D2D655D...@gecapital.com...
I recommend storing DateTime in TIniFiles as a float. --JohnH
It's not easier if you want to store dates to a file in a common format, but
also want to display dates on the screen in the format of the user's local
settings.
Jens
Possible, but hard to read, when you look into the Inifile
Regards, Werner
> My question: What format should I store the DateTime in?
Since you're only storing the date portion and not the time,
why not store it as an integer:
Ini.WriteInteger('Holidays', 'Christmas', Trunc(XMas));
Reading it back:
XMas := Ini.ReadInteger('Holidays', 'Christmas', 0);
Now, no worries about the date format at all anywhere.
Ken
---
Ken White
kwh...@adpsi.com
Clipper Functions for Delphi and C++ Builder
http://www.adpsi.com
Especially if the file you store the dates is ever shared amongst different
machines with different locales.
Mike
Daniel
Daniel
Absolutely not, and after reading more of this thread I conclude that
other (than my) solutions is much better.
Thanks,
Daniel
Has also a locale problem (DecimalSeparator) and is of course not
human-readable as a date.
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
Thanks, I did not know that the locale affected TIniFiles.
Back during the "metric" and decimal revolution why didn't
the French get the world to use the same character for
the decimal point? <g> Regards, JohnH
Just one final note: You are not the only one with this problem,
designers of the libraries have noticed it, too. Anders Hejlsberg and
his team introduced a new format string to the .net framework for
converting binary data (like DateTime or Double) to strings while making
sure it can be converted back no matter what regional settings are used.
In Delphi we still have to do this ourselves, but Delphi .NET is on it's
way.
Jens
Compellingly good reasons! --JohnH
If it is a date, store it as ISO-8601 format, YYYY-MM-DD. Everyone will
recognise it as a date, and will understand it as a date. It can be
converted to a TDateTime, independently of location, by something like
DT := EncodeDate(X(S, 1, 4), X(S, 6, 2), X(S, 9, 2)) ;
with function X(const S : string ; const J, K : integer) : integer ;
var A : integer ;
begin Result := 0 ;
for A := J to J+K-1 do Result := Result*10 + Ord(S[A])-48 ;
end ;
There may be a better way to convert; but that, once debugged, should be
good enough.
You can also store it as an integer; a TDateTime should be an exact
integer for a date, but you can always Round to be sure.
If it is a Date+Time, then either YYYY-MM-DD hh:mm:ss.sss or as a
Double, which can be written either in float or fixed point format,
depending on needs. Or the 8 bytes can be converted to 16 hex
characters, to be certain of exactness.
Never multinationalise if it can be avoided; instead, internationalise.
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.txt
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
And the Germans, too. And why are there different units like 蚓 and 蚌? Why
miles and meters? Probably that we don't get bored... :-)
Jens
Var enYear, enMonth, enDay: Word;
EncodeDate(Date, enYear, enMonth, enDay);
if date has 12/25/2002 then result =
enYear = 2002
enMonth = 12
enDay = 25
Best Regards,
Safari.
"Werner" <wer...@nospam.de> wrote in message
news:agjj7o$gci$01$1...@news.t-online.com...
Don't try this one, Europeans like using comma as a decimal separator.
On StrToFloat it will bark.
If you are only worried about date (not time), you can just store the
integer part of it.
Personally, I'd recommend:
Day=#
Month=#
Year=#
Thanks,
Brett
If Anders can do it for .net, presumably comparatively ordinary people
could do something similar in Delphi - in various ways.
It could be nice if a friend of Anders could produce Delphi code to do
exactly the same conversions, and if it could be properly publicised
(including here, and in FAQs) as matching the Anders standard.
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk / JR.St...@physics.org ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Of course anyone could do this (and I guess some already have done it). I
just meant that in .NET it's part of the framework, and the standard format
string "R" (for round-trip) can be used for a lot of datatypes (int, double,
decimal, DateTime etc.). So if you wait for Delphi .NET (probably you cannot
and don't want to), there is a common way to solve your problem:
Double.ToString("R") works as well as DateTime.ToString("R"), so in Delphi
we would need an overloaded function like
ConvertToString(V: Double, FormatString: String); overload;
ConvertToString(V: TDateTime, FormatString: String); overload; etc.
and a similar function for converting the string back.
> It could be nice if a friend of Anders could produce Delphi code to do
> exactly the same conversions
Of course Borland could introduce such a function. On the other hand they
probably have much work with Delphi .NET (and other stuff), so maybe such a
function is not on top of the to-do list (and for .NET they don't need to
implement such a function since it is part of the framework).
Jens