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