summaryrefslogtreecommitdiff
path: root/src/util/Time.c
blob: f71b3fdb256a6966f39a2dcf37564419c668967e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 /*@@
   @file      Time.c
   @date      Wed Sep 17 2000
   @author    Gabrielle Allen
   @desc 
   Date and time routines
   @enddesc 
   @version $Header$
 @@*/

/* #define DEBUG_TIME */

#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

#include "cctk_Flesh.h"
#include "cctk_Misc.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(util_Time_c);


/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    Util_CurrentTime
   @date       Tue Sep 19
   @author     Gabrielle Allen
   @desc 
   Fills string with current local time, returning the number
   of characters filled.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int Util_CurrentTime(int len, char *now)
{
  int retval;
  time_t timep;
  const char *fmt = "%X%z";

  timep = time(NULL);
  strftime(now, len, fmt, localtime(&timep));

  retval = strlen(now);
  retval=retval > len ? 0 : retval;

#ifdef DEBUG_TIME
  printf("CurrentTime = %s\n",now);
#endif

  return retval;
}


 /*@@
   @routine    Util_CurrentDate
   @date       Tue Sep 19
   @author     Gabrielle Allen
   @desc 
   Fills string with current local date, returning the number of
   characters filled.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/

int Util_CurrentDate(int len, char *now)
{
  int retval;
  time_t timep;
  const char *fmt = "%b %d %Y";

  timep = time(NULL);
  strftime(now, 50, fmt, localtime(&timep));

  retval = strlen(now);
  retval=retval > len ? 0 : retval;

#ifdef DEBUG_TIME
  printf("CurrentDate = %s\n",thedate);
#endif

  return retval;
}



 /*@@
   @routine    Util_CurrentDateTime
   @date       Wed 19 July 2006
   @author     Thomas Radke
   @desc
               Returns the current datetime in a machine-processable format
               as defined in ISO 8601 chapter 5.4.
   @enddesc

   @returntype char *
   @returndesc
               pointer to an allocated string buffer containing the datetime,
               must be freed by the user
   @endreturndesc
@@*/
char *Util_CurrentDateTime(void)
{
  char *buffer;
  time_t timep;
  const int len = sizeof ("YYYY-MM-DDThh:mm:ss+hh:mm");
  const char *fmt = "%Y-%m-%dT%H:%M:%S%z";

  buffer = calloc (1, len);
  if (buffer)
  {
    timep = time (NULL);
    strftime (buffer, len, fmt, localtime (&timep));

    /* if the timezone part is returned as "(+|-)hhmm"
       then turn it into "(+|-)hh:mm" */
    if ((buffer[len-7] == '+' || buffer[len-7] == '-') &&
        isdigit (buffer[len-6]) && isdigit (buffer[len-5]) &&
        isdigit (buffer[len-4]) && isdigit (buffer[len-3]) &&
        buffer[len-2] == 0)
    {
      buffer[len-2] = buffer[len-3];
      buffer[len-3] = buffer[len-4];
      buffer[len-4] = ':';
    }
  }

  return (buffer);
}