Board index » delphi » UTC Time
|
Richard A Stevens
Delphi Developer |
|
Richard A Stevens
Delphi Developer |
UTC Time2004-04-20 12:04:06 AM delphi165 I wasn't sure where to post this so I posted it here. How do I take a UTC date FROM THE PAST and convert it into a local date? GetLocalTime() seems to only deal with the current date/time. |
| John Herbster
Delphi Developer |
2004-04-20 12:10:52 AM
Re:UTC Time
"Richard A Stevens" <XXXX@XXXXX.COM>wrote
QuoteHow do I take a UTC date FROM THE PAST and of variable does it use? (2) What is the most historic date that you are interested in? (3) What do you mean by "local date" and what type of variable should the output use? Regards, JohnH |
| Richard A Stevens
Delphi Developer |
2004-04-20 01:12:51 AM
Re:UTC Time
(1) Currently dates are in a TDateTime variable.
(2) Two years in the past. (3) Converting past UTC times into a local timezone time including daylight savings bias. i.e. UTC time to Pacific Standard Time or Pacific Daylight Time, whatever the case may be. _____________________ "John Herbster" <herb-sci1_AT_sbcglobal.net>writes Quote
|
| John Kaster (Borland)
Delphi Developer |
2004-04-20 01:35:40 AM
Re:UTC Time
Richard A Stevens in <4083f7ed$XXXX@XXXXX.COM>writes:
QuoteI wasn't sure where to post this so I posted it here. ugly it is?), so I am sure some here will probably attack the code. But, the bottom line is, it is been working for me. Delphi 7 code. const RSSDateFormat = 'ddd, dd mmm yyyy hh:nn:ss ''GMT'''; function GetTimeZoneBias: Integer; var TimeZoneInfo: TTimeZoneInformation; begin case GetTimeZoneInformation(TimeZoneInfo) of TIME_ZONE_ID_STANDARD: Result := TimeZoneInfo.Bias + TimeZoneInfo.StandardBias; TIME_ZONE_ID_DAYLIGHT: Result := TimeZoneInfo.Bias + TimeZoneInfo.DaylightBias; else Result := 0; end; end; function GetUTC: TDateTime; var stim: SYSTEMTIME; begin GetSystemTime(stim); result:=SystemTimeToDateTime(stim); end; function GetUTCDelta: TDateTime; begin Result := Now - GetUTC; end; function DateTimeToRSSTime(Value: TDateTime; ApplyLocalBias: Boolean = True): WideString; var Bias: Integer; begin Bias := GetTimeZoneBias; if (Bias <>0) and ApplyLocalBias then Value := Value - GetUTCDelta; Result := FormatDateTime(RSSDateFormat, Value); { Do not localize } end; function RSSTimeToDateTime(Value: string; ApplyLocalBias: Boolean = True): TDateTime; var Bias: Integer; y, m, d, h, min, s: word; begin // ddd, dd mmm yyyy hh:nn:ss GMT // 1234567890123456789012345 y := StrToInt(Copy(Value, 13, 4)); m := Pos(LowerCase(Copy(Value, 9, 3)), 'jan feb mar apr may jun jul aug sep oct nov dec') // do not localize div 4 + 1; d := StrToInt(Copy(Value, 6, 2)); h := StrToInt(Copy(Value, 18, 2)); min := StrToInt(Copy(Value, 21, 2)); s := StrToInt(Copy(Value, 24, 2)); Result := EncodeDateTime(y, m, d, h, min, s, 0); Bias := GetTimeZoneBias; if (Bias <>0) and ApplyLocalBias then Result := Result + GetUTCDelta; end; -- John Kaster, Borland Developer Relations, bdn.borland.com Add a feature/Fix a bug: qc.borland.com Get source: cc.borland.com |
| John Herbster
Delphi Developer |
2004-04-20 01:37:40 AM
Re:UTC Time
"Richard A Stevens" <XXXX@XXXXX.COM>wrote
Quote(1) Currently dates are in a TDateTime variable. then and there. (Please correct me if I am wrong!) I suspect that the Windows SDK or someone has a neat routine for converting historical "UTC" times to historical wall calendar and clock dates and times based on a specific TIME_ZONE_INFORMATION record that is assumed to have applied in the past. While I have not done exactly this yet, you should be able to get and interpret the fields in the TIME_ZONE_INFORMATION record and apply the offset to your historical "UTC" time to make the wall time. Regards, JohnH |
| Richard A Stevens
Delphi Developer |
2004-04-20 03:02:47 AM
Re:UTC Time
John:
That's exactly what I want to do. Daylight Bias, as I understand it, only deals with the current system time, not whether a date in the past had any Daylight Bias. Rich "John Herbster" <herb-sci1_AT_sbcglobal.net>writes Quote
|
| Tjipke A. van der Plaats
Delphi Developer |
2004-04-20 03:30:47 AM
Re:UTC Time
Richard A Stevens writes:
QuoteI wasn't sure where to post this so I posted it here. In the first one, the FileTimeToLocalTime is the real call, the others are just for getting there from TDateTime and getting back to TDateTime. In the second one, you need to pass info about the timezone, but it only works on NT. O, and one thing to note: it show the time in relation to the current time. So a time in the past, for example before the last daylightsaving time started will show off by one...: So when it was in fact 8:00 in februari it will now show as 9:00 (just as NT/XP is showing times from files in the past). function UTCToLocalTime(aUTCTime: TDateTime; aDontConvertIfTimeIsNull: Boolean=False): TDateTime; var LT, UT: TSystemTime; FileTime, LocalFileTime: TFileTime; begin DateTimeToSystemTime(aUTCTime, UT); if aDontConvertIfTimeIsNull and (UT.wHour=0) and (UT.wMinute=0) and (UT.wSecond=0) and (UT.wMilliseconds=0) then begin Result := aUTCTime; Exit; end; if not SystemTimeToFileTime(UT, FileTime) then RaiseLastOSError; if not FileTimeToLocalFileTime(FileTime, LocalFileTime) then RaiseLastOSError; if not FileTimeToSystemTime(LocalFileTime, LT) then RaiseLastOSError; Result := SystemTimeToDateTime(LT); end; function UTCToLocalTime(aUTCTime: TDateTime; lpTimeZoneInformation: PTimeZoneInformation): TDateTime; var LT, UT: TSystemTime; begin // function works only on NT! DateTimeToSystemTime(aUTCTime, UT); if not SystemTimeToTzSpecificLocalTime(lpTimeZoneInformation, UT, LT) then RaiseLastOSError; Result := SystemTimeToDateTime(LT); end; Regards, Tjipke van der Plaats -- Update your version information in your project from the command line? Use ChangeRes! See www.tiriss.com/changeres |
| Arthur Hoornweg
Delphi Developer |
2004-04-20 02:31:51 PM
Re:UTC Time
John Kaster (Borland) writes:
....... John, this won't work, because your "GetTimeZoneBias" and GetUTCDelta functions give the *current* timezone/ DST bias, not that of the two years old tdatetime that Richard is trying to convert. One needs to determine the bias for the specific date! -- Arthur Hoornweg (please remove the ".net" from my e-mail address) |
| Dr John Stockton
Delphi Developer |
2004-04-20 06:10:47 PM
Re:UTC Time
JRS: In article <4084080b$XXXX@XXXXX.COM>, seen in news:borla
nd.public.delphi.non-technical, Richard A Stevens <XXXX@XXXXX.COM> posted at Mon, 19 Apr 2004 10:12:51 : Quote>"Richard A Stevens" <XXXX@XXXXX.COM>wrote location, but that is no great help. You need the past rules for the desired local location. You do not say whether the current location will always be PST/PDT, or whether it will always be USA, or whether it may be anywhere; that matters. The US rules are well-defined, but odd parts of the US have no Summer Time. The CA ones are non-identical. The algorithm is to take the UTC year, consider it as the destination year (It will be, except for southern hemisphere destinations for a fraction of a day, which does not ultimately matter), then calculate the local transition date/times for that year by local rules, then convert those to UTC, and compare. Now you know whether it is summer or winter at the UTC in question. Adjust by the local winter offset, and if needed by the clock change (which is one hour almost everywhere). My <URL:www.merlyn.demon.co.uk/uksumtim.htm>discusses Summer Time, for the UK, the EU, and other places that might be of interest; <URL:www.merlyn.demon.co.uk/js-date3.htm>and <URL:www.merlyn.demon.co.uk/js-date5.htm>have code in javascript. <URL:www.merlyn.demon.co.uk/programs/dateprox.pas> has Pascal for calculating the EU change dates, adaptable no doubt for NA. Indeed, js-date5.htm#Demo will do the job requested (so can be used as a check) if you select NY then change EST5EDT to PST8PDT. -- ?John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ? Web <URL:www.merlyn.demon.co.uk/>- w. FAQish topics, links, acronyms PAS EXE etc : <URL:www.merlyn.demon.co.uk/programs/>- see 00index.htm Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc. |
| John Wester [Group W]
Delphi Developer |
2004-04-20 09:42:08 PM
Re:UTC Time
In article <4083f7ed$XXXX@XXXXX.COM>, XXXX@XXXXX.COM
says... QuoteI wasn't sure where to post this so I posted it here. around with dates. -- John Life is complex. It has real and imaginary parts |
| Richard A Stevens
Delphi Developer |
2004-04-20 10:34:53 PM
Re:UTC Time
Thanks Arthur. I see you understand what I am trying to do..
Richard Stevens _____________________ "Arthur Hoornweg" <XXXX@XXXXX.COM>writes QuoteJohn Kaster (Borland) writes: |
| John Kaster (Borland)
Delphi Developer |
2004-04-21 12:14:26 AM
Re:UTC Time
Arthur Hoornweg in <4084c3d2$XXXX@XXXXX.COM>writes:
QuoteJohn, this won't work, because your "GetTimeZoneBias" -- John Kaster, Borland Developer Relations, bdn.borland.com Add a feature/Fix a bug: qc.borland.com Get source: cc.borland.com |
| John Herbster
Delphi Developer |
2004-04-21 03:05:23 AM
Re:UTC TimeQuote>You want to convert these past "UTC" times to be what proposed, you might find this Google search interesting www.google.com/search There may be some Internet accessible tables and some programming components available for helping with the historical corrections. Regards, JohnH |
| Kirk Halgren
Delphi Developer |
2004-04-28 08:33:37 PM
Re:UTC Time
"Dr John Stockton" <XXXX@XXXXX.COM>writes
<snip> QuoteWindows will give the present Summer Time rules for the current daylight saving time didn't even begin until WWI. The portion of Indiana which is in the Eastern Time Zone refrains from DST, but certain counties on the border with neighboring states use it. IIRC, the entire US stayed on it for two years during the Nixon administration, due to the oil shortage. I think that Arizona does not observe it, but oddly the Native Americans do. See: webexhibits.org/daylightsaving/g.html Calculating leap years is trivial compared to this. Did you know that Windows 3.1 could never become Y2K compliant because the MS programmers just assumed that 2000 is not a leap year? Most centuries are not, but any divisible by 4 are. They thought they knew the details, but didn't look them up. Kirk Halgren "One word sums up probably the responsibility of any Governor, and that one word is 'to be prepared'." -- George W. Bush |
| Wayne Niddery [TeamB]
Delphi Developer |
2004-04-28 11:39:33 PM
Re:UTC Time
Kirk Halgren writes:
Quote
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com) RADBooks: www.logicfundamentals.com/RADBooks.html "True peace is not the absence of tension, but the presence of justice." - Martin Luther King, Jr. |
