]>
Commit | Line | Data |
---|---|---|
8b7215ac JB |
1 | --- links-2.1pre14/builtin.c.orig 2003-04-30 10:41:30.000000000 +0200 |
2 | +++ links-2.1pre14/builtin.c 2003-11-15 18:05:59.987606816 +0100 | |
3 | @@ -15,6 +15,10 @@ | |
4 | #undef __USE_XOPEN | |
5 | #include <ctype.h> | |
6 | ||
7 | +#ifdef HAVE_SYS_TIME_H | |
8 | +#include <sys/time.h> | |
9 | +#endif | |
10 | + | |
11 | #ifdef JS | |
12 | ||
13 | #define PREFIX 0x40 | |
14 | @@ -704,17 +708,36 @@ | |
15 | */ | |
16 | ||
17 | static struct tm * casek; | |
18 | +static unsigned short casek_msec; | |
19 | ||
20 | int vartoint(lns*pna,js_context*context); | |
21 | ||
22 | +/* sets casek and casek_msec acc. to localtime */ | |
23 | +void casek_localtime(void) | |
24 | +{ time_t*cas=js_mem_alloc(sizeof(time_t)); | |
25 | +#ifdef HAVE_GETTIMEOFDAY | |
26 | + struct timeval *tv=js_mem_alloc(sizeof(struct timeval)); | |
27 | + int res; | |
28 | + if(!(res = gettimeofday(tv, NULL))) { | |
29 | + *cas=tv->tv_sec; | |
30 | + casek_msec=tv->tv_usec/1000; | |
31 | + } | |
32 | + js_mem_free(tv); | |
33 | + if(res) /* gettimeofday() failed, fallback to time() */ | |
34 | +#endif | |
35 | + { | |
36 | + time(cas); | |
37 | + casek_msec=0; | |
38 | + } | |
39 | + casek=localtime(cas); | |
40 | + js_mem_free(cas); | |
41 | +} | |
42 | + | |
43 | void sezer_zizalu(char*argv,js_context*context) | |
44 | { char*naargy=js_mem_alloc(strlen(argv)+2); | |
45 | - time_t*cas=js_mem_alloc(sizeof(time_t)); | |
46 | + casek_localtime(); | |
47 | strcpy(naargy,argv); | |
48 | strcat(naargy,"~"); | |
49 | - time(cas); | |
50 | - casek=localtime(cas); | |
51 | - js_mem_free(cas); | |
52 | #ifdef HAVE_STRPTIME | |
53 | if(naargy[0]>57)/*Je to mon,day,year...*/ | |
54 | { if(!strptime(naargy,"%h %d, %Y, %R:%S~",casek)) | |
55 | @@ -756,7 +779,6 @@ | |
56 | long pomint, pomint1=0, pomint2; | |
f001cafa JB |
57 | long*pompointer; |
58 | int i=0,j=0; | |
8b7215ac | 59 | - time_t*cas; |
f001cafa JB |
60 | char pomchar; |
61 | ||
62 | plns*pomns; | |
8b7215ac JB |
63 | @@ -1237,18 +1259,20 @@ |
64 | { sezer_zizalu((char*)pomarg->argument,context); | |
65 | js_mem_free(pomarg); | |
66 | } | |
67 | - else{ pomint1=to32int(pomarg,context); | |
68 | + else{ float pomf=rint(tofloat(pomarg,context)); | |
69 | + /* can be >32 bits */ | |
f001cafa | 70 | if(!(pomarg=getarg(&argy))) |
8b7215ac JB |
71 | - {/* pomint=to32int(pomarg,context);*/ |
72 | - time_t t = (time_t)pomint1; | |
73 | + { | |
74 | + /* milliseconds since Epoch */ | |
75 | + time_t t = (time_t)(pomf/1000); | |
f001cafa | 76 | casek=localtime(&t); |
8b7215ac | 77 | + casek_msec=((long long)pomf)%1000; |
f001cafa JB |
78 | /* Je to doba od usvitu dejin a aby toho nebylo malo tak v sekundach :-( */ |
79 | } | |
80 | else | |
8b7215ac JB |
81 | - { cas=js_mem_alloc(sizeof(time_t)); |
82 | - time(cas); | |
83 | - casek=localtime(cas); | |
84 | - js_mem_free(cas); | |
85 | + { | |
86 | + pomint1=(int)pomf; | |
87 | + casek_localtime(); | |
f001cafa | 88 | casek->tm_year=pomint1; |
8b7215ac JB |
89 | casek->tm_mon=to32int(pomarg,context); |
90 | if((pomarg=getarg(&argy))) | |
91 | @@ -1263,10 +1287,8 @@ | |
92 | js_error("Missing day!",context); | |
93 | } | |
f001cafa | 94 | } |
8b7215ac JB |
95 | - } else{ cas=js_mem_alloc(sizeof(time_t)); |
96 | - *cas=time(0); | |
97 | - casek=localtime(cas); | |
98 | - js_mem_free(cas); | |
99 | + } else{ | |
100 | + casek_localtime(); | |
f001cafa | 101 | if(pomarg) delarg(pomarg,context); |
8b7215ac JB |
102 | } |
103 | /* Hopla mizero - a mame casek */ | |
104 | @@ -1297,10 +1319,17 @@ | |
f001cafa JB |
105 | BUILDFCE("getSeconds",CgetSeconds); |
106 | pomvar->handler=(long)pompointer; | |
107 | BUILDFCE("setTime",CsetTime); | |
108 | - pomvar->handler=casek->tm_sec; | |
8b7215ac JB |
109 | - pompointer=&pomvar->handler; |
110 | + /* float because (long) int is too small on 32-bit machines */ | |
111 | + pomfloat=js_mem_alloc(sizeof(float)); | |
112 | + *pomfloat=((float)mktime(casek))*1000 + casek_msec; | |
113 | + /* embed it in variable to avoid memory leaks - is any better way? */ | |
114 | + pomvar1=buildin("settimetempval",context->namespace,context->lnamespace,context); | |
115 | + pomvar1->type=INTVAR; | |
116 | + pomvar1->value=pomvar->mid=0; | |
117 | + pomvar1->handler=(long)pomfloat; | |
118 | + pomvar->handler=(long)pomfloat; | |
f001cafa JB |
119 | BUILDFCE("getTime",CgetTime); |
120 | - pomvar->handler=(long)pomns; | |
8b7215ac | 121 | + pomvar->handler=(long)pomfloat; |
f001cafa JB |
122 | BUILDFCE("setYear",CsetYear); |
123 | pomvar->handler=casek->tm_year; | |
124 | pompointer=&(pomvar->handler); | |
8b7215ac JB |
125 | @@ -2211,13 +2240,14 @@ |
126 | idebug("CsetTime called "); | |
127 | if(!(pomarg=getarg(&argy))) | |
128 | js_error("setTime function requires argument!",context); | |
129 | - else variable->handler=to32int(pomarg,context); | |
130 | + else *(float*)(variable->handler)=tofloat(pomarg,context); | |
131 | idebug("and exited!\n"); | |
f001cafa JB |
132 | break; |
133 | case CgetTime: | |
134 | idebug("CgetTime called "); | |
135 | - rettype=INTEGER; | |
136 | - retval=*(long*)variable->handler; | |
137 | + rettype=FLOAT; | |
138 | + retval=(long)js_mem_alloc(sizeof(float)); | |
8b7215ac | 139 | + *(float*)retval=*(float*)variable->handler; |
f001cafa JB |
140 | idebug("and exited!\n"); |
141 | break; | |
142 | case CsetYear: |