PS-Trainer C - Entwicklung
Bibliotheks-Funktionen: Umwandlung von Datum und Zeit
Homepage von PS-Trainer - C-Entwicklung - Datum & Zeit - an PS-Trainer
PS-Trainer PS-Trainer

Date and Time Management  
asctime, _wasctime , structure tm Converts a tm time structure to a character string.
ctime, _wctime Convert a time value to a string and adjust for local time zone settings.
localtime , structure tm Converts a time value and corrects for the local time zone.
mktime Converts the local time to a calendar value.
gmtime Converts a time value to a structure.
strftime, wcsftime Format a time string.


asctime, _wasctime
Converts a tm time structure to a character string.
char *asctime( const struct tm *timeptr );
wchar_t *_wasctime( const struct tm *timeptr );


Routine Required Header Compatibility
asctime <time.h> ANSI, Win 95, Win NT
_wasctime <time.h> or <wchar.h> Win 95, Win NT

Remarks
The asctime function converts a time stored as a structure to a character string. The timeptr value is usually obtained from a call to gmtime or localtime, which both return a pointer to a tm structure, defined in TIME.H.

timeptr Field Value
tm_hour Hours since midnight (0 – 23)
tm_isdst 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. The C run-time library assumes the United States's rules for implementing the calculation of Daylight Saving Time (DST).
tm_mday Day of month (1 – 31)
tm_min Minutes after hour (0 – 59)
tm_mon Month (0 – 11; January = 0)
tm_sec Seconds after minute (0 – 59)
tm_wday Day of week (0 – 6; Sunday = 0)
tm_yday Day of year (0 – 365; January 1 = 0)
tm_year Year (current year minus 1900)

The converted character string is also adjusted according to the local time zone settings. See the time, _ftime, and localtime functions for information on configuring the local time and the _tzset function for details about defining the time zone environment and global variables.

The string result produced by asctime contains exactly 26 characters and has the form Wed Jan 02 02:03:55 1980\n\0. A 24-hour clock is used. All fields have a constant width. The newline character and the null character occupy the last two positions of the string. asctime uses a single, statically allocated buffer to hold the return string. Each call to this function destroys the result of the previous call.

_wasctime is a wide-character version of asctime. _wasctime and asctime behave identically otherwise.

Generic-Text Routine Mapping:
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tasctime asctime asctime _wasctime

Subject: Time Management Routines
Keywords: See also ctime, _ftime, gmtime, localtime, time, _tzset
Return Value
asctime returns a pointer to the character string result;
_wasctime returns a pointer to the wide-character string result. There is no error return value.

Parameter
timeptr Time/date structure

Example
/* ASCTIME.C: 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 function.
*/

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

struct tm *newtime;
time_t aclock;

void main( void )
{
time( &aclock ); /* Get time in seconds */

newtime = localtime( &aclock ); /* Convert time to struct */
/* tm form */

/* Print local time as a string */
printf( "The current date and time are: %s", asctime( newtime ) );
}


Output
The current date and time are: Sun May 01 20:27:01 1994


ctime, _wctime
Convert a time value to a string and adjust for local time zone settings.
char *ctime( const time_t *timer );
wchar_t *_wctime( const time_t *timer );


Routine Required Header Compatibility
ctime <time.h> ANSI, Win 95, Win NT
_wctime <time.h> or <wchar.h> Win 95, Win NT

Remarks
The ctime function converts a time value stored as a time_t structure into a character string. The timer value is usually obtained from a call to time, which returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). The string result produced by ctime contains exactly 26 characters and has the form:
Wed Jan 02 02:03:55 1980\n\0

A 24-hour clock is used. All fields have a constant width. The newline character ('\n') and the null character ('\0') occupy the last two positions of the string.
The converted character string is also adjusted according to the local time zone settings. See the time, _ftime, and localtime functions for information on configuring the local time and the _tzset function for details about defining the time zone environment and global variables.

A call to ctime modifies the single statically allocated buffer used by the gmtime and localtime functions. Each call to one of these routines destroys the result of the previous call. ctime shares a static buffer with the asctime function. Thus, a call to ctime destroys the results of any previous call to asctime, localtime, or gmtime.

_wctime is a wide-character version of ctime; _wctime returns a pointer to a wide-character string. _wctime and ctime behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tctime ctime ctime _wctime

Subject: Time Management Routines
Keywords: See also asctime, _ftime, gmtime, localtime, time
Return Value
Each of these functions returns a pointer to the character string result.
If time represents a date before midnight, January 1, 1970, UTC, the function returns NULL.

Parameter
timer Pointer to stored time

Example
/* CTIME.C: This program gets the current
* time in time_t form, then uses ctime to
* display the time in string form.
*/

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

void main( void )
{
time_t ltime;

time( &ltime );
printf( "The time is %s\n", ctime( &ltime ) );
}


Output
The time is Fri Apr 29 12:25:12 1994


localtime
Converts a time value and corrects for the local time zone.
struct tm *localtime( const time_t *timer );

Routine Required Header Compatibility
localtime <time.h> ANSI, Win 95, Win NT

Return Value
localtime returns a pointer to the structure result.
If the value in timer represents a date before midnight, January 1, 1970, localtime returns NULL.
The fields of the structure type tm store the following values, each of which is an int:
tm_sec Seconds after minute (0 – 59)
tm_min Minutes after hour (0 – 59)
tm_hour Hours after midnight (0 – 23)
tm_mday Day of month (1 – 31)
tm_mon   Month (0 – 11; January = 0)
tm_year   Year (current year minus 1900)
tm_wday   Day of week (0 – 6; Sunday = 0)
tm_yday   Day of year (0 – 365; January 1 = 0)
tm_isdst   Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect;
negative value if status of daylight saving time is unknown.
The C run-time library assumes the United States's rules for implementing the calculation of Daylight Saving Time (DST).

Remarks
The localtime function converts a time stored as a time_t value and stores the result in a structure of type tm. The long value timer represents the seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC). This value is usually obtained from the time function.

gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion.
Each call to one of these routines destroys the result of the previous call.

localtime corrects for the local time zone if the user first sets the global environment variable TZ. When TZ is set, three other environment variables (_timezone, _daylight, and _tzname) are automatically set as well.
See _tzset for a description of these variables.
TZ
is a Microsoft extension and not part of the ANSI standard definition of localtime.
Note The target environment should try to determine whether daylight saving time is in effect.

Subject: Time Management Routines
Keywords: See also asctime, ctime, _ftime, gmtime, time, _tzset
Parameter
timeptr Pointer to stored time

Example
/* LOCALTIM.C: This program uses time to get the current time
* and then uses localtime to convert this time to a structure
* representing the local time. The program converts the result
* from a 24-hour clock to a 12-hour clock and determines the
* proper extension (AM or PM).
*/

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

void main( void )
{
struct tm *newtime;
char am_pm[] = "AM";
time_t long_time;

time( &long_time ); /* Get time as long integer. */
newtime = localtime( &long_time ); /* Convert to local time. */

if( newtime->tm_hour > 12 ) /* Set up extension. */
strcpy( am_pm, "PM" );
if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
newtime->tm_hour -= 12; /* to 12-hour clock. */
if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
newtime->tm_hour = 12;

printf( "%.19s %s\n", asctime( newtime ), am_pm );
}


Output
Tue Mar 23 11:28:17 AM


mktime
Converts the local time to a calendar value.
time_t mktime( struct tm *timeptr );

Routine Required Header Compatibility
mktime <time.h> ANSI, Win 95, Win NT

Remarks
The mktime function converts the supplied time structure (possibly incomplete) pointed to by timeptr into a fully defined structure with normalized values and then converts it to a time_t calendar time value. For description of tm structure fields, see asctime. The converted time has the same encoding as the values returned by the time function. The original values of the tm_wday and tm_yday components of the timeptr structure are ignored, and the original values of the other components are not restricted to their normal ranges.

mktime handles dates in any time zone from midnight, January 1, 1970, to midnight, February 5, 2036. If successful, mktime sets the values of tm_wday and tm_yday as appropriate and sets the other components to represent the specified calendar time, but with their values forced to the normal ranges; the final value of tm_mday is not set until tm_mon and tm_year are determined. When specifying a tm structure time, set the tm_isdst field to 0 to indicate that standard time is in effect, or to a value greater than 0 to indicate that daylight savings time is in effect, or to a value less than zero to have the C run-time library code compute whether standard time or daylight savings time is in effect. (The C run-time library assumes the United States's rules for implementing the calculation of Daylight Saving Time). tm_isdst is a required field. If not set, its value is undefined and the return value from mktime is unpredictable. If timeptr points to a tm structure returned by a previous call to asctime, gmtime, or localtime, the tm_isdst field contains the correct value.

Note that gmtime and localtime use a single statically allocated buffer for the conversion. If you supply this buffer to mktime, the previous contents are destroyed.

Subject: Time Management Routines
Keywords: See also asctime, gmtime, localtime, time
Return Value
mktime returns the specified calendar time encoded as a value of type time_t. If timeptr references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, the function returns –1 cast to type time_t.

Parameter
timeptr Pointer to time structure
Example
/* MKTIME.C: The example takes a number of days
* as input and returns the time, the current
* date, and the specified number of days.
*/

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

void main( void )
{
struct tm when;
time_t now, result;
int days;

time( &now );
when = *localtime( &now );
printf( "Current time is %s\n", asctime( &when ) );
printf( "How many days to look ahead: " );
scanf( "%d", &days );

when.tm_mday = when.tm_mday + days;
if( (result = mktime( &when )) != (time_t)-1 )
printf( "In %d days the time will be %s\n", days, asctime( &when ) );
else
perror( "mktime failed" );
}


Output
Current time is Tue May 03 12:45:47 1994

How many days to look ahead: 29
In 29 days the time will be Wed Jun 01 12:45:47 1994


gmtime
Converts a time value to a structure.
struct tm *gmtime( const time_t *timer );

Routine Required Header Compatibility
gmtime <time.h> ANSI, Win 95, Win NT

Remarks
The gmtime function breaks down the timer value and stores it in a statically allocated structure of type tm, defined in TIME.H. The value of timer is usually obtained from a call to the time function.
Note: The target environment should try to determine whether daylight savings time is in effect. The C run-time library assumes the United States's rules for implementing the calculation of Daylight Saving Time (DST).

Subject: Time Management Routines
Keywords: See also asctime, ctime, _ftime, localtime, mktime, time
Return Value
gmtime returns a pointer to a structure of type tm. The fields of the returned structure hold the evaluated value of the timer argument in UTC rather than in local time. Each of the structure fields is of type int, as follows:
tm_sec Seconds after minute (0 – 59)
tm_min Minutes after hour (0 – 59)
tm_hour Hours since midnight (0 – 23)
tm_mday Day of month (1 – 31)
tm_mon Month (0 – 11; January = 0)
tm_year Year (current year minus 1900)
tm_wday Day of week (0 – 6; Sunday = 0)
tm_yday Day of year (0 – 365; January 1 = 0)
tm_isdst Always 0 for gmtime

The gmtime, mktime, and localtime functions use the same single, statically allocated structure to hold their results. Each call to one of these functions destroys the result of any previous call.
If timer represents a date before midnight, January 1, 1970, gmtime returns NULL. There is no error return.

Parameter
timer Pointer to stored time. The time is represented as seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC).

Example
/* GMTIME.C: This program uses gmtime to convert a long-
* integer representation of coordinated universal time
* to a structure named newtime, then uses asctime to
* convert this structure to an output string.
*/

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

void main( void )
{
struct tm *newtime;
long ltime;

time( &ltime );

/* Obtain coordinated universal time: */
newtime = gmtime( &ltime );
printf( "Coordinated universal time is %s\n",
asctime( newtime ) );
}


Output
Coordinated universal time is Tue Mar 23 02:00:56 1993


strftime, wcsftime
Format a time string.
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );


Routine Required Header Compatibility
strftime <time.h> ANSI, Win 95, Win NT
wcsftime <time.h> or <wchar.h> ANSI, Win 95, Win NT

Remarks
The strftime and wcsftime functions format the tm time value in timeptr according to the supplied format argument and store the result in the buffer strDest. At most, maxsize characters are placed in the string. For a description of the fields in the timeptr structure, see asctime. wcsftime is the wide-character equivalent of strftime; its string-pointer argument points to a wide-character string. These functions behave identically otherwise.

Note Prior to this version of Visual C++, the documentation described the format parameter of wcsftime as having the datatype const wchar_t *, but the actual implementation of the format datatype was const char *. In this version, the implementation of the format datatype has been updated to reflect the previous and current documentation, that is: const wchar_t *.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcsftime strftime strftime wcsftime

The format argument consists of one or more codes; as in printf, the formatting codes are preceded by a percent sign (%). Characters that do not begin with % are copied unchanged to strDest. The LC_TIME category of the current locale affects the output formatting of strftime.(For more information on LC_TIME, see setlocale.)
The formatting codes for strftime are listed below:
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
% Date and time representation appropriate for locale
% Day of month as decimal number (01 – 31)
% Hour in 24-hour format (00 – 23)
% Hour in 12-hour format (01 – 12)
% Day of year as decimal number (001 – 366)
% Month as decimal number (01 – 12)
% Minute as decimal number (00 – 59)
% Current locale's A.M./P.M. indicator for 12-hour clock
% Second as decimal number (00 – 59)
% Week of year as decimal number, with Sunday as first day of week (00 – 51)
% Weekday as decimal number (0 – 6; Sunday is 0)
% Week of year as decimal number, with Monday as first day of week (00 – 51)
% Date representation for current locale
% Time representation for current locale
% Year without century, as decimal number (00 – 99)
% Year with century, as decimal number
%z, % Time-zone name or abbreviation; no characters if time zone is unknown
%%  Percent sign
As in the printf function, the # flag may prefix any formatting code.
In that case, the meaning of the format code is changed as follows:
Format Code Meaning
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% # flag is ignored.
%#c Long date and time representation, appropriate for current locale. For example: "Tuesday, March 14, 1995, 12:41:29".
%#x Long date representation, appropriate to current locale. For example: "Tuesday, March 14, 1995".
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y Remove leading zeros (if any).

Subject: Locale Routines, Time Management Routines, String Manipulation Routines
Keywords: See also localeconv, setlocale, strcoll, _stricoll, strxfrm
Return Value
strftime returns the number of characters placed in strDest if the total number of resulting characters, including the terminating null, is not more than maxsize.
wcsftime
returns the corresponding number of wide characters. Otherwise, the functions return 0, and the contents of strDest is indeterminate.

Parameters
strDest Output string
maxsize Maximum length of string
format Format-control string
timeptr tm data structure

Example
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/

#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

/* 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. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );

/* Get UNIX-style time and display as number and string. */
time( &ltime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );

/* Display UTC. */
gmt = gmtime( &ltime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

/* Convert to time structure and adjust for PM if necessary. */
today = localtime( &ltime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;

/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );

/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );

/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

/* Use time structure to build a customized time string. */
today = localtime( &ltime );

/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}

Output
OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993

Today is Tuesday, day 03 of May in the year 1994.


Homepage von PS-Trainer - C-Entwicklung - Datum & Zeit - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1