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

Time Management Use these functions to get the current time and convert, adjust, and store it as necessary.
Get the time
time Gets the system time.
_ftime , structure _timeb Gets the current time.
_strtime, _wstrtime Copy the time to a buffer.
_strdate, _wstrdate Copy a date to a buffer.
Convert date and / or time
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.
Other date & time functions
clock Calculates the processor time used by the calling process.
_tzset Sets time environment variables.
difftime Finds the difference between two times.


Time Management
Use these functions to get the current time and convert, adjust, and store it as necessary.
The current time is the system time.
The _ftime and localtime routines use the TZ environment variable. If TZ is not set, the run-time library attempts to use the time-zone information specified by the operating system. If this information is unavailable, these functions use the default value of PST8PDT. For more information on TZ, see _tzset; also see _daylight, timezone, and _tzname.
Time Routines.

Note In all versions of Microsoft C/C++ except Microsoft C/C++ version 7.0, and in all versions of Microsoft Visual C++, the time function returns the current time as the number of seconds elapsed since midnight on January 1, 1970. In Microsoft C/C++ version 7.0, time returned the current time as the number of seconds elapsed since midnight on December 31, 1899.
Function
Use
asctime, _wasctime Convert time from type struct tm to character string
clock Return elapsed CPU time for process
ctime, _wctime Convert time from type time_t to character string
difftime Compute difference between two specified time values
_ftime Store current system time in variable of type struct _timeb
_futime Set modification time on open file
gmtime Convert time from type time_t to struct tm
localtime Convert time from type time_t to struct tm with local correction
mktime Convert time to calendar value
_strdate, _wstrdate Return current system date as string
strftime, wcsftime Format date-and-time string for international use
_strtime, _wstrtime Return current system time as string
time Get current system time as type time_t
_tzset Set external time variables from environment time variable TZ
_utime, _wutime Set modification time for specified file using either current time or time value stored in structure

difftime
Finds the difference between two times.
double difftime( time_t timer1, time_t timer0 );

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

Remarks
The difftime function computes the difference between the two supplied time values timer0 and timer1.

Subject: Floating-Point Support Routines, Time Management Routines
Keywords: See also time
Return Value
difftime returns the elapsed time in seconds, from timer0 to timer1. The value returned is a double-precision floating-point number.

Parameters
timer1 Ending time
timer0 Beginning time

Example
/* DIFFTIME.C: This program calculates the amount of time
* needed to do a floating-point multiply 10 million times.
*/

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

void main( void )
{
time_t start, finish;
long loop;
double result, elapsed_time;

printf( "Multiplying 2 floating point numbers 10 million times...\n" );

time( &start );
for( loop = 0; loop < 10000000; loop++ )
result = 3.63 * 5.27;
time( &finish );

elapsed_time = difftime( finish, start );
printf( "\nProgram takes %6.0f seconds.\n", elapsed_time );
}


Output
Multiplying 2 floats 10 million times...

Program takes 2 seconds.


clock
Calculates the processor time used by the calling process.
clock_t clock( void );

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

Remarks
The clock function tells how much processor time the calling process has used. The time in seconds is approximated by dividing the clock return value by the value of the CLOCKS_PER_SEC constant. In other words, clock returns the number of processor timer ticks that have elapsed. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.

Subject: Time Management Routines
Keywords: See also difftime, time
Return Value
clock returns the number of clock ticks of elapsed processor time. The returned value is the product of the amount of time that has elapsed since the start of a process and the value of the CLOCKS_PER_SEC constant. If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.

Example
/* CLOCK.C: This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/

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

void sleep( clock_t wait );

void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}


Output
Delay for three seconds
Done!
Time to do 600000 empty loops is 0.1 seconds


_tzset
Sets time environment variables.
void _tzset( void );

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

Remarks
The _tzset function uses the current setting of the environment variable TZ to assign values to three global variables: _daylight, _timezone, and _tzname. These variables are used by the _ftime and localtime functions to make corrections from coordinated universal time (UTC) to local time, and by the time function to compute UTC from system time.

Use the following syntax to set the TZ environment variable:
set TZ=tzn[+ | –]hh[:mm[:ss] ][dzn]
tzn Three-letter time-zone name, such as PST. You must specify the correct offset from UTC.
hh Difference in hours between UTC and local time. Optionally signed.
mm Minutes. Separated from hh by a colon (:).
ss Seconds. Separated from mm by a colon (:).
dzn Three-letter daylight-saving-time zone such as PDT. If daylight saving time is never in effect in the locality, set TZ without a value for dzn. The C run-time library assumes the United States's rules for implementing the calculation of Daylight Saving Time (DST).

For example, to set the TZ environment variable to correspond to the current time zone in Germany, you can use one of the following statements:
set TZ=GST1GDT
set TZ=GST+1GDT

These strings use GST to indicate German standard time, assume that Germany is one hour ahead of UTC, and assume that daylight savings time is in effect.

If the TZ value is not set, _tzset attempts to use the time zone information specified by the operating system. Under Windows NT and Windows 95, this information is specified in the Control Panel's Date/Time application. If _tzset cannot obtain this information, it uses PST8PDT by default, which signifies the Pacific time zone.

Based on the TZ environment variable value, the following values are assigned to the
global variables _daylight, _timezone, and _tzname when _tzset is called:

Global Variable Description Default Value
_daylight Nonzero value if a daylight-saving-time zone is specified in TZ setting; otherwise, 0 1
_timezone Difference in seconds between UTC and local time. 28800 (28800 seconds equals 8 hours)
_tzname[0] String value of time-zone name from TZ environmental variable; empty if TZ has not been set PST
_tzname[1] String value of daylight-saving-time zone; empty if daylight-saving-time zone is omitted from TZ environmental variable PDT

The default values shown in the preceding table for _daylight and the _tzname array correspond to "PST8PDT." If the DST zone is omitted from the TZ environmental variable, the value of _daylight is 0 and the _ftime, gmtime, and localtime functions return 0 for their DST flags.

Subject: Time Management RoutinesTime Management Routines
Keywords: See also asctime, _ftime, gmtime, localtime, time, _utime
Return Value
None

Example
/* TZSET.C: This program first sets up the time zone by
* placing the variable named TZ=EST5 in the environment
* table. It then uses _tzset to set the global variables
* named _daylight, _timezone, and _tzname.
*/

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

void main( void )
{
if( _putenv( "TZ=EST5EDT" ) == -1 )
{
printf( "Unable to set TZ\n" );
exit( 1 );
}
else
{
_tzset();
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
}
exit( 0 );
}


Output
_daylight = 1
_timezone = 18000
_tzname[0] = EST


Homepage von PS-Trainer - C-Entwicklung - Bibliotheken - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1