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