擷取時區名稱或日光節約標準時間 (DST) 區域名稱的字元字串表示。
語法
errno_t _get_tzname(
size_t* pReturnValue,
char* timeZoneName,
size_t sizeInBytes,
int index
);
參數
pReturnValue
包含終止符的timeZoneNameNULL字串長度。
timeZoneName
代表時區名稱或日光節約標準時區名稱 (DST) 的字元字串的位址,取決於 index。
sizeInBytes
timeZoneName 字元字串的大小,以位元組為單位。
index
index要擷取的兩個時區名稱之一的 。
index |
timeZoneName 的內容。 |
timeZoneName 預設值 |
|---|---|---|
| 0 | 時區名稱 | "PST" |
| 1 | 日光節約標準時區名稱 | "PDT" |
| > 1 或 < 0 | 請將 errno 設為 EINVAL |
未修改 |
除非在執行時間期間明確更新, "PST" 否則會針對標準時區和 "PDT" 日光標準時區傳回 。 如需詳細資訊,請參閱。
不保證時區字串在OS版本之間相同。 官方時區名稱可以和變更。
傳回值
如果成功則為零,否則為 errno 類型的值。
timeZoneName如果 為 ,或 sizeInBytes 為NULL零或小於零(但不是兩者),則會叫用無效的參數處理程式,如參數驗證中所述。 若允許繼續執行,此函式會將 errno 設為 EINVAL,並傳回 EINVAL。
錯誤條件
pReturnValue |
timeZoneName |
sizeInBytes |
index |
傳回值 | timeZoneName 的內容。 |
|---|---|---|---|---|---|
| 時區名稱的大小 | NULL |
0 | 0 或 1 | 0 | 未修改 |
| 時區名稱的大小 | 任意 | > 0 | 0 或 1 | 0 | 時區名稱 |
| 未修改 | NULL |
> 0 | 任意 | EINVAL |
未修改 |
| 未修改 | 任意 | 零 | 任意 | EINVAL |
未修改 |
| 未修改 | 任意 | > 0 | > 1 | EINVAL |
未修改 |
備註
函_get_tzname式會根據 index 值,將目前時區名稱或日光節約標準時區名稱 (DST) 的字元字串表示擷取至 的位址timeZoneName,以及中的pReturnValue字元串大小。 如果 timeZoneName 是 NULL 且 sizeInBytes 為零,則會在 中傳回字串大小,以位元組為單位來保存指定的時區和終止 NULL。pReturnValue
值 index 必須是標準時區的 0 或日光標準時區的 1;任何其他值都有不確定的結果。
根據預設, "PST" 會針對標準時區和 "PDT" 日光標準時區傳回 。 若函式需要時區資訊,例如 strftime、ftime、ftime_s、、、 localtimemktime等,則第一次更新真正的時區名稱。 如果在呼叫 _get_tzname之前未呼叫不需要時區資訊的函式,則會傳回預設值,除非您先使用上述其中一個函式或呼叫 tzset來明確更新它們。 此外,如果 TZ 已設定環境變數,則會優先於操作系統所報告的時區名稱。 即使在此情況下,在呼叫 之前 _get_tzname 必須呼叫上述其中一個函式,否則會傳回預設時區值。 如需環境變數和 CRT 的詳細資訊 TZ ,請參閱 _tzset。
警告
操作系統版本之間不保證時區字串相同。 官方時區名稱可以和變更。
根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。
範例
此範例會呼叫 _get_tzname 以取得必要的緩衝區大小,以顯示目前的日光節約標準時區名稱、配置該大小的緩衝區、再次呼叫 _get_tzname 以載入緩衝區中的名稱,並將它列印至控制台。
它也會呼叫 _tzset() ,讓OS在呼叫 _get_tzname()之前更新時區資訊。 否則會使用預設值。
// crt_get_tzname.c
// Compile by using: cl /W4 crt_get_tzname.c
#include <stdio.h>
#include <time.h>
#include <malloc.h>
enum TZindex {
STD,
DST
};
int main()
{
size_t tznameSize = 0;
char * tznameBuffer = NULL;
_tzset(); // Update the time zone information
// Get the size of buffer required to hold DST time zone name
if (_get_tzname(&tznameSize, NULL, 0, DST))
{
return 1; // Return an error value if it failed
}
// Allocate a buffer for the name
if (NULL == (tznameBuffer = (char *)(malloc(tznameSize))))
{
return 2; // Return an error value if it failed
}
// Load the name in the buffer
if (_get_tzname(&tznameSize, tznameBuffer, tznameSize, DST))
{
return 3; // Return an error value if it failed
}
printf_s("The current Daylight standard time zone name is %s.\n", tznameBuffer);
return 0;
}
輸出
The current Daylight standard time zone name is Pacific Daylight Time.
需求
| 常式 | 必要的標頭 |
|---|---|
_get_tzname |
<time.h> |
如需詳細資訊,請參閱相容性。
另請參閱
時間管理
errno、_doserrno、_sys_errlist 和 _sys_nerr
_get_daylight
_get_dstbias
_get_timezone