Actual source code: fdate.c

petsc-3.3-p7 2013-05-11
  2: #include <petscsys.h>
  3: #if defined(PETSC_HAVE_SYS_TIME_H)
  4: #include <sys/types.h>
  5: #include <sys/time.h>
  6: #endif
  7: #include <time.h>
  8: #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
  9: EXTERN_C_BEGIN
 10: extern int gettimeofday(struct timeval *,struct timezone *);
 11: EXTERN_C_END
 12: #endif
 13: 
 14: /*
 15:   This function is called once during the initialize stage.
 16:   It stashes the timestamp, and uses it when needed. This is so that 
 17:   error handlers may report the date without generating possible
 18:   additional system errors during the call to get the date.

 20: */
 23: /*@C
 24:     PetscGetDate - Gets the current date.

 26:    Not collective

 28:   Input Parameter:
 29: .  len - length of string to hold date

 31:   Output Parameter:
 32: .  date - the date

 34:   Level: beginner

 36:     This function DOES make a system call and thus SHOULD NOT be called
 37:     from an error handler. 

 39: @*/
 40: PetscErrorCode  PetscGetDate(char date[],size_t len)
 41: {
 42:   char           *str=PETSC_NULL;
 43: #if defined(PETSC_HAVE_TIME)
 44:   time_t         aclock;
 45: #else
 46:   struct timeval tp;
 47: #endif

 51: #if defined(PETSC_HAVE_TIME)
 52:   time(&aclock);
 53:   PetscStrncpy(date,asctime(localtime(&aclock)),len);
 54: #else
 55:   gettimeofday(&tp,(struct timezone *)0);
 56:   PetscStrncpy(date,asctime(localtime((time_t*)&tp.tv_sec)),len);
 57: #endif
 58:   /* now strip out the new-line chars at the end of the string */
 59:   PetscStrstr(date,"\n",&str);
 60:   if (str) str[0] = 0;
 61:   return(0);
 62: }