xref: /petsc/src/sys/time/fdate.c (revision f1580f4e3ce5d5b2393648fd039d0d41b440385d)
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 /*@C
12     PetscGetDate - Gets the current date.
13 
14    Not collective
15 
16   Input Parameter:
17 .  len - length of string to hold date
18 
19   Output Parameter:
20 .  date - the date
21 
22   Level: beginner
23 
24   Note:
25   This function makes a system call and thus SHOULD NOT be called from an error handler.
26 
27   Developer Note:
28   This function is called once during `PetscInitialize()`.
29   It stashes the timestamp, and uses it when needed. This is so that
30   error handlers may report the date without generating possible
31   additional system errors during the call to get the date.
32 
33 @*/
34 PetscErrorCode PetscGetDate(char date[], size_t len) {
35   char *str = NULL;
36 #if defined(PETSC_HAVE_TIME)
37   time_t aclock;
38 #else
39   struct timeval tp;
40 #endif
41 
42   PetscFunctionBegin;
43 #if defined(PETSC_HAVE_TIME)
44   time(&aclock);
45   PetscCall(PetscStrncpy(date, asctime(localtime(&aclock)), len));
46 #else
47   gettimeofday(&tp, (struct timezone *)0);
48   PetscCall(PetscStrncpy(date, asctime(localtime((time_t *)&tp.tv_sec)), len));
49 #endif
50   /* now strip out the new-line chars at the end of the string */
51   PetscCall(PetscStrstr(date, "\n", &str));
52   if (str) str[0] = 0;
53   PetscFunctionReturn(0);
54 }
55