xref: /petsc/src/sys/time/fdate.c (revision 3923b477fd0dced8a2d147b4fb4519fe3af97d3f)
1 
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.
19 
20 */
21 #undef __FUNCT__
22 #define __FUNCT__ "PetscGetDate"
23 /*@C
24     PetscGetDate - Gets the current date.
25 
26    Not collective
27 
28   Input Parameter:
29 .  len - length of string to hold date
30 
31   Output Parameter:
32 .  date - the date
33 
34   Level: beginner
35 
36     This function DOES make a system call and thus SHOULD NOT be called
37     from an error handler.
38 
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
48   PetscErrorCode ierr;
49 
50   PetscFunctionBegin;
51 #if defined(PETSC_HAVE_TIME)
52   time(&aclock);
53   ierr = PetscStrncpy(date,asctime(localtime(&aclock)),len);CHKERRQ(ierr);
54 #else
55   gettimeofday(&tp,(struct timezone *)0);
56   ierr = PetscStrncpy(date,asctime(localtime((time_t*)&tp.tv_sec)),len);CHKERRQ(ierr);
57 #endif
58   /* now strip out the new-line chars at the end of the string */
59   ierr = PetscStrstr(date,"\n",&str);CHKERRQ(ierr);
60   if (str) str[0] = 0;
61   PetscFunctionReturn(0);
62 }
63 
64