xref: /petsc/src/sys/time/fdate.c (revision bce096a405b23f07feb1783633a56a7a90eceb89)
1 #define PETSC_DLL
2 
3 #include "petsc.h"
4 #include "petscsys.h"
5 #include "petscfix.h"
6 #if defined(PETSC_HAVE_SYS_TIME_H)
7 #include <sys/types.h>
8 #include <sys/time.h>
9 #endif
10 #include <time.h>
11 #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
12 EXTERN_C_BEGIN
13 EXTERN int gettimeofday(struct timeval *,struct timezone *);
14 EXTERN_C_END
15 #endif
16 
17 /*
18   This function is called once during the initialize stage.
19   It stashes the timestamp, and uses it when needed. This is so that
20   error handlers may report the date without generating possible
21   additional system errors during the call to get the date.
22 
23 */
24 #undef __FUNCT__
25 #define __FUNCT__ "PetscGetDate"
26 /*@C
27     PetscGetDate - Gets the current date.
28 
29    Not collective
30 
31   Input Parameter:
32 .  len - length of string to hold date
33 
34   Output Parameter:
35 .  date - the date
36 
37   Level: beginner
38 
39     This function DOES make a system call and thus SHOULD NOT be called
40     from an error handler.
41 
42 @*/
43 PetscErrorCode PETSC_DLLEXPORT PetscGetDate(char date[],size_t len)
44 {
45   char           *str=PETSC_NULL;
46 #if defined(PETSC_HAVE_TIME)
47   time_t         aclock;
48 #else
49   struct timeval tp;
50 #endif
51   PetscErrorCode ierr;
52 
53   PetscFunctionBegin;
54 #if defined(PETSC_HAVE_TIME)
55   time(&aclock);
56   ierr = PetscStrncpy(date,asctime(localtime(&aclock)),len);CHKERRQ(ierr);
57 #else
58   gettimeofday(&tp,(struct timezone *)0);
59   ierr = PetscStrncpy(date,asctime(localtime((time_t*)&tp.tv_sec)),len);CHKERRQ(ierr);
60 #endif
61   /* now strip out the new-line chars at the end of the string */
62   ierr = PetscStrstr(date,"\n",&str);CHKERRQ(ierr);
63   if (str) str[0] = 0;
64   PetscFunctionReturn(0);
65 }
66 
67