mirror of
https://github.com/dlang/phobos.git
synced 2025-05-12 07:08:48 +03:00
Use new std.windows.registry in std.datetime.
This commit is contained in:
parent
5dbc7371cc
commit
cce3e38388
1 changed files with 40 additions and 111 deletions
131
std/datetime.d
131
std/datetime.d
|
@ -129,9 +129,7 @@ version(Windows)
|
||||||
{
|
{
|
||||||
import core.sys.windows.windows;
|
import core.sys.windows.windows;
|
||||||
import std.c.windows.winsock;
|
import std.c.windows.winsock;
|
||||||
|
import std.windows.registry;
|
||||||
//For system call to access the registry.
|
|
||||||
pragma(lib, "advapi32.lib");
|
|
||||||
}
|
}
|
||||||
else version(Posix)
|
else version(Posix)
|
||||||
{
|
{
|
||||||
|
@ -30129,99 +30127,45 @@ else version(Windows)
|
||||||
|
|
||||||
static immutable(WindowsTimeZone) getTimeZone(string name)
|
static immutable(WindowsTimeZone) getTimeZone(string name)
|
||||||
{
|
{
|
||||||
auto keyStr = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\0";
|
scope baseKey = Registry.localMachine.getKey(`Software\Microsoft\Windows NT\CurrentVersion\Time Zones`);
|
||||||
HKEY baseKey;
|
|
||||||
|
|
||||||
|
foreach (tzKeyName; baseKey.keyNames)
|
||||||
{
|
{
|
||||||
auto result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyStr.ptr, 0, KEY_READ, &baseKey);
|
if (tzKeyName != name)
|
||||||
if(result != ERROR_SUCCESS)
|
continue;
|
||||||
throw new DateTimeException(format("Failed to open registry. Error: %s", result));
|
|
||||||
}
|
|
||||||
scope(exit) RegCloseKey(baseKey);
|
|
||||||
|
|
||||||
char[1024] keyName;
|
scope tzKey = baseKey.getKey(tzKeyName);
|
||||||
auto nameLen = to!DWORD(keyName.length);
|
|
||||||
int result;
|
|
||||||
for(DWORD index = 0;
|
|
||||||
(result = RegEnumKeyExA(baseKey, index, keyName.ptr, &nameLen, null, null, null, null)) != ERROR_NO_MORE_ITEMS;
|
|
||||||
++index, nameLen = keyName.length)
|
|
||||||
{
|
|
||||||
if(result == ERROR_SUCCESS)
|
|
||||||
{
|
|
||||||
HKEY tzKey;
|
|
||||||
if(RegOpenKeyExA(baseKey, keyName.ptr, 0, KEY_READ, &tzKey) == ERROR_SUCCESS)
|
|
||||||
{
|
|
||||||
scope(exit) RegCloseKey(tzKey);
|
|
||||||
char[1024] strVal;
|
|
||||||
auto strValLen = to!DWORD(strVal.length);
|
|
||||||
|
|
||||||
bool queryStringValue(string name, size_t lineNum = __LINE__)
|
scope stdVal = tzKey.getValue("Std");
|
||||||
{
|
auto stdName = stdVal.value_SZ;
|
||||||
strValLen = strVal.length;
|
|
||||||
|
|
||||||
return RegQueryValueExA(tzKey, name.ptr, null, null, cast(ubyte*)strVal.ptr, &strValLen) == ERROR_SUCCESS;
|
scope dstVal = tzKey.getValue("Dlt");
|
||||||
}
|
auto dstName = dstVal.value_SZ;
|
||||||
|
|
||||||
if(to!string(keyName.ptr) == name)
|
scope tziVal = tzKey.getValue("TZI");
|
||||||
{
|
auto binVal = tziVal.value_BINARY;
|
||||||
if(queryStringValue("Std\0"))
|
assert(binVal.length == REG_TZI_FORMAT.sizeof);
|
||||||
{
|
auto tziFmt = cast(REG_TZI_FORMAT*)binVal.ptr;
|
||||||
//Cannot use to!wstring(char*), probably due to bug http://d.puremagic.com/issues/show_bug.cgi?id=5016
|
|
||||||
static wstring conv(char* cstr, size_t strValLen)
|
|
||||||
{
|
|
||||||
cstr[strValLen - 1] = '\0';
|
|
||||||
|
|
||||||
string retval;
|
|
||||||
|
|
||||||
for(;; ++cstr)
|
|
||||||
{
|
|
||||||
if(*cstr == '\0')
|
|
||||||
break;
|
|
||||||
|
|
||||||
retval ~= *cstr;
|
|
||||||
}
|
|
||||||
|
|
||||||
return to!wstring(retval);
|
|
||||||
}
|
|
||||||
|
|
||||||
//auto stdName = to!wstring(strVal.ptr);
|
|
||||||
auto stdName = conv(strVal.ptr, strValLen);
|
|
||||||
|
|
||||||
if(queryStringValue("Dlt\0"))
|
|
||||||
{
|
|
||||||
//auto dstName = to!wstring(strVal.ptr);
|
|
||||||
auto dstName = conv(strVal.ptr, strValLen);
|
|
||||||
|
|
||||||
enum tzi = "TZI\0";
|
|
||||||
REG_TZI_FORMAT binVal;
|
|
||||||
auto binValLen = to!DWORD(REG_TZI_FORMAT.sizeof);
|
|
||||||
|
|
||||||
if(RegQueryValueExA(tzKey, tzi.ptr, null, null, cast(ubyte*)&binVal, &binValLen) == ERROR_SUCCESS)
|
|
||||||
{
|
|
||||||
TIME_ZONE_INFORMATION tzInfo;
|
TIME_ZONE_INFORMATION tzInfo;
|
||||||
|
|
||||||
auto stdNameLen = stdName.length > 32 ? 32 : stdName.length;
|
auto wstdName = toUTF16(stdName);
|
||||||
auto dstNameLen = dstName.length > 32 ? 32 : dstName.length;
|
auto wdstName = toUTF16(dstName);
|
||||||
|
auto wstdNameLen = wstdName.length > 32 ? 32 : wstdName.length;
|
||||||
|
auto wdstNameLen = wdstName.length > 32 ? 32 : wdstName.length;
|
||||||
|
|
||||||
tzInfo.Bias = binVal.Bias;
|
tzInfo.Bias = tziFmt.Bias;
|
||||||
tzInfo.StandardName[0 .. stdNameLen] = stdName[0 .. stdNameLen];
|
tzInfo.StandardName[0 .. wstdNameLen] = wstdName[0 .. wstdNameLen];
|
||||||
tzInfo.StandardName[stdNameLen .. $] = '\0';
|
tzInfo.StandardName[wstdNameLen .. $] = '\0';
|
||||||
tzInfo.StandardDate = binVal.StandardDate;
|
tzInfo.StandardDate = tziFmt.StandardDate;
|
||||||
tzInfo.StandardBias = binVal.StandardBias;
|
tzInfo.StandardBias = tziFmt.StandardBias;
|
||||||
tzInfo.DaylightName[0 .. dstNameLen] = dstName[0 .. dstNameLen];
|
tzInfo.DaylightName[0 .. wdstNameLen] = wdstName[0 .. wdstNameLen];
|
||||||
tzInfo.DaylightName[dstNameLen .. $] = '\0';
|
tzInfo.DaylightName[wdstNameLen .. $] = '\0';
|
||||||
tzInfo.DaylightDate = binVal.DaylightDate;
|
tzInfo.DaylightDate = tziFmt.DaylightDate;
|
||||||
tzInfo.DaylightBias = binVal.DaylightBias;
|
tzInfo.DaylightBias = tziFmt.DaylightBias;
|
||||||
|
|
||||||
return new WindowsTimeZone(name, tzInfo);
|
return new WindowsTimeZone(name, tzInfo);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new DateTimeException(format("Failed to find time zone: %s", name));
|
throw new DateTimeException(format("Failed to find time zone: %s", name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30229,27 +30173,12 @@ else version(Windows)
|
||||||
{
|
{
|
||||||
auto timezones = appender!(string[])();
|
auto timezones = appender!(string[])();
|
||||||
|
|
||||||
auto keyStr = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones";
|
scope baseKey = Registry.localMachine.getKey(`Software\Microsoft\Windows NT\CurrentVersion\Time Zones`);
|
||||||
HKEY baseKey;
|
|
||||||
|
|
||||||
|
foreach (tzKeyName; baseKey.keyNames)
|
||||||
{
|
{
|
||||||
auto result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyStr.ptr, 0, KEY_READ, &baseKey);
|
timezones.put(tzKeyName);
|
||||||
if(result != ERROR_SUCCESS)
|
|
||||||
throw new DateTimeException(format("Failed to open registry. Error: %s", result));
|
|
||||||
}
|
}
|
||||||
scope(exit) RegCloseKey(baseKey);
|
|
||||||
|
|
||||||
char[1024] keyName;
|
|
||||||
auto nameLen = to!DWORD(keyName.length);
|
|
||||||
int result;
|
|
||||||
for(DWORD index = 0;
|
|
||||||
(result = RegEnumKeyExA(baseKey, index, keyName.ptr, &nameLen, null, null, null, null)) != ERROR_NO_MORE_ITEMS;
|
|
||||||
++index, nameLen = keyName.length)
|
|
||||||
{
|
|
||||||
if(result == ERROR_SUCCESS)
|
|
||||||
timezones.put(to!string(keyName.ptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
sort(timezones.data);
|
sort(timezones.data);
|
||||||
|
|
||||||
return timezones.data;
|
return timezones.data;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue