asctime_s, _wasctime_sasctime_s, _wasctime_s
Konwertuj strukturę czasu TM na ciąg znaków.Convert a tm time structure to a character string. Te funkcje są wersjami asctime, _wasctime z ulepszonymi zabezpieczeniami, zgodnie z opisem w temacie funkcje zabezpieczeń w CRT.These functions are versions of asctime, _wasctime with security enhancements as described in Security Features in the CRT.
SkładniaSyntax
errno_t asctime_s(
char* buffer,
size_t numberOfElements,
const struct tm *tmSource
);
errno_t _wasctime_s(
wchar_t* buffer,
size_t numberOfElements
const struct tm *tmSource
);
template <size_t size>
errno_t asctime_s(
char (&buffer)[size],
const struct tm *tmSource
); // C++ only
template <size_t size>
errno_t _wasctime_s(
wchar_t (&buffer)[size],
const struct tm *tmSource
); // C++ only
ParametryParameters
buforubuffer
Wskaźnik do buforu do przechowywania wyniku ciągu znaków.A pointer to a buffer to store the character string result. Ta funkcja przyjmuje wskaźnik do prawidłowej lokalizacji w pamięci o rozmiarze określonym przez NumberOfElements.This function assumes a pointer to a valid memory location with a size specified by numberOfElements.
numberOfElementsnumberOfElements
Rozmiar buforu używany do przechowywania wyniku.The size of the buffer used to store the result.
tmSourcetmSource
Struktura czasu/daty.Time/date structure. Ta funkcja przyjmuje wskaźnik do prawidłowego struct
obiektu TM .This function assumes a pointer to a valid struct
tm object.
Wartość zwracanaReturn Value
Zero, jeśli powodzenie.Zero if successful. Jeśli wystąpi awaria, zostanie wywołana procedura obsługi nieprawidłowego parametru, zgodnie z opisem w walidacji parametru.If there is a failure, the invalid parameter handler is invoked, as described in Parameter Validation. Jeśli wykonanie może być kontynuowane, zwracaną wartością jest kod błędu.If execution is allowed to continue, the return value is an error code. Kody błędów są zdefiniowane w ERRNO. H.Error codes are defined in ERRNO.H. Aby uzyskać więcej informacji, zobacz errno stałe.For more information, see errno Constants. W poniższej tabeli przedstawiono rzeczywiste kody błędów zwracane dla każdego warunku błędu.The actual error codes returned for each error condition are shown in the following table.
Warunki błędówError Conditions
buforubuffer | numberOfElementsnumberOfElements | tmSourcetmSource | PrzesłaćReturn | Wartość w buforzeValue in buffer |
---|---|---|---|---|
NULLNULL | DowolneAny | DowolneAny | EINVALEINVAL | Nie zmodyfikowanoNot modified |
Nie ma wartości null (wskazuje na prawidłową pamięć)Not NULL (points to valid memory) | 00 | DowolneAny | EINVALEINVAL | Nie zmodyfikowanoNot modified |
Nie ma wartości nullNot NULL | 0< rozmiar < 260< size < 26 | DowolneAny | EINVALEINVAL | Pusty ciągEmpty string |
Nie ma wartości nullNot NULL | >= 26>= 26 | NULLNULL | EINVALEINVAL | Pusty ciągEmpty string |
Nie ma wartości nullNot NULL | >= 26>= 26 | Nieprawidłowa struktura czasu lub wartości spoza zakresu dla składników czasuInvalid time structure or out of range values for components of the time | EINVALEINVAL | Pusty ciągEmpty string |
Uwaga
Warunki błędów dla wasctime_s są podobne do asctime_s z wyjątkiem tego, że limit rozmiaru jest mierzony w wyrazach.Error conditions for wasctime_s are similar to asctime_s with the exception that the size limit is measured in words.
UwagiRemarks
Funkcja asctime konwertuje godzinę przechowywaną jako strukturę do ciągu znaków.The asctime function converts a time stored as a structure to a character string. Wartość tmSource jest zazwyczaj uzyskiwana z wywołania gmtime lub localtime.The tmSource value is usually obtained from a call to gmtime or localtime. Obie funkcje mogą służyć do wypełnienia struktury TM , zgodnie z definicją w Time. H.Both functions can be used to fill in a tm structure, as defined in TIME.H.
timeptr element członkowskitimeptr member | WartośćValue |
---|---|
tm_hourtm_hour | Godz. od północy (0-23)Hours since midnight (0-23) |
tm_isdsttm_isdst | Pozytywna, jeśli obowiązuje zmiana czasu letniego; 0, jeśli czas letni nie jest stosowany; wartość ujemna, jeśli stan czasu letniego jest nieznany.Positive if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative if status of daylight saving time is unknown. Biblioteka środowiska uruchomieniowego C przyjmuje reguły Stany Zjednoczone "na potrzeby wykonywania obliczeń czasu letniego (DST).The C run-time library assumes the United States' rules for implementing the calculation of Daylight Saving Time (DST). |
tm_mdaytm_mday | Dzień miesiąca (1-31)Day of month (1-31) |
tm_mintm_min | Minuty po godzinie (0-59)Minutes after hour (0-59) |
tm_montm_mon | Miesiąc (0-11; Styczeń = 0)Month (0-11; January = 0) |
tm_sectm_sec | Sekund po minucie (0-59)Seconds after minute (0-59) |
tm_wdaytm_wday | Dzień tygodnia (0-6; Niedziela = 0)Day of week (0-6; Sunday = 0) |
tm_ydaytm_yday | Dzień roku (0-365; 1 stycznia = 0)Day of year (0-365; January 1 = 0) |
tm_yeartm_year | Year (bieżący rok minus 1900)Year (current year minus 1900) |
Przekonwertowany ciąg znaków jest również dostosowywany zgodnie z ustawieniami lokalnej strefy czasowej.The converted character string is also adjusted according to the local time zone settings. Zapoznaj się z funkcjami Time, _time32, _time64, _ftime, _ftime32, _ftime64i localtime_s, _localtime32_s , _localtime64_s, Aby uzyskać informacje na temat konfigurowania środowiska strefy czasowej i zmiennych globalnych.See the time, _time32, _time64, _ftime, _ftime32, _ftime64, and localtime_s, _localtime32_s, _localtime64_s functions for information about configuring the local time and the _tzset function for information about defining the time zone environment and global variables.
Wynik ciągu utworzony przez asctime_s zawiera dokładnie 26 znaków i ma postać Wed Jan 02 02:03:55 1980\n\0
.The string result produced by asctime_s contains exactly 26 characters and has the form Wed Jan 02 02:03:55 1980\n\0
. Używany jest zegar 24-godzinny.A 24-hour clock is used. Wszystkie pola mają stałą szerokość.All fields have a constant width. Nowy znak linii i znak null zajmują ostatnie dwa pozycje ciągu.The new line character and the null character occupy the last two positions of the string. Wartość, która została przeniesiona jako drugi parametr, musi mieć co najmniej wartość Big.The value passed in as the second parameter should be at least this big. Jeśli jest mniejsza, zostanie zwrócony kod błędu, EINVAL.If it is less, an error code, EINVAL, will be returned.
_wasctime_s to dwubajtowa wersja asctime_s._wasctime_s is a wide-character version of asctime_s. _wasctime_s i asctime_s zachowują się identycznie w inny sposób._wasctime_s and asctime_s behave identically otherwise.
Wersje biblioteki debugowania tych funkcji najpierw wypełniają bufor 0xFE.The debug library versions of these functions first fill the buffer with 0xFE. Aby wyłączyć to zachowanie, użyj _CrtSetDebugFillThreshold.To disable this behavior, use _CrtSetDebugFillThreshold.
Domyślnie globalny stan tej funkcji jest objęty zakresem aplikacji.By default, this function's global state is scoped to the application. Aby to zmienić, zobacz stan globalny w CRT.To change this, see Global state in the CRT.
Generic-Text mapowanie proceduryGeneric-Text Routine Mapping
Procedura TCHAR.HTCHAR.H routine | Nie zdefiniowano _MBCS _UNICODE &_UNICODE & _MBCS not defined | _MBCS zdefiniowano_MBCS defined | _UNICODE zdefiniowano_UNICODE defined |
---|---|---|---|
_tasctime_s_tasctime_s | asctime_sasctime_s | asctime_sasctime_s | _wasctime_s_wasctime_s |
W języku C++ korzystanie z tych funkcji jest uproszczone przez przeciążenia szablonów; przeciążenia mogą automatycznie wywnioskować długość buforu, eliminując konieczność określenia argumentu rozmiaru.In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically, eliminating the need to specify a size argument. Aby uzyskać więcej informacji, zobacz bezpieczne przeciążenia szablonów.For more information, see Secure Template Overloads.
WymaganiaRequirements
ProceduraRoutine | Wymagany nagłówekRequired header |
---|---|
asctime_sasctime_s | <time.h> |
_wasctime_s_wasctime_s | <time.h> lub <wchar.h><time.h> or <wchar.h> |
ZabezpieczeniaSecurity
Jeśli wskaźnik buforu ma wartość inną niż null , a wskaźnik nie wskazuje prawidłowego buforu, funkcja zostanie zastąpiona, niezależnie od lokalizacji.If the buffer pointer is not NULL and the pointer does not point to a valid buffer, the function will overwrite whatever is at the location. Może to również spowodować naruszenie zasad dostępu.This can also result in an access violation.
Przepełnienie buforu może wystąpić, jeśli długość argumentu rozmiaru przebiegła jest większa niż rzeczywisty rozmiar buforu.A buffer overrun can occur if the size argument passed in is greater than the actual size of the buffer.
PrzykładExample
Ten program umieszcza czas systemowy w aclockej liczbie całkowitej, tłumaczy je na strukturę newtime , a następnie konwertuje ją na postać ciągu dla danych wyjściowych przy użyciu funkcji asctime_s .This program places the system time in the long integer aclock, translates it into the structure newtime and then converts it to string form for output, using the asctime_s function.
// crt_asctime_s.c
#include <time.h>
#include <stdio.h>
struct tm newtime;
__time32_t aclock;
int main( void )
{
char buffer[32];
errno_t errNum;
_time32( &aclock ); // Get time in seconds.
_localtime32_s( &newtime, &aclock ); // Convert time to struct tm form.
// Print local time as a string.
errNum = asctime_s(buffer, 32, &newtime);
if (errNum)
{
printf("Error code: %d", (int)errNum);
return 1;
}
printf( "Current date and time: %s", buffer );
return 0;
}
Current date and time: Wed May 14 15:30:17 2003
Zobacz teżSee also
Zarządzanie czasemTime Management
ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_sctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s
_ftime, _ftime32, _ftime64_ftime, _ftime32, _ftime64
gmtime_s, _gmtime32_s, _gmtime64_sgmtime_s, _gmtime32_s, _gmtime64_s
localtime_s, _localtime32_s, _localtime64_slocaltime_s, _localtime32_s, _localtime64_s
time, _time32, _time64time, _time32, _time64
_tzset_tzset