1/* Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
2This file is part of GNU Fortran libU77 library.
3
4This library is free software; you can redistribute it and/or modify it
5under the terms of the GNU Library General Public License as published
6by the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9GNU Fortran is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with GNU Fortran; see the file COPYING.LIB.  If
16not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17Boston, MA 02111-1307, USA.  */
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22#include <stdio.h>
23#if TIME_WITH_SYS_TIME
24# include <sys/time.h>
25# include <time.h>
26#else
27# if HAVE_SYS_TIME_H
28#  include <sys/time.h>
29# else
30#  include <time.h>
31# endif
32#endif
33#include "f2c.h"
34
35#ifdef KR_headers
36VOID s_copy ();
37#else
38void s_copy(register char *a, register char *b, ftnlen la, ftnlen lb);
39#endif
40
41int G77_date_and_time_0 (char *date, char *fftime, char *zone,
42			 integer *values, ftnlen date_len,
43			 ftnlen fftime_len, ftnlen zone_len)
44{
45  time_t lt=time(&lt);
46  struct tm ltime = *localtime(&lt), gtime = *gmtime(&lt);
47  char dat[9], zon[6], ftim[11];
48  int i, vals[8];
49
50  vals[0] = 1900 + ltime.tm_year;
51  vals[1] = 1 + ltime.tm_mon;
52  vals[2] = ltime.tm_mday;
53  /* fixme: year boundaries */
54  vals[3] = (ltime.tm_min - gtime.tm_min +
55	     60*(ltime.tm_hour - gtime.tm_hour +
56		 24*(ltime.tm_yday -gtime.tm_yday)));
57  vals[4] = ltime.tm_hour;
58  vals[5] = ltime.tm_min;
59  vals[6] = ltime.tm_sec;
60  vals[7] = 0;                  /* no STDC way to get this */
61  /* GNUish way; maybe use `ftime' on other systems. */
62#if HAVE_GETTIMEOFDAY
63  {
64    struct timeval tp;
65#if HAVE_STRUCT_TIMEZONE
66    struct timezone tzp;
67    /* This is still not strictly correct on some systems such as HPUX,
68       which does have struct timezone, but gettimeofday takes void* as
69       the 2nd arg.  However, the effect of passing anything other than a null
70       pointer is unspecified on HPUX. */
71    if (! gettimeofday (&tp, &tzp))
72#else
73    if (! gettimeofday (&tp, (void *) 0))
74#endif
75      vals[7] = tp.tv_usec/1000;
76  }
77#endif
78  if (values)			/* null pointer for missing optional */
79    for (i=0; i<=7; i++)
80      values[i] = vals[i];
81  sprintf (dat, "%04d%02d%02d", vals[0], vals[1], vals[2]);
82  s_copy(date, dat, date_len, 8);
83  if (zone) {
84    sprintf(zon, "%+03d%02d", vals[3] / 60, abs(vals[3] % 60));
85    s_copy(zone, zon, zone_len, 5);
86  }
87  if (fftime) {
88    sprintf (ftim, "%02d%02d%02d.%03d", vals[4], vals[5], vals[6], vals[7]);
89    s_copy(fftime, ftim, fftime_len, 10);
90  }
91  return 0;
92}
93