* All files: Updated copyright to reflect Cygnus purchase.
[official-gcc.git] / libjava / java / util / natGregorianCalendar.cc
blobf9d208d667c8dea5c3d2a34e1fb2ba1dddcd3c67
1 /* Copyright (C) 1998, 1999 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 #include <config.h>
11 // We want to make sure to pick up the POSIX `_r' functions. Some
12 // systems, such as Solaris 2.6, require this define in order to
13 // declare the functions in the appropriate header.
14 #if defined (HAVE_GMTIME_R) || defined (HAVE_LOCALTIME_R)
15 # define _POSIX_PTHREAD_SEMANTICS
16 # ifndef _REENTRANT
17 # define _REENTRANT
18 # endif /* _REENTRANT */
19 #endif
21 #ifdef ECOS
22 #include <string.h>
23 #endif
25 #include <gcj/cni.h>
26 #include <java/util/TimeZone.h>
27 #include <java/util/GregorianCalendar.h>
28 #include <time.h>
30 void
31 java::util::GregorianCalendar::computeTime ()
33 struct tm tim;
34 tim.tm_sec = elements(fields)[SECOND];
35 tim.tm_min = elements(fields)[MINUTE];
36 tim.tm_hour = elements(fields)[HOUR_OF_DAY];
37 tim.tm_mday = elements(fields)[DATE];
38 tim.tm_mon = elements(fields)[MONTH];
39 tim.tm_year = elements(fields)[YEAR] - 1900;
40 tim.tm_isdst = 0; // FIXME
41 #ifndef ECOS
42 // FIXME: None of the standard C library access to the ECOS calendar
43 // is yet available.
44 time_t t = mktime (&tim);
45 #else
46 time_t t = 0;
47 #endif
49 // Adjust for local timezone (introduced by mktime) and our
50 // timezone.
51 #if defined (STRUCT_TM_HAS_GMTOFF)
52 t += tim.tm_gmtoff;
53 #elif defined (HAVE_TIMEZONE)
54 t -= timezone;
55 #endif
56 java::util::TimeZone *zone = getTimeZone ();
57 t += zone->getRawOffset();
59 // Adjust for milliseconds.
60 time = t * (jlong) 1000 + elements(fields)[MILLISECOND];
62 isTimeSet = true;
65 void
66 java::util::GregorianCalendar::computeFields ()
68 time_t t = time / 1000;
69 int millis = time % 1000;
70 if (t < 0 && millis != 0)
72 t--;
73 millis = t - 1000 * t;
75 elements(fields)[MILLISECOND] = millis;
76 struct tm tim;
77 java::util::TimeZone *zone = getTimeZone ();
79 // FIXME: None of the standard C library access to the ECOS calendar
80 // is yet available.
81 #ifdef ECOS
82 memset (&tim, 0, sizeof tim);
83 #else
84 if (zone->getRawOffset() == 0 || ! zone->useDaylightTime())
86 #if defined(__JV_POSIX_THREADS__) && defined(HAVE_GMTIME_R)
87 gmtime_r (&t, &tim);
88 #else
89 // Get global lock (because gmtime uses a global buffer). FIXME
90 tim = *(struct tm*) gmtime (&t);
91 // Release global lock. FIXME
92 #endif
94 else
96 #if defined(__JV_POSIX_THREADS__) && defined(HAVE_LOCALTIME_R)
97 localtime_r (&t, &tim);
98 #else
99 // Get global lock (because localtime uses a global buffer). FIXME
100 tim = *(struct tm*) localtime (&t);
101 // Release global lock. FIXME
102 #endif
104 #endif /* ECOS */
105 elements(fields)[SECOND] = tim.tm_sec;
106 elements(fields)[MINUTE] = tim.tm_min;
107 elements(fields)[HOUR_OF_DAY] = tim.tm_hour;
108 elements(fields)[AM_PM] = tim.tm_hour < 12 ? AM : PM;
109 elements(fields)[HOUR] = tim.tm_hour % 12;
110 elements(fields)[DATE] = tim.tm_mday;
111 elements(fields)[MONTH] = tim.tm_mon;
112 elements(fields)[YEAR] = 1900 + tim.tm_year;
113 elements(fields)[DAY_OF_WEEK] = tim.tm_wday + 1;
114 elements(fields)[DAY_OF_WEEK_IN_MONTH] = ((tim.tm_mday - 1) / 7) + 1;
115 elements(fields)[DAY_OF_YEAR] = tim.tm_yday + 1;
116 elements(fields)[WEEK_OF_MONTH]
117 = (tim.tm_mday + 6 + (5 - tim.tm_wday + getFirstDayOfWeek()) % 7) / 7;
118 elements(fields)[WEEK_OF_YEAR]
119 = (tim.tm_yday + 7 + (5 - tim.tm_wday + getFirstDayOfWeek()) % 7) / 7;
120 elements(fields)[ERA] = AD;
121 elements(fields)[DST_OFFSET] = tim.tm_isdst <= 0 ? 0 : 60*60*1000;
122 elements(fields)[ZONE_OFFSET] = getTimeZone()->getRawOffset();
123 areFieldsSet = true;