chcę zrobic aplikację wyświetlającą czas w nowym jorku niezależnie od miejsca uruchomienia programu (czyli dodanie godzin nie wchodzi w grę..)
Jedyne wyjście to sprawdzenie w jakiej strefie czasowej znajduje się komp na którym uruchamiany jest twój program i na tej postawie określenie koniecznej poprawki (oczywiście wszystkie strefy względem GMT). Informację o strefie czasowej można wydębić z systemu poprzez WinApi, ale:
- Często strefa czasowa jest nie ustawiona w systemie
- Strefa czasowa jest błędnie ustawiona
3.Trzebaby przekopać się przez help, żeby wynaleźć odpowiednią funkcję (makro?)
Mozliwosci są 3: hmm...
1: Patrz wyżej (w sumie jest to jedyne proste rozwiązanie ale moze być błąd wynikający ze złego ustawienia strefy czasowej na kompie oraz ze złego ustawienia zegarka);
2: W necie mozna znaleŹĆ programy korzystające z tzw serwerów czasu, być moze są takze serwery czasu które udostępniają dane dotyczące (baaardzo dokładnej) godziny w danym miejscy na ziemi.
3: Rozwiązanie najtrudniejsze i najdrorzsze, wymagające wielkiej wiedzy z pozomu elektroniki i programowania, ale dające najlepsze wyniki - budowa odbiornika GPS (to wbrew pozorom jest mozliwe w warunkach amatorskich). Taki odbiornik umozliwia okreslenie połozenia na ziemi (a wiec strefy czasowej w jakiej znajduje sie odbiornik) i o ile sie nie myle umozliwia okreslenie baaardzo dokładnego czasu w innym miejscu na ziemi (np w Nowym Yorku).
PS. Co do tego GPS to chyba przesyłana jest informacja o aktualnym czasie ale nie jestem tego w 100% pewien.
{browar}
Z GPS jest jak mówisz , przesyłany jest bardzo dokładny czas. Na postawie bowiem różnic czasu przebiegu sygnału od i do odbiornika przy transmisji z czterema satelitami i na podstawie znajomości ich położenia określa się położenie odbiornika GPS. Konieczna jest przy tym synchronizacja pomiędzy zegarem odbiornika i zegarem jednego z satelitów.
Ale tu chodzi o to by program przełany netem do miejsca X wiedział co pokazać, jako czas NEW York :)--Michał
TJS group
delphi 5,6
Witam
Oto Gotowe rozwiązanie życze powodzenia................................... {brawo} {brawo} {brawo} {brawo} {brawo} {brawo} {brawo} {brawo} {brawo}
--- Source: TimeZone.pas ---
unit TimeZone;
interface
uses
Windows, Classes;
-
Get delta added to date/time
function DateTimeDelta(DateTime, Delta: TDateTime): TDateTime; -
Get standard time difference with UTC based on specified time-zone
function GetTzStandardDelta(TzInfo: TTimeZoneInformation): TDateTime; -
Get daylight time difference with UTC based on specified time-zone
function GetTzDaylightDelta(TzInfo: TTimeZoneInformation): TDateTime; -
Get time difference with UTC based on current time-zone
function GetTimeZoneDelta: TDateTime; -
Get current UTC time
function GetUniversalTime: TDateTime; -
Get current local time (same as Now)
function GetLocalTime: TDateTime; -
Get UTC from local time based on current time-zone
function LocalToUniversalTime(const DateTime: TDateTime): TDateTime; -
Get local time from UTC based on current time-zone
function UniversalToLocalTime(const DateTime: TDateTime): TDateTime; -
Get start of standard time in specified time-zone
function GetTzStandardDate(const TzInfo: TTimeZoneInformation): TDateTime; -
Get start of daylight time in specified time-zone
function GetTzDaylightDate(const TzInfo: TTimeZoneInformation): TDateTime; -
Check if in daylight time
function IsDaylightTime: Boolean; -
Check if date/time is in daylight time
function IsTzSpecificDaylightTime(const TzInfo: TTimeZoneInformation; const
DateTime: TDateTime): Boolean; -
Get local time from UTC based on specified time-zone
function UniversalToTzSpecificTime(const TzInfo: TTimeZoneInformation; const
DateTime: TDateTime): TDateTime; -
Get local time from UTC based on specified time-zone (Windows NT only)
function UniversalToTzSpecificTime_NT(const TzInfo: TTimeZoneInformation;
const DateTime: TDateTime): TDateTime; -
Get list of names of known time-zones
procedure GetTimeZoneList(const List: TStrings); -
Get time-zone information of specified time-zone name
procedure GetTzSpecificInformation(const TimeZone: String;
var DisplayName: String; MapID: String; var TzInfo: TTimeZoneInformation); -
Get list of current time in all time-zones
procedure GetWorldClock(List: TStrings);
implementation
uses
SysUtils, Registry;
-
Get delta added to date/time
function DateTimeDelta(DateTime, Delta: TDateTime): TDateTime;
begin
if DateTime < 0 then
Result := DateTime - Delta
else
Result := DateTime + Delta;
end; -
Get standard time difference with UTC based on specified time-zone
function GetTzStandardDelta(TzInfo: TTimeZoneInformation): TDateTime;
begin
Result := (TzInfo.Bias + TzInfo.StandardBias) / 1440;
end; -
Get daylight time difference with UTC based on specified time-zone
function GetTzDaylightDelta(TzInfo: TTimeZoneInformation): TDateTime;
begin
Result := (TzInfo.Bias + TzInfo.DayLightBias) / 1440;
end; -
Get time difference with UTC based on current time-zone
function GetTimeZoneDelta: TDateTime;
var
TzInfo: TTimeZoneInformation;
begin
case GetTimeZoneInformation(TzInfo) of
TIME_ZONE_ID_UNKNOWN: Result := TzInfo.Bias / 1440;
TIME_ZONE_ID_STANDARD: Result := GetTzStandardDelta(TzInfo);
TIME_ZONE_ID_DAYLIGHT: Result := GetTzDaylightDelta(TzInfo);
else
raise Exception.Create(SysErrorMessage(GetLastError));
end;
end; -
Get current UTC time
function GetUniversalTime: TDateTime;
var
SystemTime: TSystemTime;
begin
GetSystemTime(SystemTime);
Result := SystemTimeToDateTime(SystemTime);
end; -
Get current local time (same as Now)
function GetLocalTime: TDateTime;
var
LocalTime: TSystemTime;
begin
Windows.GetLocalTime(LocalTime);
Result := SystemTimeToDateTime(LocalTime);
end; -
Get UTC from local time based on current time-zone
function LocalToUniversalTime(const DateTime: TDateTime): TDateTime;
begin
Result := DateTimeDelta(DateTime, GetTimeZoneDelta);
end; -
Get local time from UTC based on current time-zone
function UniversalToLocalTime(const DateTime: TDateTime): TDateTime;
begin
Result := DateTimeDelta(DateTime, -GetTimeZoneDelta);
end; -
Get start of standard time in specified time-zone
function GetTzStandardDate(const TzInfo: TTimeZoneInformation): TDateTime;
var
SystemTime, LocalTime: TSystemTime;
begin
if TzInfo.StandardDate.wMonth = 0 then
raise Exception.Create('No start of standard time defined');
LocalTime := TzInfo.StandardDate;
if LocalTime.wYear > 0 then
Result := SystemTimeToDateTime(LocalTime)
else
begin
GetSystemTime(SystemTime);
LocalTime.wYear := SystemTime.wYear;
if LocalTime.wDay < 5 then
begin
LocalTime.wDay := (LocalTime.wDay - 1) * 7 + 1;
Result := SystemTimeToDateTime(LocalTime);
end
else
begin
LocalTime.wMonth := (LocalTime.wMonth mod 12) + 1;
LocalTime.wDay := 1;
Result := SystemTimeToDateTime(LocalTime) - 7;
end;
Result := Result + (7 - Abs(Trunc(Result) - 1) mod 7 +
LocalTime.wDayOfWeek) mod 7;
end;
end;
// Get start of daylight time in specified time-zone
function GetTzDaylightDate(const TzInfo: TTimeZoneInformation): TDateTime;
var
SystemTime, LocalTime: TSystemTime;
begin
if TzInfo.DaylightDate.wMonth = 0 then
raise Exception.Create('No start of daylight time defined');
LocalTime := TzInfo.DaylightDate;
if LocalTime.wYear > 0 then
Result := SystemTimeToDateTime(LocalTime)
else
begin
GetSystemTime(SystemTime);
LocalTime.wYear := SystemTime.wYear;
if LocalTime.wDay < 5 then
begin
LocalTime.wDay := (LocalTime.wDay - 1) * 7 + 1;
Result := SystemTimeToDateTime(LocalTime);
end
else
begin
LocalTime.wMonth := (LocalTime.wMonth mod 12) + 1;
LocalTime.wDay := 1;
Result := SystemTimeToDateTime(LocalTime) - 7;
end;
Result := Result + (7 - Abs(Trunc(Result) - 1) mod 7 +
LocalTime.wDayOfWeek) mod 7;
end;
end;
-
Check if in daylight time
function IsDaylightTime: Boolean;
var
TzInfo: TTimeZoneInformation;
begin
Result := (GetTimeZoneInformation(TzInfo) = TIME_ZONE_ID_DAYLIGHT);
end; -
Check if date/time is in daylight time
function IsTzSpecificDaylightTime(const TzInfo: TTimeZoneInformation; const
DateTime: TDateTime): Boolean;
var
StandardDate, DaylightDate: TDateTime;
begin
if (TzInfo.StandardDate.wMonth = 0) or (TzInfo.DaylightDate.wMonth = 0)
then
Result := False
else
begin
StandardDate := GetTzStandardDate(TzInfo);
DaylightDate := GetTzDaylightDate(TzInfo);
Result := (StandardDate < DaylightDate) xor
((DateTimeDelta(DateTime, -GetTzDaylightDelta(TzInfo)) < StandardDate)
or
(DateTimeDelta(DateTime, -GetTzStandardDelta(TzInfo)) >=
DaylightDate));
end;
end; -
Get local time from UTC based on specified time-zone
function UniversalToTzSpecificTime(const TzInfo: TTimeZoneInformation; const
DateTime: TDateTime): TDateTime;
var
StandardDate, DaylightDate, Result2: TDateTime;
begin
if (TzInfo.StandardDate.wMonth = 0) or (TzInfo.DaylightDate.wMonth = 0)
then
Result := DateTimeDelta(DateTime, -GetTzStandardDelta(TzInfo))
else
begin
Result := DateTimeDelta(DateTime, -GetTzStandardDelta(TzInfo));
Result2 := DateTimeDelta(DateTime, -GetTzDaylightDelta(TzInfo));
StandardDate := GetTzStandardDate(TzInfo);
DaylightDate := GetTzDaylightDate(TzInfo);
if (StandardDate < DaylightDate) xor
((Result2 < StandardDate) or (Result >= DaylightDate)) then
Result := Result2;
end;
end; -
Get local time from UTC based on specified time-zone (Windows NT only)
function UniversalToTzSpecificTime_NT(const TzInfo: TTimeZoneInformation;
const DateTime: TDateTime): TDateTime;
var
SystemTime, LocalTime: TSystemTime;
begin
DateTimeToSystemTime(DateTime, SystemTime);
if SystemTimeToTzSpecificLocalTime(@TzInfo, SystemTime, LocalTime) then
Result := SystemTimeToDateTime(LocalTime)
else
raise Exception.Create(SysErrorMessage(GetLastError));
end; -
Get list of names of known time-zones
procedure GetTimeZoneList(const List: TStrings);
var
R: TRegistry;
begin
R := TRegistry.Create;
try
R.RootKey := HKEY_LOCAL_MACHINE;
if R.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Time
Zones') then
try
R.GetKeyNames(List);
finally
R.CloseKey;
end
else
raise Exception.Create('Unable to retreive time-zones');
finally
R.Free;
end;
end; -
Get time-zone information of specified time-zone name
procedure GetTzSpecificInformation(const TimeZone: String;
var DisplayName: String; MapID: String; var TzInfo: TTimeZoneInformation);
var
R: TRegistry;
StandardName, DaylightName: WideString;
TZI: record
Bias, StandardBias, DaylightBias: LongInt;
StandardDate, DaylightDate: TSystemTime;
end;
Len: Integer;
begin
R := TRegistry.Create;
try
R.RootKey := HKEY_LOCAL_MACHINE;
if R.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Time
Zones\' + TimeZone) then
try
DisplayName := R.ReadString('Display');
MapID := R.ReadString('MapID');
StandardName := R.ReadString('Std');
DaylightName := R.ReadString('Dlt');
R.ReadBinaryData('TZI', TZI, SizeOf(TZI));
FillChar(TzInfo, SizeOf(TzInfo), #0);
TzInfo.Bias := TZI.Bias;
Len := Length(StandardName) * 2;
if Len > SizeOf(TzInfo.StandardName) then
Len := SizeOf(TzInfo.StandardName);
Move(PWideChar(StandardName), TzInfo.StandardName, Len);
TzInfo.StandardDate := TZI.StandardDate;
TzInfo.StandardBias := TZI.StandardBias;
Len := Length(DaylightName) * 2;
if Len > SizeOf(TzInfo.DaylightName) then
Len := SizeOf(TzInfo.DaylightName);
Move(PWideChar(DaylightName), TzInfo.DaylightName, Len);
TzInfo.DaylightDate := TZI.DaylightDate;
TzInfo.DaylightBias := TZI.DaylightBias;
finally
R.CloseKey;
end
else
raise Exception.Create('Unknown time-zone');
finally
R.Free;
end;
end;
// Get list of current time in all time-zones
procedure GetWorldClock(List: TStrings);
var
TzList: TStrings;
UTC: TDateTime;
i: Integer;
DisplayName, MapID: String;
TzInfo: TTimeZoneInformation;
begin
List.Clear;
UTC := GetUniversalTime;
List.Add('UTC: ' + DateTimeToStr(UTC));
TzList := TStringList.Create;
try
GetTimeZoneList(TzList);
for i := 0 to TzList.Count - 1 do
begin
GetTzSpecificInformation(TzList[i], DisplayName, MapID, TzInfo);
List.Add(TzList[i] + ': ' + DateTimeToStr(
UniversalToTzSpecificTime(TzInfo, UTC)));
end;
finally
TzList.Free;
end;
end;
end.
--Cbmqenjvnz :-)
jmlfgxvpu xgóeml bqfmlsehwš jvnqbzbć....
FRQGF