1 /*
2  * Copyright (C) 2001-2005 Chris Ross, Stephan Engstrom, Alex Holden et al
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * o Neither the name of the ferite software nor the names of its contributors may
14  *   be used to endorse or promote products derived from this software without
15  *   specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "util_date.h"
31 #include "../stream/util_stream.h"
32 
system_call_tm(FeriteScript * script,struct tm * tm)33 FeriteVariable *system_call_tm( FeriteScript *script, struct tm *tm )
34 {
35     FeriteVariable *object = NULL, *pointer = NULL, **args = NULL;
36     FeriteClass *cls = NULL;
37     FeriteFunction *function = NULL;
38 
39     if((cls = ferite_find_class( script, script->mainns, "Date" )) != NULL)
40     {
41         pointer = system_create_pointer_var( script, "struct::tm", tm );
42         object = ferite_new_object( script, cls, NULL );
43         function = ferite_object_get_function( script, VAO(object), "__RegisterFromPointer__" );
44         args = ferite_create_parameter_list_from_data( script, "o", VAO(pointer) );
45         ferite_variable_destroy( script, ferite_call_function( script, VAO(object), NULL, function, args ) );
46         ferite_variable_destroy( script, pointer );
47         ferite_delete_parameter_list( script, args );
48         FE_RETURN_VAR( object );
49     }
50     FE_RETURN_NULL_OBJECT;
51 }
system_sync_to_tm(struct FeTm * Tm,struct tm * tm)52 int system_sync_to_tm( struct FeTm *Tm, struct tm *tm )
53 {
54     memset( tm, '\0', sizeof(struct tm) );
55     tm->tm_sec   = VAI(Tm->tm_sec);
56     tm->tm_min   = VAI(Tm->tm_min);
57     tm->tm_hour  = VAI(Tm->tm_hour);
58     tm->tm_mday  = VAI(Tm->tm_mday);
59     tm->tm_mon   = VAI(Tm->tm_mon) - 1;
60     tm->tm_year  = VAI(Tm->tm_year) - 1900;
61     tm->tm_wday  = VAI(Tm->tm_wday);
62     tm->tm_yday  = VAI(Tm->tm_yday);
63     tm->tm_isdst = VAI(Tm->tm_isdst);
64 #if !defined(USING_SOLARIS) && !defined(USING_CYGWIN) && !defined(USING_MINGW)
65     tm->tm_zone  = VAS(Tm->tm_zone)->data;
66     tm->tm_gmtoff = VAI(Tm->tm_gmtoff);
67 #endif
68     return 0;
69 }
70 
system_sync_to_FeTm(struct FeTm * Tm,struct tm * tm)71 int system_sync_to_FeTm( struct FeTm *Tm, struct tm *tm )
72 {
73 #if !defined(USING_SOLARIS) && !defined(USING_CYGWIN) && !defined(USING_MINGW)
74 	char *zone = (char *)tm->tm_zone;
75 #endif
76 
77     VAI(Tm->tm_sec)   = tm->tm_sec;
78     VAI(Tm->tm_min)   = tm->tm_min;
79     VAI(Tm->tm_hour)  = tm->tm_hour;
80     VAI(Tm->tm_mday)  = tm->tm_mday;
81     VAI(Tm->tm_mon)   = tm->tm_mon + 1;
82     VAI(Tm->tm_year)  = tm->tm_year + 1900;
83     VAI(Tm->tm_wday)  = tm->tm_wday;
84     VAI(Tm->tm_yday)  = tm->tm_yday;
85     VAI(Tm->tm_isdst) = tm->tm_isdst;
86 #if !defined(USING_SOLARIS) && !defined(USING_CYGWIN) && !defined(USING_MINGW)
87     VAI(Tm->tm_gmtoff) = tm->tm_gmtoff;
88     ferite_str_destroy( VAS(Tm->tm_zone) );
89     VAS(Tm->tm_zone) = ferite_str_new( zone, 0, FE_CHARSET_DEFAULT );
90 #endif
91     return 0;
92 }
93 
94 #define FE_SYS_ALARM_OWNER 1
95 AlarmData *currentAlarm;
timer_sig_alarm(int signum)96 void timer_sig_alarm( int signum )
97 {
98 #if !defined(USING_MINGW)
99     if( currentAlarm != NULL )
100     {
101         ferite_script_function_execute( currentAlarm->script, currentAlarm->script->mainns, NULL, currentAlarm->function, NULL );
102         if( currentAlarm->recurring )
103           alarm( currentAlarm->interval );
104     }
105 #endif
106 }
107