Initial revision
[cacao.git] / nat / util.c
1 /***************************** nat/util.c **************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Contains the native functions for class java.util.
8
9         Authors: Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
10
11         Last Change: 1996/11/14
12
13 *******************************************************************************/
14
15 #include <time.h>
16
17 /**************************** java.util.Date **********************************/
18
19
20 struct java_lang_String* java_util_Date_toString 
21         (struct java_util_Date* this) 
22 {
23         time_t tt;
24         char *buffer;
25         int l;
26         
27         if (!this->valueValid) java_util_Date_computeValue(this);
28         tt = builtin_l2i ( builtin_ldiv(this->value, builtin_i2l(1000) ) );
29
30         buffer = ctime (&tt);
31         l = strlen(buffer);
32         while (l>1 && buffer[l-1]=='\n') l--;
33         buffer[l] = '\0';
34         
35         return (java_lang_String*) javastring_new_char( buffer );
36 }
37
38
39 struct java_lang_String* java_util_Date_toLocaleString 
40         (struct java_util_Date* this)
41 {
42         time_t tt;
43         char *buffer;
44         int l;
45
46         if (!this->valueValid) java_util_Date_computeValue(this);
47         tt = builtin_l2i ( builtin_ldiv(this->value, builtin_i2l(1000) ) );
48
49         buffer = asctime (localtime(&tt));
50         l = strlen(buffer);
51         while (l>1 && buffer[l-1]=='\n') l--;
52         buffer[l] = '\0';
53
54         return (java_lang_String*) javastring_new_char ( buffer );
55 }
56
57 struct java_lang_String* java_util_Date_toGMTString 
58         (struct java_util_Date* this)
59 {
60         time_t tt;
61         char *buffer;
62         int l;
63
64         if (!this->valueValid) java_util_Date_computeValue(this);
65         tt = builtin_l2i ( builtin_ldiv(this->value, builtin_i2l(1000) ) );
66
67         buffer = asctime (gmtime(&tt));
68         l = strlen(buffer);
69         while (l>1 && buffer[l-1]=='\n') l--;
70         buffer[l] = '\0';
71         
72         
73         return (java_lang_String*) javastring_new_char ( buffer );
74 }
75
76
77
78 void java_util_Date_expand (struct java_util_Date* this)
79 {
80         struct tm *t;
81         time_t tt;
82         
83         if (this->expanded) return;
84
85         tt = builtin_l2i ( builtin_ldiv(this->value, builtin_i2l(1000) ) );
86         t = gmtime (&tt);
87         this->tm_millis = 
88           builtin_l2i( builtin_lrem (this->value, builtin_i2l(1000) ) );
89         this->tm_sec = t->tm_sec;
90         this->tm_min = t->tm_min;
91         this->tm_hour = t->tm_hour;
92         this->tm_mday = t->tm_mday;
93         this->tm_mon = t->tm_mon;
94         this->tm_wday = t->tm_wday;
95         this->tm_yday = t->tm_yday;
96         this->tm_year = t->tm_year;
97         this->tm_isdst = t->tm_isdst;
98         this->expanded = 1;
99 }
100
101 void java_util_Date_computeValue (struct java_util_Date* this)
102 {
103         struct tm t;
104         
105         if (this->valueValid) return;
106         t.tm_sec = this->tm_sec;
107         t.tm_min = this->tm_min;
108         t.tm_hour = this->tm_hour;
109         t.tm_mday = this->tm_mday;
110         t.tm_mon = this->tm_mon;
111         t.tm_wday = this->tm_wday;
112         t.tm_yday = this->tm_yday;
113         t.tm_year = this->tm_year;
114         t.tm_isdst = this->tm_isdst;
115         this->value = 
116           builtin_ladd( 
117            builtin_lmul( builtin_i2l(mktime(&t)), builtin_i2l(1000)), 
118            builtin_i2l(this->tm_millis)
119           );
120         this->valueValid = 1;   
121 }