Time function in cpp

time

Get the current calendar time as a value of type time_t .

The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer .

The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime , gmtime or difftime ).

Parameters

timer Pointer to an object of type time_t , where the time value is stored.
Alternatively, this parameter can be a null pointer, in which case the parameter is not used (the function still returns a value of type time_t with the result).

Return Value

The current calendar time as a time_t object.

If the argument is not a null pointer, the return value is the same as the one stored in the location pointed by argument timer .

Читайте также:  Html по центру страницы по вертикали

If the function could not retrieve the calendar time, it returns a value of -1 .

time_t is an alias of a fundamental arithmetic type capable of representing times.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* time example */ /* printf */ /* time_t, struct tm, difftime, time, mktime */ int main () < time_t timer; struct tm y2k = ; double seconds; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time(&timer); /* get current time; same as: timer = time(NULL) */ seconds = difftime(timer,mktime(&y2k)); printf ("%.f seconds since January 1, 2000 in the current timezone", seconds); return 0; >
414086872 seconds since January 1, 2000 in the current timezone 

Data races

Exceptions (C++)

See also

asctime Convert tm structure to string (function) gmtime Convert time_t to tm as UTC time (function) localtime Convert time_t to tm as local time (function)

Источник

time , _time32 , _time64

destTime
Pointer to the storage location for time.

Return value

Returns the time as seconds elapsed since midnight, January 1, 1970, or -1 if there’s an error.

Remarks

The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, Coordinated Universal Time (UTC), according to the system clock. The return value is stored in the location given by destTime . This parameter may be NULL , in which case the return value isn’t stored.

time is a wrapper for _time64 and time_t is, by default, equivalent to __time64_t . If you need to force the compiler to interpret time_t as the old 32-bit time_t , you can define _USE_32BIT_TIME_T . We don’t recommend _USE_32BIT_TIME_T , because your application may fail after January 18, 2038; the use of this macro isn’t allowed on 64-bit platforms.

Requirements

Routine Required C header Required C++ header
time , _time32 , _time64 or

For more compatibility information, see Compatibility.

Example

// crt_times.c // compile with: /W3 // This program demonstrates these time and date functions: // time _ftime ctime_s asctime_s // _localtime64_s _gmtime64_s mktime _tzset // _strtime_s _strdate_s strftime // // Also the global variable: // _tzname // // Turn off deprecated unsafe CRT function warnings #define _CRT_SECURE_NO_WARNINGS 1 #include #include #include #include #include #include int main() < char tmpbuf[128], timebuf[26], ampm[] = "AM"; time_t ltime; struct _timeb tstruct; struct tm today, gmt, xmas = < 0, 0, 12, 25, 11, 93 >; 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. _strtime_s( tmpbuf, 128 ); printf( "OS time:\t\t\t\t%s\n", tmpbuf ); _strdate_s( tmpbuf, 128 ); printf( "OS date:\t\t\t\t%s\n", tmpbuf ); // Get UNIX-style time and display as number and string. time( <ime ); printf( "Time in seconds since UTC 1/1/70:\t%lld\n", (long long)ltime ); err = ctime_s(timebuf, 26, <ime); if (err) < printf("ctime_s failed due to an invalid argument."); exit(1); >printf( "UNIX time and date:\t\t\t%s", timebuf ); // Display UTC. err = _gmtime64_s( &gmt, <ime ); if (err) < printf("_gmtime64_s failed due to an invalid argument."); >err = asctime_s(timebuf, 26, &gmt); if (err) < printf("asctime_s failed due to an invalid argument."); exit(1); >printf( "Coordinated universal time:\t\t%s", timebuf ); // Convert to time structure and adjust for PM if necessary. err = _localtime64_s( &today, <ime ); if (err) < printf("_localtime64_s failed due to an invalid argument."); exit(1); >if ( today.tm_hour >= 12 ) < strcpy_s( ampm, sizeof(ampm), "PM" ); today.tm_hour -= 12; >if ( today.tm_hour == 0 ) // Adjust if midnight hour. today.tm_hour = 12; // Convert today into an ASCII string err = asctime_s(timebuf, 26, &today); if (err) < printf("asctime_s failed due to an invalid argument."); exit(1); >// 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", timebuf + 11, ampm ); // Print additional time information. _ftime( &tstruct ); // C4996 // Note: _ftime is deprecated; consider using _ftime_s instead printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm ); printf( "Zone difference in hours from UTC:\t%u\n", tstruct.timezone/60 ); printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); //C4996 // Note: _tzname is deprecated; consider using _get_tzname 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 ) < err = asctime_s(timebuf, 26, &xmas); if (err) < printf("asctime_s failed due to an invalid argument."); exit(1); >printf( "Christmas\t\t\t\t%s\n", timebuf ); > // Use time structure to build a customized time string. err = _localtime64_s( &today, <ime ); if (err) < printf(" _localtime64_s failed due to invalid arguments."); exit(1); >// 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 ); > 
OS time: 13:51:23 OS date: 04/25/03 Time in seconds since UTC 1/1/70: 1051303883 UNIX time and date: Fri Apr 25 13:51:23 2003 Coordinated universal time: Fri Apr 25 20:51:23 2003 12-hour time: 01:51:23 PM Plus milliseconds: 552 Zone difference in hours from UTC: 8 Time zone name: Pacific Standard Time Daylight savings: YES Christmas Sat Dec 25 12:00:00 1993 Today is Friday, day 25 of April in the year 2003. 

Источник

std:: time

Returns the current calendar time encoded as a std::time_t object, and also stores it in the object pointed to by arg , unless arg is a null pointer.

Contents

[edit] Parameters

[edit] Return value

Current calendar time encoded as std::time_t object on success, ( std:: time_t ) ( — 1 ) on error. If arg is not null, the return value is also stored in the object pointed to by arg .

[edit] Notes

The encoding of calendar time in std::time_t is unspecified, but most systems conform to the POSIX specification and return a value of integral type holding 86400 times the number of calendar days since the Epoch plus the number of seconds that have passed since the last midnight UTC. Most notably, POSIX time does not (and can not) take leap seconds into account, so that this integral value is not equal to the number of S.I. seconds that have passed since the epoch, but rather is reduced with the number of leap seconds that have occurred since the epoch. Implementations in which std::time_t is a 32-bit signed integer (many historical implementations) fail in the year 2038.

[edit] Example

#include #include int main() { std::time_t result = std::time(nullptr); std::cout  std::asctime(std::localtime(&result))  result  " seconds since the Epoch\n"; }
Wed Sep 21 10:27:52 2011 1316615272 seconds since the Epoch

Источник

time() in C++

C++ Course: Learn the Essentials

The time() function in C++ returns the current calendar time that has passed since 00:00 hours, January 1, 1970, UTC as an object of type time_t . The function accepts a single argument as a pointer to a time_t object and returns the time in seconds or -1 on failure.

What is time() in C++?

Ever wondered how we can find today’s date or time using C++? C++ has a time() function that returns the current calendar time as an object of type time_t . time() function is defined in ctime header file.

The value returned from the function represents the number of seconds since 00:00 hours, January 1, 1970, UTC.

Syntax of time() in C++

The time function accepts one argument in its function call. The syntax of time() function is:

Here, the arg pointer is the function argument of type time_t .

Parameters of time() in C++

The time() function takes the following parameters as an argument:

  • arg: It is a pointer to a time_t object which (if not nullptr ) stores the time. Alternatively, if the value of this parameter is the NULL pointer, the parameter is not used in this case.

Return Value of time() in C++

The time() function returns either:

  • On Success:: The current calendar time as a value of type time_t .
  • On Failure:: -1 which is casted to type time_t .

Exceptions of time() in C++

The time() function guarantees no throw. Hence, it never throws an exception and always returns a value.

Example of time() Function

Let us see the time required by the C++ program to run a loop that required 10 8 iterations.

Here, we calculate time once before using the sleep function and once after we sleep for 5 seconds. This way, the difference between the two times shows the compiler’s time to execute the sleep function.

Example

1. Program to demonstrate time in C++

Let us see the time() function used in C++ to print the seconds passed since 00:00 , January 1, 1970 .

Here, we call the time() function to which we pass a NULL pointer as an argument. The function returns time in seconds that have passed since 00:00:00 GMT, Jan 1, 1970 .

2. time in c++ with Reference Pointer

Let us see how we can use the argument in the time() function to store the returned time in a reference pointer.

Here, we create a variable of type time_t and pass its reference as a function argument to the time function. Therefore, the time value in seconds is stored in the present_time variable.

Conclusion

  • The time() function returns the current calendar time as an object of type time_t . time() function is defined in ctime header file.
  • The value returned from the function represents the number of seconds since 00:00 hours, January 1, 1970, UTC.
  • The function accepts a single argument pointer ( time_t* arg ) to a time_t object which (if not nullptr ) stores the time.
  • The time() function returns either time in seconds or -1 on failure.

See Also

The header file supports four time-related types: tm, clock_t, time_t, and size_t. Each of these types represents time and date as an integer. C++ ctime header has several other time-related functions that you can explore while working with date and time. Some of them are:

Also, you can read more articles on some of the advanced concepts in C++ like:

Источник

Оцените статью