cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : ctime (time.h) : time
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
ctime (time.h)
functions:
· asctime
· clock
· ctime
· difftime
· gmtime
· localtime
· mktime
· strftime
· time
macros:
· CLOCKS_PER_SEC
· NULL
types:
· clock_t
· size_t
· time_t
· struct tm

-

time function
time_t time ( time_t * timer );
<ctime>

Get current time

Get the current calendar time as a time_t object.

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

Parameters

timer
Pointer to an object of type time_t, where the time value is stored.
Alternativelly, this parameter can be a null pointer, in which case the parameter is not used, but the a time_t object is still returned by the function.

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 the argument.

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

Example

/* time example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time (NULL);
  printf ("%ld hours since January 1, 1970", seconds/3600);
  
  return 0;
}

Possible output:


266344 hours since January 1, 1970

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)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us