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