2012-01-29 17:41:06 +08:00
|
|
|
/* tzset.c: Convert current Windows timezone to POSIX timezone information.
|
|
|
|
|
2014-12-05 21:41:52 +08:00
|
|
|
Copyright 2012, 2013, 2014 Red Hat, Inc.
|
2012-01-29 17:41:06 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2013-04-23 17:44:36 +08:00
|
|
|
#include <inttypes.h>
|
2012-01-29 20:06:23 +08:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <locale.h>
|
2012-01-29 17:41:06 +08:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <cygwin/version.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#ifndef GEOID_NOT_AVAILABLE
|
|
|
|
#define GEOID_NOT_AVAILABLE -1
|
|
|
|
#endif
|
|
|
|
|
2014-12-05 21:41:52 +08:00
|
|
|
/* The auto-generated tzmap.h contains the mapping table from Windows timezone
|
|
|
|
and country per ISO 3166-1 to POSIX timezone. For more info, see the file
|
|
|
|
itself. */
|
|
|
|
#include "tzmap.h"
|
2012-01-29 17:41:06 +08:00
|
|
|
|
|
|
|
#define TZMAP_SIZE (sizeof tzmap / sizeof tzmap[0])
|
|
|
|
|
|
|
|
static struct option longopts[] =
|
|
|
|
{
|
|
|
|
{"help", no_argument, NULL, 'h' },
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static char opts[] = "hV";
|
|
|
|
|
2012-01-29 20:06:23 +08:00
|
|
|
#define REG_TZINFO L"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation"
|
|
|
|
#define REG_TZDB L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"
|
2012-01-29 17:41:06 +08:00
|
|
|
|
|
|
|
static inline HKEY
|
2012-01-29 20:06:23 +08:00
|
|
|
reg_open (HKEY pkey, PCWSTR path, const char *msg)
|
2012-01-29 17:41:06 +08:00
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
HKEY hkey;
|
|
|
|
|
2012-01-29 20:06:23 +08:00
|
|
|
ret = RegOpenKeyExW (pkey, path, 0, KEY_READ, &hkey);
|
2012-01-29 17:41:06 +08:00
|
|
|
if (ret == ERROR_SUCCESS)
|
|
|
|
return hkey;
|
|
|
|
if (msg)
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "%s: cannot open registry %s, error code %" PRIu32 "\n",
|
|
|
|
program_invocation_short_name, msg, (unsigned int) ret);
|
2012-01-29 17:41:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For symmetry */
|
|
|
|
#define reg_close(hkey) RegCloseKey(hkey)
|
|
|
|
|
|
|
|
static inline BOOL
|
2012-01-29 20:06:23 +08:00
|
|
|
reg_query (HKEY hkey, PCWSTR value, PWCHAR buf, DWORD size, const char *msg)
|
2012-01-29 17:41:06 +08:00
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
DWORD type;
|
|
|
|
|
2012-01-29 20:06:23 +08:00
|
|
|
ret = RegQueryValueExW (hkey, value, 0, &type, (LPBYTE) buf, &size);
|
2012-01-29 17:41:06 +08:00
|
|
|
if (ret == ERROR_SUCCESS)
|
|
|
|
return TRUE;
|
|
|
|
if (msg)
|
2013-04-23 17:44:36 +08:00
|
|
|
fprintf (stderr, "%s: cannot query registry %s, error code %" PRIu32 "\n",
|
|
|
|
program_invocation_short_name, msg, (unsigned int) ret);
|
2012-01-29 17:41:06 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline BOOL
|
2012-01-29 20:06:23 +08:00
|
|
|
reg_enum (HKEY hkey, int idx, PWCHAR name, DWORD size)
|
2012-01-29 17:41:06 +08:00
|
|
|
{
|
2012-01-29 20:06:23 +08:00
|
|
|
return RegEnumKeyExW (hkey, idx, name, &size, NULL, NULL, NULL, NULL)
|
2012-01-29 17:41:06 +08:00
|
|
|
== ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage (FILE *stream)
|
|
|
|
{
|
|
|
|
fprintf (stream, ""
|
|
|
|
"Usage: %1$s [OPTION]\n"
|
|
|
|
"\n"
|
|
|
|
"Print POSIX-compatible timezone ID from current Windows timezone setting\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -h, --help output usage information and exit.\n"
|
|
|
|
" -V, --version output version information and exit.\n"
|
|
|
|
"\n"
|
|
|
|
"Use %1$s to set your TZ variable. In POSIX-compatible shells like bash,\n"
|
|
|
|
"dash, mksh, or zsh:\n"
|
|
|
|
"\n"
|
|
|
|
" export TZ=$(%1$s)\n"
|
|
|
|
"\n"
|
|
|
|
"In csh-compatible shells like tcsh:\n"
|
|
|
|
"\n"
|
|
|
|
" setenv TZ `%1$s`\n"
|
|
|
|
"\n", program_invocation_short_name);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_version ()
|
|
|
|
{
|
|
|
|
printf ("tzset (cygwin) %d.%d.%d\n"
|
|
|
|
"POSIX-timezone generator\n"
|
|
|
|
"Copyright (C) 2012 - %s Red Hat, Inc.\n"
|
|
|
|
"This is free software; see the source for copying conditions. There is NO\n"
|
|
|
|
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR / 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MAJOR % 1000,
|
|
|
|
CYGWIN_VERSION_DLL_MINOR,
|
|
|
|
strrchr (__DATE__, ' ') + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
HKEY hkey, skey;
|
2012-01-29 20:06:23 +08:00
|
|
|
WCHAR keyname[256], stdname[256], std2name[256], country[10], *spc;
|
2012-01-29 17:41:06 +08:00
|
|
|
GEOID geo;
|
|
|
|
int opt, idx, gotit = -1;
|
|
|
|
|
2012-01-29 20:06:23 +08:00
|
|
|
setlocale (LC_ALL, "");
|
2012-01-29 17:41:06 +08:00
|
|
|
while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage (stdout);
|
|
|
|
return 0;
|
|
|
|
case 'V':
|
|
|
|
print_version ();
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "Try `%s --help' for more information.\n",
|
|
|
|
program_invocation_short_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
usage (stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First fetch current timezone information from registry. */
|
|
|
|
hkey = reg_open (HKEY_LOCAL_MACHINE, REG_TZINFO, "timezone information");
|
|
|
|
if (!hkey)
|
|
|
|
return 1;
|
|
|
|
/* Vista introduced the TimeZoneKeyName value, which simplifies the
|
|
|
|
job a lot. */
|
2012-01-29 20:06:23 +08:00
|
|
|
if (!reg_query (hkey, L"TimeZoneKeyName", keyname, sizeof keyname, NULL))
|
2012-01-29 17:41:06 +08:00
|
|
|
{
|
|
|
|
/* Pre-Vista we have a lot more to do. First fetch the name of the
|
|
|
|
Standard (non-DST) timezone. If we can't get that, give up. */
|
2012-01-29 20:06:23 +08:00
|
|
|
if (!reg_query (hkey, L"StandardName", stdname, sizeof stdname,
|
2012-01-29 17:41:06 +08:00
|
|
|
"timezone information"))
|
|
|
|
{
|
|
|
|
reg_close (hkey);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
reg_close (hkey);
|
|
|
|
/* Now open the timezone database registry key. Every subkey is a
|
|
|
|
timezone. The key name is what we're after, but to find the right
|
|
|
|
one, we have to compare the name of the previously fetched
|
|
|
|
"StandardName" with the "Std" value in the timezone info... */
|
|
|
|
hkey = reg_open (HKEY_LOCAL_MACHINE, REG_TZDB, "timezone database");
|
|
|
|
if (!hkey)
|
|
|
|
return 1;
|
|
|
|
for (idx = 0; reg_enum (hkey, idx, keyname, sizeof keyname); ++idx)
|
|
|
|
{
|
|
|
|
skey = reg_open (hkey, keyname, NULL);
|
|
|
|
if (skey)
|
|
|
|
{
|
|
|
|
/* ...however, on MUI-enabled machines, the names are not stored
|
|
|
|
directly in the above StandardName, rather it is a resource
|
|
|
|
pointer into tzres.dll. This is stored in MUI_Std.
|
|
|
|
Fortunately it's easy to recognize this situation: If
|
|
|
|
StandardName starts with @, it's a resource pointer, otherwise
|
|
|
|
it's the cleartext value. */
|
2012-01-29 20:06:23 +08:00
|
|
|
ret = reg_query (skey, stdname[0] == L'@' ? L"MUI_Std" : L"Std",
|
2012-01-29 17:41:06 +08:00
|
|
|
std2name, sizeof std2name, NULL);
|
|
|
|
reg_close (skey);
|
2012-01-29 20:06:23 +08:00
|
|
|
if (ret && !wcscmp (stdname, std2name))
|
2012-01-29 17:41:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reg_close (hkey);
|
|
|
|
|
2014-12-05 21:41:52 +08:00
|
|
|
/* We fetch the current Geo-location of the user and convert it to an
|
|
|
|
ISO 3166-1 compatible nation code. */
|
2012-01-29 20:06:23 +08:00
|
|
|
*country = L'\0';
|
2014-12-05 21:41:52 +08:00
|
|
|
geo = GetUserGeoID (GEOCLASS_NATION);
|
|
|
|
if (geo != GEOID_NOT_AVAILABLE)
|
|
|
|
GetGeoInfoW (geo, GEO_ISO2, country, sizeof country, 0);
|
|
|
|
/* If, for some reason, the Geo-location isn't available, we use the locale
|
2012-01-29 17:41:06 +08:00
|
|
|
setting instead. */
|
|
|
|
if (!*country)
|
2012-01-29 20:06:23 +08:00
|
|
|
GetLocaleInfoW (LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME,
|
|
|
|
country, sizeof country);
|
2012-01-29 17:41:06 +08:00
|
|
|
|
|
|
|
/* Now iterate over the mapping table and find the right entry. */
|
|
|
|
for (idx = 0; idx < TZMAP_SIZE; ++idx)
|
|
|
|
{
|
2015-07-08 16:25:49 +08:00
|
|
|
if (!wcscasecmp (keyname, tzmap[idx].win_tzkey))
|
2012-01-29 17:41:06 +08:00
|
|
|
{
|
|
|
|
if (gotit < 0)
|
|
|
|
gotit = idx;
|
2015-07-08 16:25:49 +08:00
|
|
|
if (!wcscasecmp (country, tzmap[idx].country))
|
2012-01-29 17:41:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (gotit >= 0)
|
|
|
|
{
|
|
|
|
idx = gotit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (idx >= TZMAP_SIZE)
|
|
|
|
{
|
|
|
|
if (gotit < 0)
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"%s: can't find matching POSIX timezone for "
|
2012-01-29 20:06:23 +08:00
|
|
|
"Windows timezone \"%ls\"\n",
|
2012-01-29 17:41:06 +08:00
|
|
|
program_invocation_short_name, keyname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
idx = gotit;
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
/* Got one. Print it.
|
|
|
|
Note: The tzmap array is in the R/O data section on x86_64. Don't
|
|
|
|
try to overwrite the space, as the code did originally. */
|
|
|
|
spc = wcschr (tzmap[idx].posix_tzid, L' ');
|
|
|
|
if (!spc)
|
|
|
|
spc = wcschr (tzmap[idx].posix_tzid, L'\0');
|
|
|
|
printf ("%.*ls\n", (int) (spc - tzmap[idx].posix_tzid), tzmap[idx].posix_tzid);
|
2012-01-29 17:41:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|