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>
      6: #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
      7: PETSC_EXTERN int gettimeofday(struct timeval *, struct timezone *);
      8: #endif

     10: /*@C
     11:   PetscGetDate - Gets the current date.

     13:   Not Collective

     15:   Input Parameter:
     16: . len - length of string to hold date

     18:   Output Parameter:
     19: . date - the date

     21:   Level: beginner

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

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

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

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