summaryrefslogtreecommitdiff
path: root/libjava/java/util/natDate.cc
blob: 563f8c0cd8e9d5c3714c57e95b61cdaf4f63cc49 (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
/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>

// We want to make sure to pick up the POSIX ctime_r.  Some systems,
// such as Solaris 2.6, have their own version as well.
#ifdef HAVE_CTIME_R
#define _POSIX_PTHREAD_SEMANTICS
#endif

#include <cni.h>
#include <java/util/Date.h>
#include <java/lang/String.h>

#include <time.h>

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#if HAVE_CTIME_R
/* Use overload resolution to find out the signature of ctime_r.  */

  /* This is Posix ctime_r().  */
template <typename T_clock, typename T_buf, size_t buflen>
static inline char *
ctime_adaptor (char* (*ctime_r)(T_clock *clock, T_buf *buf),
	       time_t *clock, char (&buf)[buflen])
{
  return ctime_r(clock, buf);
}

/* This is an old-style ctime_r, used on IRIX 5.2.  */
template <typename T_clock, typename T_buf, typename T_buflen, size_t buflen>
static inline char *
ctime_adaptor (char* (*ctime_r)(T_clock *clock, T_buf *buf, T_buflen len),
	       time_t *clock, char (&buf)[buflen])
{
  return ctime_r(clock, buf, buflen);
}
#endif

jstring
java::util::Date::toString()
{
#ifdef HAVE_CTIME_R
  time_t t = millis / 1000;
  char buf[30];
  return JvNewStringLatin1 (ctime_adaptor (ctime_r, &t, buf));
#elif defined (HAVE_CTIME)
  // FIXME: this isn't thread-safe.
  time_t t = millis / 1000;
  return JvNewStringLatin1 (ctime (&t));
#else
  return NULL;
#endif
}