aboutsummaryrefslogtreecommitdiffstats
path: root/epan/ftypes/ftype-time.c
blob: 059a77d9cf7af5b84068fb4642770e2e2612c868 (plain)
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
/*
 * $Id: ftype-time.c,v 1.4 2001/05/31 05:01:06 guy Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 2001 Gerald Combs
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <time.h>

#include <ftypes-int.h>

static gboolean
cmp_eq(fvalue_t *a, fvalue_t *b)
{
	return ((a->value.time.tv_sec) ==(b->value.time.tv_sec))
	     &&((a->value.time.tv_usec)==(b->value.time.tv_usec));
}
static gboolean
cmp_ne(fvalue_t *a, fvalue_t *b)
{
	return (a->value.time.tv_sec !=b->value.time.tv_sec)
	     ||(a->value.time.tv_usec!=b->value.time.tv_usec);
}
static gboolean
cmp_gt(fvalue_t *a, fvalue_t *b)
{
	if (a->value.time.tv_sec > b->value.time.tv_sec) {
		return TRUE;
	}
	if (a->value.time.tv_sec < b->value.time.tv_sec) {
		return FALSE;
	}

	return a->value.time.tv_usec > b->value.time.tv_usec;
}
static gboolean
cmp_ge(fvalue_t *a, fvalue_t *b)
{
	if (a->value.time.tv_sec > b->value.time.tv_sec) {
		return TRUE;
	}
	if (a->value.time.tv_sec < b->value.time.tv_sec) {
		return FALSE;
	}

	return a->value.time.tv_usec >= b->value.time.tv_usec;
}
static gboolean
cmp_lt(fvalue_t *a, fvalue_t *b)
{
	if (a->value.time.tv_sec < b->value.time.tv_sec) {
		return TRUE;
	}
	if (a->value.time.tv_sec > b->value.time.tv_sec) {
		return FALSE;
	}

	return a->value.time.tv_usec < b->value.time.tv_usec;
}
static gboolean
cmp_le(fvalue_t *a, fvalue_t *b)
{
	if (a->value.time.tv_sec < b->value.time.tv_sec) {
		return TRUE;
	}
	if (a->value.time.tv_sec > b->value.time.tv_sec) {
		return FALSE;
	}

	return a->value.time.tv_usec <= b->value.time.tv_usec;
}


static gboolean
relative_val_from_string(fvalue_t *fv, char *s, LogFunc log)
{

	if (sscanf(s,"%lu.%lu",&fv->value.time.tv_sec,&fv->value.time.tv_usec)!=2) {
		log("\"%s\" is not a valid relative time. Use \"<seconds>.<useconds>\"",s);
		return FALSE;
	}

	return TRUE;
}


static gboolean
absolute_val_from_string(fvalue_t *fv, char *s, LogFunc log)
{
	struct tm tm;
	char *str;
	str=strptime(s,"%b %d, %Y %H:%M:%S.",&tm);
	if (!str) {
		log("\"%s\" is not a valid absolute time. Example: \"Nov 12, 1999 08:55:44.123\"",s);
		return FALSE;
	}
	fv->value.time.tv_sec = mktime(&tm);
	sscanf(str,"%lu",&fv->value.time.tv_usec);
	return TRUE;
}

static void
time_fvalue_new(fvalue_t *fv)
{
	fv->value.time.tv_sec = 0;
	fv->value.time.tv_usec = 0;
}

static void
time_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
{
	g_assert(!already_copied);
	memcpy(&(fv->value.time), value, sizeof(struct timeval));
}

static gpointer
value_get(fvalue_t *fv)
{
	return &(fv->value.time);
}

void
ftype_register_time(void)
{

	static ftype_t abstime_type = {
		"FT_ABSOLUTE_TIME",
		"date/time",
		0,
		time_fvalue_new,
		NULL,
		absolute_val_from_string,

		time_fvalue_set,
		NULL,
		NULL,

		value_get,
		NULL,
		NULL,

		cmp_eq,
		cmp_ne,
		cmp_gt,
		cmp_ge,
		cmp_lt,
		cmp_le,
	};
	static ftype_t reltime_type = {
		"FT_RELATIVE_TIME",
		"time offset",
		0,
		time_fvalue_new,
		NULL,
		relative_val_from_string,

		time_fvalue_set,
		NULL,
		NULL,

		value_get,
		NULL,
		NULL,

		cmp_eq,
		cmp_ne,
		cmp_gt,
		cmp_ge,
		cmp_lt,
		cmp_le,
	};

	ftype_register(FT_ABSOLUTE_TIME, &abstime_type);
	ftype_register(FT_RELATIVE_TIME, &reltime_type);
}