This is Unofficial EPICS BASE Doxygen Site
tz2timezone.c File Reference
#include <vxWorks.h>
#include <envLib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <epicsTime.h>
+ Include dependency graph for tz2timezone.c:

Go to the source code of this file.

Functions

int tz2timezone (void)
 

Function Documentation

int tz2timezone ( void  )

Definition at line 158 of file tz2timezone.c.

159 {
160  const char *tz = getenv("TZ");
161  /* Spec. says that zone names must be at least 3 chars.
162  * I've never seen a longer zone name, but I'll allocate
163  * 40 chars. If you live in a zone with a longer name,
164  * you may want to think about the benefits of relocation.
165  */
166  char zone[40];
167  char start[10], stop[10]; /* only really need 7 bytes now */
168  int hours = 0, minutes = 0, sign = 1;
169  /* This is more than enough, even with a 40-char zone
170  * name, and 4-char offset.
171  */
172  char timezone[100];
173  int i = 0; /* You *always need an "i" :-) */
174  epicsTimeStamp now;
175  struct tm current;
176 
177  /* First let's get the current time. We need the year to
178  * compute the start/stop dates for DST.
179  */
180  if (epicsTimeGetCurrent(&now) ||
181  epicsTimeToTM(&current, NULL, &now))
182  return ERROR;
183 
184  /* Make sure TZ exists.
185  * Spec. says that ZONE must be at least 3 chars.
186  */
187  if ((!tz) || (strlen(tz) < 3))
188  return ERROR;
189 
190  /* OK, now a bunch of brute-force parsing. My brain hurts if
191  * I try to think of an elegant regular expression for the
192  * string.
193  */
194 
195  /* Start extracting zone name, must be alpha */
196  while ((i < sizeof(zone) - 1) && isalpha((int) *tz)) {
197  zone[i++] = *tz++;
198  }
199  if (i < 3)
200  return ERROR; /* Too short, not a real zone name? */
201 
202  zone[i] = 0; /* Nil-terminate (for now) */
203 
204  /* Now extract offset time. The format is [+/-]hh[:mm[:ss]]
205  * Recall that TIMEZONE doesn't support seconds....
206  */
207  if (*tz == '-') {
208  sign = -1;
209  tz++;
210  }
211  else if (*tz == '+') {
212  tz++;
213  }
214 
215  /* Need a digit now */
216  if (!isdigit((int) *tz))
217  return ERROR;
218 
219  /* First get the hours */
220  tz = getTime(tz, &hours);
221  if (hours > 24)
222  return ERROR;
223 
224  if (*tz == ':') { /* There is a minutes part */
225  /* Need another digit now */
226  if (!isdigit((int) *++tz))
227  return ERROR;
228 
229  /* Extract the minutes */
230  tz = getTime(tz, &minutes);
231  if (minutes > 60)
232  return ERROR;
233 
234  /* Skip any seconds part */
235  if (*tz == ':') {
236  int seconds;
237  tz = getTime(tz + 1, &seconds);
238  }
239  }
240 
241  /* Extract any DST zone name, must be alpha */
242  if (isalpha((int) *tz)) {
243  zone[i++] = '/'; /* Separate the names */
244 
245  while ((i < sizeof(zone) - 1) && isalpha((int) *tz)) {
246  zone[i++] = *tz++;
247  }
248  zone[i] = 0; /* Nil-terminate */
249  }
250 
251  minutes += hours * 60;
252  minutes *= sign;
253 
254  /* Look for start/stop dates - require neither or both */
255  tz = strchr(tz, ',');
256  if (!tz) { /* No daylight savings time here */
257  /* Format the env. variable */
258  sprintf(timezone, "TIMEZONE=%s::%d", zone, minutes);
259  }
260  else {
261  if (extractDate(tz, &current, start) != OK)
262  return ERROR;
263 
264  tz = strchr(tz + 1, ',');
265  if (!tz)
266  return ERROR;
267  if (extractDate(tz, &current, stop) != OK)
268  return ERROR;
269 
270  /* Format the env. variable */
271  sprintf(timezone, "TIMEZONE=%s::%d:%s:%s", zone, minutes, start, stop);
272  }
273 
274  /* Make it live! */
275  putenv(timezone);
276 
277  return OK;
278 }
int i
Definition: scan.c:967
#define NULL
Definition: catime.c:38
int epicsStdCall epicsTimeGetCurrent(epicsTimeStamp *pDest)
Get current time into *pDest.
int putenv(char *)
EPICS time stamp, for use from C code.
Definition: epicsTime.h:33
LIBCOM_API int epicsStdCall epicsTimeToTM(struct tm *pDest, unsigned long *pNSecDest, const epicsTimeStamp *pSrc)
Convert epicsTimeStamp to struct tm in local time zone.
Definition: epicsTime.cpp:956