回复
(零):要探索JDK的核心底层源码,那必须掌握native用法(二)
Bald_eagle
发布于 2022-6-7 17:08
浏览
0收藏
了解不同系统下findJavaTZ_md方法执行
windows系统
/*
* Detects the platform time zone which maps to a Java time zone ID.
*/
char *findJavaTZ_md(const char *java_home_dir, const char *country)
{
char winZoneName[MAX_ZONE_CHAR];
char winMapID[MAX_MAPID_LENGTH];
char *std_timezone = NULL;
int result;
winMapID[0] = 0;
result = getWinTimeZone(winZoneName, winMapID);
if (result != VALUE_UNKNOWN) {
if (result == VALUE_GMTOFFSET) {
std_timezone = _strdup(winZoneName);
} else {
std_timezone = matchJavaTZ(java_home_dir, result,
winZoneName, winMapID, country);
}
}
return std_timezone;
}
注释写得很清楚,获取“Time Zones”注册表中的当前时区
/*
* Gets the current time zone entry in the "Time Zones" registry.
*/
static int getWinTimeZone(char *winZoneName, char *winMapID)
{
...
}
时区的设置方式:
如图
那时区上的选择值是从哪取到的,上面有说了,是在注册表中取值
打开注册表 :Regedit-->
计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\
unix相关的平台
findJavaTz_md()方法的注释上写得很清楚了:将平台时区ID映射为Java时区ID
/*
* findJavaTZ_md() maps platform time zone ID to Java time zone ID
* using <java_home>/lib/tzmappings. If the TZ value is not found, it
* trys some libc implementation dependent mappings. If it still
* can't map to a Java time zone ID, it falls back to the GMT+/-hh:mm
* form. `country', which can be null, is not used for UNIX platforms.
*/
/*ARGSUSED1*/
char *
findJavaTZ_md(const char *java_home_dir, const char *country)
{
char *tz;
char *javatz = NULL;
char *freetz = NULL;
tz = getenv("TZ");
#ifdef __linux__
if (tz == NULL) {
#else
#ifdef __solaris__
if (tz == NULL || *tz == '\0') {
#endif
#endif
tz = getPlatformTimeZoneID();
freetz = tz;
}
/*
* Remove any preceding ':'
*/
if (tz != NULL && *tz == ':') {
tz++;
}
#ifdef __solaris__
if (strcmp(tz, "localtime") == 0) {
tz = getSolarisDefaultZoneID();
freetz = tz;
}
#endif
if (tz != NULL) {
#ifdef __linux__
/*
* Ignore "posix/" prefix.
*/
if (strncmp(tz, "posix/", 6) == 0) {
tz += 6;
}
#endif
javatz = strdup(tz);
if (freetz != NULL) {
free((void *) freetz);
}
}
return javatz;
}
步骤:
1、使用< Java home>/lib/tzmappings,。如果没有找到"TZ"变量,就进行第2步
2、 tz = getPlatformTimeZoneID(); 执行Linux特定的映射,如果找到,返回一个时区ID,否则返回null
【Linux】Centos7修改系统时区timezone方式:
timedatectl
如图
修改时区
timedatectl set-timezone Asia/Shanghai
如图
3、对比/etc/localtime与"/usr/share/zoneinfo目录下的文件,如果一致,就返回时区ID,没有则到第4步
4、返回到GMT
文章转自公众号:小虚竹JAVA之术
分类
标签
已于2022-6-7 17:08:05修改
赞
收藏
回复
相关推荐