共用方式為


_strtime_s, _wstrtime_s

將目前的時間複製到緩衝區。 這些函式是 _strtime, _wstrtime 的版本,具有 CRT 中的安全性功能中所述的安全性增強功能。

語法

errno_t _strtime_s(
   char *buffer,
   size_t numberOfElements
);
errno_t _wstrtime_s(
   wchar_t *buffer,
   size_t numberOfElements
);
template <size_t size>
errno_t _strtime_s(
   char (&buffer)[size]
); // C++ only
template <size_t size>
errno_t _wstrtime_s(
   wchar_t (&buffer)[size]
); // C++ only

參數

buffer
長度至少為 10 個位元組的緩衝區內會寫入時間。

numberOfElements
緩衝區的大小。

傳回值

如果成功,則為零。

如果發生錯誤狀況,則會叫用無效的參數處理程式,如參數驗證中所述。 如果失敗,傳回的值會是錯誤碼。 錯誤碼在 EERRNO.H 中定義,請參閱下表查看此函式產生的確切錯誤。 如需錯誤碼的詳細資訊,請參閱 errno 常數

錯誤條件

buffer numberOfElements 傳回 buffer 的內容。
NULL (任何) EINVAL 未修改
不是 NULL (指向有效的緩衝區) 0 EINVAL 未修改
不是 NULL (指向有效的緩衝區) 0 < 大小 < 9 EINVAL 空字串
不是 NULL (指向有效的緩衝區) 大小 > 9 0 目前的時間格式一如<備註>所指定

安全性問題

如果參數大於 9,傳入無效的非NULL 值,則會導致存取違規 numberOfElements

傳遞大於緩衝區實際大小的 numberOfElements 值會造成緩衝區滿溢。

備註

這些函式提供 _strtime_wstrtime 的更安全版本。 函 _strtime_s 式會將目前的當地時間複製到 所指向的 buffer緩衝區。 時間會格式化為 hh:mm:ss ,其中 hh 兩位數代表 24 小時表示法中的小時, mm 是代表小時過去分鐘數的兩位數,而 ss 是代表秒數的兩位數。 例如,字串 18:23:44 代表下午 6 點之後的 23 分 44 秒。緩衝區長度必須至少為9個字節;實際大小是由第二個參數指定。

_wstrtime_s_strtime_s 的寬字元版本,_wstrtime_s 的引數與傳回值是寬字元字串。 除此之外,這些函式的行為相同。

C++ 利用多載樣板簡化了這些函式的使用方式。多載可自動推斷緩衝區長度 (因而不須指定大小引數),也可以將不安全的舊函式自動取代成較新且安全的對應函式。 如需詳細資訊,請參閱安全範本多載

這些函式的偵錯連結庫版本會先將緩衝區填入0xFE。 若要停用此行為,請使用 _CrtSetDebugFillThreshold

根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態

泛型文字例程對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 已定義 _UNICODE 已定義
_tstrtime_s _strtime_s _strtime_s _wstrtime_s

需求

常式 必要的標頭
_strtime_s <time.h>
_wstrtime_s <time.h> 或 <wchar.h>

如需相容性詳細資訊,請參閱相容性

範例

// strtime_s.c

#include <time.h>
#include <stdio.h>

int main()
{
    char tmpbuf[9];
    errno_t err;

    // Set time zone from TZ environment variable. If TZ is not set,
    // the operating system is queried to obtain the default value
    // for the variable.
    //
    _tzset();

    // Display operating system-style date and time.
    err = _strtime_s( tmpbuf, 9 );
    if (err)
    {
       printf("_strdate_s failed due to an invalid argument.");
      exit(1);
    }
    printf( "OS time:\t\t\t\t%s\n", tmpbuf );
    err = _strdate_s( tmpbuf, 9 );
    if (err)
    {
       printf("_strdate_s failed due to an invalid argument.");
       exit(1);
    }
    printf( "OS date:\t\t\t\t%s\n", tmpbuf );

}
OS time:            14:37:49
OS date:            04/25/03

另請參閱

時間管理
asctime_s, _wasctime_s
ctime_s、、_ctime32_s_ctime64_s_wctime_s、、_wctime32_s_wctime64_s
gmtime_s、 、 _gmtime32_s_gmtime64_s
localtime_s、 、 _localtime32_s_localtime64_s
mktime、 、 _mktime32_mktime64
time、 、 _time32_time64
_tzset