OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [native/] [jni/] [java-util/] [java_util_VMTimeZone.c] - Blame information for rev 774

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 774 jeremybenn
/* VMTimeZone.c - Native method for java.util.VMTimeZone
2
   Copyright (C) 1999, 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
#include "config.h"
39
 
40
#if TIME_WITH_SYS_TIME
41
# include <sys/time.h>
42
# include <time.h>
43
#else
44
# if HAVE_SYS_TIME_H
45
#  include <sys/time.h>
46
# else
47
#  include <time.h>
48
# endif
49
#endif
50
 
51
#include <stdio.h>
52
#include <string.h>
53
#include <stdlib.h>
54
 
55
#include <jni.h>
56
 
57
#include "java_util_VMTimeZone.h"
58
 
59
static size_t jint_to_charbuf (char *bufend, jint num);
60
 
61
/**
62
 * This method returns a time zone id string which is in the form
63
 * (standard zone name) or (standard zone name)(GMT offset) or
64
 * (standard zone name)(GMT offset)(daylight time zone name).  The
65
 * GMT offset can be in seconds, or where it is evenly divisible by
66
 * 3600, then it can be in hours.  The offset must be the time to
67
 * add to the local time to get GMT.  If a offset is given and the
68
 * time zone observes daylight saving then the (daylight time zone
69
 * name) must also be given (otherwise it is assumed the time zone
70
 * does not observe any daylight savings).
71
 * <p>
72
 * The result of this method is given to getDefaultTimeZone(String)
73
 * which tries to map the time zone id to a known TimeZone.  See
74
 * that method on how the returned String is mapped to a real
75
 * TimeZone object.
76
 */
77
JNIEXPORT jstring JNICALL
78
Java_java_util_VMTimeZone_getSystemTimeZoneId (JNIEnv * env,
79
                                               jclass clazz
80
                                               __attribute__ ((__unused__)))
81
{
82
  struct tm tim;
83
#ifndef HAVE_LOCALTIME_R
84
  struct tm *lt_tim;
85
#endif
86
#ifdef HAVE_TM_ZONE
87
  int month;
88
#endif
89
  time_t current_time;
90
  long tzoffset;
91
  const char *tz1, *tz2;
92
  char tzoff[11];
93
  size_t tz1_len, tz2_len, tzoff_len;
94
  char *tzid;
95
  jstring retval;
96
 
97
  time (&current_time);
98
#ifdef HAVE_LOCALTIME_R
99
  localtime_r (&current_time, &tim);
100
#else
101
  /* Fall back on non-thread safe localtime. */
102
  lt_tim = localtime (&current_time);
103
  memcpy (&tim, lt_tim, sizeof (struct tm));
104
#endif
105
  mktime (&tim);
106
 
107
#ifdef HAVE_STRUCT_TM_TM_ZONE
108
  /* We will cycle through the months to make sure we hit dst. */
109
  month = tim.tm_mon;
110
  tz1 = tz2 = NULL;
111
  while (tz1 == NULL || tz2 == NULL)
112
    {
113
      if (tim.tm_isdst > 0)
114
        tz2 = tim.tm_zone;
115
      else if (tz1 == NULL)
116
        {
117
          tz1 = tim.tm_zone;
118
          month = tim.tm_mon;
119
        }
120
 
121
      if (tz1 == NULL || tz2 == NULL)
122
        {
123
          tim.tm_mon++;
124
          tim.tm_mon %= 12;
125
        }
126
 
127
      if (tim.tm_mon == month && tz2 == NULL)
128
        tz2 = "";
129
      else
130
        mktime (&tim);
131
    }
132
  /* We want to make sure the tm struct we use later on is not dst. */
133
  tim.tm_mon = month;
134
  mktime (&tim);
135
#elif defined (HAVE_TZNAME)
136
  /* If dst is never used, tzname[1] is the empty string. */
137
  tzset ();
138
  tz1 = tzname[0];
139
  tz2 = tzname[1];
140
#else
141
  /* Some targets have no concept of timezones. Assume GMT without dst. */
142
  tz1 = "GMT";
143
  tz2 = "";
144
#endif
145
 
146
#ifdef STRUCT_TM_HAS_GMTOFF
147
  /* tm_gmtoff is the number of seconds that you must add to GMT to get
148
     local time, we need the number of seconds to add to the local time
149
     to get GMT. */
150
  tzoffset = -1L * tim.tm_gmtoff;
151
#elif HAVE_UNDERSCORE_TIMEZONE
152
  /* On some systems _timezone is actually defined as time_t. */
153
  tzoffset = (long) _timezone;
154
#elif HAVE_TIMEZONE
155
  /* timezone is secs WEST of UTC. */
156
  tzoffset = timezone;
157
#else
158
  /* FIXME: there must be another global if neither tm_gmtoff nor timezone
159
     is available, esp. if tzname is valid.
160
     Richard Earnshaw <rearnsha@arm.com> has suggested using difftime to
161
     calculate between gmtime and localtime (and accounting for possible
162
     daylight savings time) as an alternative. */
163
  tzoffset = 0L;
164
#endif
165
 
166
  if ((tzoffset % 3600) == 0)
167
    tzoffset = tzoffset / 3600;
168
 
169
  tz1_len = strlen (tz1);
170
  tz2_len = strlen (tz2);
171
  tzoff_len = jint_to_charbuf (tzoff + 11, tzoffset);
172
  tzid = (char *) malloc (tz1_len + tz2_len + tzoff_len + 1);   /* FIXME alloc */
173
  memcpy (tzid, tz1, tz1_len);
174
  memcpy (tzid + tz1_len, tzoff + 11 - tzoff_len, tzoff_len);
175
  memcpy (tzid + tz1_len + tzoff_len, tz2, tz2_len);
176
  tzid[tz1_len + tzoff_len + tz2_len] = '\0';
177
 
178
  retval = (*env)->NewStringUTF (env, tzid);
179
  free (tzid);
180
 
181
  return retval;
182
}
183
 
184
/* Put printed (decimal) representation of NUM in a buffer.
185
   BUFEND marks the end of the buffer, which must be at least 11 chars long.
186
   Returns the COUNT of chars written.  The result is in
187
   (BUFEND - COUNT) (inclusive) upto (BUFEND) (exclusive).
188
 
189
   Note that libgcj has a slightly different version called _Jv_FormatInt
190
   that works on jchar buffers.
191
*/
192
 
193
static size_t
194
jint_to_charbuf (char *bufend, jint num)
195
{
196
  register char *ptr = bufend;
197
  jboolean isNeg;
198
  if (num < 0)
199
    {
200
      isNeg = JNI_TRUE;
201
      num = -(num);
202
      if (num < 0)
203
        {
204
          /* Must be MIN_VALUE, so handle this special case.
205
             FIXME use 'unsigned jint' for num. */
206
          *--ptr = '8';
207
          num = 214748364;
208
        }
209
    }
210
  else
211
    isNeg = JNI_FALSE;
212
 
213
  do
214
    {
215
      *--ptr = (char) ((int) '0' + (num % 10));
216
      num /= 10;
217
    }
218
  while (num > 0);
219
 
220
  if (isNeg)
221
    *--ptr = '-';
222
  return bufend - ptr;
223
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.