xref: /petsc/src/sys/time/fdate.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 
2 #include <petscsys.h>
3 #if defined(PETSC_HAVE_SYS_TIME_H)
4 #include <sys/time.h>
5 #endif
6 #include <time.h>
7 #if defined(PETSC_NEEDS_GETTIMEOFDAY_PROTO)
8 PETSC_EXTERN int gettimeofday(struct timeval *, struct timezone *);
9 #endif
10 
11 /*
12   This function is called once during the initialize stage.
13   It stashes the timestamp, and uses it when needed. This is so that
14   error handlers may report the date without generating possible
15   additional system errors during the call to get the date.
16 
17 */
18 /*@C
19     PetscGetDate - Gets the current date.
20 
21    Not collective
22 
23   Input Parameter:
24 .  len - length of string to hold date
25 
26   Output Parameter:
27 .  date - the date
28 
29   Level: beginner
30 
31     This function DOES make a system call and thus SHOULD NOT be called
32     from an error handler.
33 
34 @*/
35 PetscErrorCode PetscGetDate(char date[], size_t len) {
36   char *str = NULL;
37 #if defined(PETSC_HAVE_TIME)
38   time_t aclock;
39 #else
40   struct timeval tp;
41 #endif
42 
43   PetscFunctionBegin;
44 #if defined(PETSC_HAVE_TIME)
45   time(&aclock);
46   PetscCall(PetscStrncpy(date, asctime(localtime(&aclock)), len));
47 #else
48   gettimeofday(&tp, (struct timezone *)0);
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(0);
55 }
56