1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
(c) 1202:forum::für::umläute:2000
1506:forum::für::umläute:2003: use timeb only if needed (like on windoze)
"time" gets the current time from the system
"date" gets the current date from the system
*/
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#define USE_TIMEB
#endif
#ifdef MACOSX
#include <sys/types.h>
/* typedef _BSD_TIME_T_ time_t; */
#endif
#include "zexy.h"
#include <time.h>
#ifdef USE_TIMEB
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
/* ----------------------- time --------------------- */
static t_class *time_class;
typedef struct _time
{
t_object x_obj;
int GMT;
t_outlet *x_outlet1;
t_outlet *x_outlet2;
t_outlet *x_outlet3;
t_outlet *x_outlet4;
} t_time;
static void *time_new(t_symbol *s, int argc, t_atom *argv)
{
t_time *x = (t_time *)pd_new(time_class);
char buf[5];
x->GMT=0;
if (argc) {
atom_string(argv, buf, 5);
if (buf[0]=='G' && buf[1]=='M' && buf[2]=='T')
x->GMT = 1;
}
x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
x->x_outlet3 = outlet_new(&x->x_obj, &s_float);
x->x_outlet4 = outlet_new(&x->x_obj, &s_float);
return (x);
}
static void time_bang(t_time *x)
{
struct tm *resolvetime;
float ms = 0.f;
#ifdef USE_TIMEB
struct timeb mytime;
ftime(&mytime);
resolvetime = (x->GMT)?gmtime(&mytime.time):localtime(&mytime.time);
ms=mytime.millitm;
#else
struct timeval tv;
gettimeofday(&tv, 0);
resolvetime = (x->GMT)?gmtime(&tv.tv_sec):localtime(&tv.tv_sec);
ms = tv.tv_usec*0.001;
#endif
// outlet_float(x->x_outlet4, (t_float)(mytime.millitm));
outlet_float(x->x_outlet4, (t_float)(ms));
outlet_float(x->x_outlet3, (t_float)resolvetime->tm_sec);
outlet_float(x->x_outlet2, (t_float)resolvetime->tm_min);
outlet_float(x->x_outlet1, (t_float)resolvetime->tm_hour);
}
static void help_time(t_time *x)
{
post("\n%c time\t\t:: get the current system time", HEARTSYMBOL);
post("\noutputs are\t: hour / minute / sec / msec");
post("\ncreation\t:: 'time [GMT]': show local time or GMT");
}
void time_setup(void)
{
time_class = class_new(gensym("time"),
(t_newmethod)time_new, 0,
sizeof(t_time), 0, A_GIMME, 0);
class_addbang(time_class, time_bang);
class_addmethod(time_class, (t_method)help_time, gensym("help"), 0);
class_sethelpsymbol(time_class, gensym("zexy/time"));
}
/* ----------------------- date --------------------- */
static t_class *date_class;
typedef struct _date
{
t_object x_obj;
int GMT;
t_outlet *x_outlet1;
t_outlet *x_outlet2;
t_outlet *x_outlet3;
t_outlet *x_outlet4;
t_outlet *x_outlet5;
t_outlet *x_outlet6;
} t_date;
static void *date_new(t_symbol *s, int argc, t_atom *argv)
{
t_date *x = (t_date *)pd_new(date_class);
char buf[5];
x->GMT=0;
if (argc) {
atom_string(argv, buf, 5);
if (buf[0]=='G' && buf[1]=='M' && buf[2]=='T')
x->GMT = 1;
}
x->x_outlet1 = outlet_new(&x->x_obj, &s_float);
x->x_outlet2 = outlet_new(&x->x_obj, &s_float);
x->x_outlet3 = outlet_new(&x->x_obj, &s_float);
x->x_outlet4 = outlet_new(&x->x_obj, &s_float);
x->x_outlet5 = outlet_new(&x->x_obj, &s_float);
x->x_outlet6 = outlet_new(&x->x_obj, &s_float);
return (x);
}
static void date_bang(t_date *x)
{
struct tm *resolvetime;
#ifdef USE_TIMEB
struct timeb mytime;
ftime(&mytime);
resolvetime=(x->GMT)?gmtime(&mytime.time):localtime(&mytime.time);
#else
struct timeval tv;
gettimeofday(&tv, 0);
resolvetime = (x->GMT)?gmtime(&tv.tv_sec):localtime(&tv.tv_sec);
#endif
outlet_float(x->x_outlet6, (t_float)resolvetime->tm_isdst);
outlet_float(x->x_outlet5, (t_float)resolvetime->tm_yday);
outlet_float(x->x_outlet4, (t_float)resolvetime->tm_wday);
outlet_float(x->x_outlet3, (t_float)resolvetime->tm_mday);
outlet_float(x->x_outlet2, (t_float)resolvetime->tm_mon + 1);
outlet_float(x->x_outlet1, (t_float)resolvetime->tm_year + 1900);
}
static void help_date(t_date *x)
{
post("\n%c date\t\t:: get the current system date", HEARTSYMBOL);
post("\noutputs are\t: year / month / day / day of week /day of year / daylightsaving (1/0)");
post("\ncreation\t::'date [GMT]': show local date or GMT");
}
void date_setup(void)
{
date_class = class_new(gensym("date"),
(t_newmethod)date_new, 0,
sizeof(t_date), 0, A_GIMME, 0);
class_addbang(date_class, date_bang);
class_addmethod(date_class, (t_method)help_date, gensym("help"), 0);
class_sethelpsymbol(date_class, gensym("zexy/date"));
}
/* general setup */
void z_datetime_setup(void)
{
time_setup();
date_setup();
}
|