Actual source code: fdate.c

  1: #include <petscsys.h>
  2: #if defined(PETSC_HAVE_SYS_TIME_H)
  3:   #include <sys/time.h>
  4: #endif
  5: #include <time.h>

  7: /*@C
  8:   PetscGetDate - Gets the current date.

 10:   Not Collective

 12:   Input Parameter:
 13: . len - length of string to hold date

 15:   Output Parameter:
 16: . date - the date

 18:   Level: beginner

 20:   Note:
 21:   This function makes a system call and thus SHOULD NOT be called from an error handler.

 23:   Developer Notes:
 24:   This function is called once during `PetscInitialize()`.
 25:   It stashes the timestamp, and uses it when needed. This is so that
 26:   error handlers may report the date without generating possible
 27:   additional system errors during the call to get the date.

 29: .seealso: `PetscGetHostName()`
 30: @*/
 31: PetscErrorCode PetscGetDate(char date[], size_t len)
 32: {
 33:   char *str = NULL;
 34: #if defined(PETSC_HAVE_TIME)
 35:   time_t aclock;
 36: #else
 37:   struct timeval tp;
 38: #endif

 40:   PetscFunctionBegin;
 41: #if defined(PETSC_HAVE_TIME)
 42:   time(&aclock);
 43:   PetscCall(PetscStrncpy(date, asctime(localtime(&aclock)), len));
 44: #else
 45:   gettimeofday(&tp, NULL);
 46:   PetscCall(PetscStrncpy(date, asctime(localtime((time_t *)&tp.tv_sec)), len));
 47: #endif
 48:   /* now strip out the new-line chars at the end of the string */
 49:   PetscCall(PetscStrstr(date, "\n", &str));
 50:   if (str) str[0] = 0;
 51:   PetscFunctionReturn(PETSC_SUCCESS);
 52: }