Browse Source

Change utils on struct tm

Enhancement in comparison behavior + print function
Yann Weber 7 years ago
parent
commit
9b99e070a9
2 changed files with 58 additions and 12 deletions
  1. 8
    0
      src/include/ttail_search.h
  2. 50
    12
      src/ttail_search.c

+ 8
- 0
src/include/ttail_search.h View File

@@ -95,4 +95,12 @@ const char* ttail_logline_subst(ttail_t*, const char*);
95 95
  */
96 96
 int ttail_tm_cmp(const struct tm*, const struct tm*);
97 97
 
98
+/**<! Set all fields to -1 */
99
+void ttail_tm_init(struct tm* tm);
100
+
101
+/**@brief Print a struct tm on stdout
102
+ *@param struct tm *
103
+ */
104
+void ttail_tm_print(const struct tm*);
105
+
98 106
 #endif

+ 50
- 12
src/ttail_search.c View File

@@ -30,7 +30,7 @@ int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
30 30
 {
31 31
 	const char *subst, *ret;
32 32
 
33
-	memset(tm, 0,sizeof(struct tm));
33
+	ttail_tm_init(tm);
34 34
 
35 35
 	subst = ttail_logline_subst(ttail, logline);
36 36
 	if(!subst)
@@ -75,17 +75,55 @@ const char* ttail_logline_subst(ttail_t* t, const char* logline)
75 75
 int ttail_tm_cmp(const struct tm *ta, const struct tm *tb)
76 76
 {
77 77
 	int r;
78
-	r = ta->tm_year - tb->tm_year;
79
-	if(r) { return r; }
80
-	r = ta->tm_mon - tb->tm_mon;
81
-	if(r) { return r; }
82
-	r = ta->tm_mday - tb->tm_mday;
83
-	if(r) { return r; }
84
-	r = ta->tm_hour - tb->tm_hour;
85
-	if(r) { return r; }
86
-	r = ta->tm_min - tb->tm_min;
87
-	if(r) { return r; }
88
-	r = ta->tm_sec - tb->tm_sec;
78
+	r=0;
79
+	if(ta->tm_year >= 0 && ta->tm_year >= 0)
80
+	{
81
+		r = ta->tm_year - tb->tm_year;
82
+		if(r) { return r; }
83
+	}
84
+	if(ta->tm_mon >= 0 && ta->tm_mon >= 0)
85
+	{
86
+		r = ta->tm_mon - tb->tm_mon;
87
+		if(r) { return r; }
88
+	}
89
+	if(ta->tm_mday >= 0 && ta->tm_mday >= 0)
90
+	{
91
+		r = ta->tm_mday - tb->tm_mday;
92
+		if(r) { return r; }
93
+	}
94
+	if(ta->tm_hour >= 0 && ta->tm_hour >= 0)
95
+	{
96
+		r = ta->tm_hour - tb->tm_hour;
97
+		if(r) { return r; }
98
+	}
99
+	if(ta->tm_min >= 0 && ta->tm_min >= 0)
100
+	{
101
+		r = ta->tm_min - tb->tm_min;
102
+		if(r) { return r; }
103
+	}
104
+	if(ta->tm_sec >= 0 && ta->tm_sec >= 0)
105
+	{
106
+		r = ta->tm_sec - tb->tm_sec;
107
+	}
89 108
 	return r;
90 109
 }
91 110
 
111
+void ttail_tm_init(struct tm* tm)
112
+{
113
+	size_t i;
114
+	int *ptr;
115
+	ptr = (int*)tm;
116
+	for(i=0; i<sizeof(struct tm)/sizeof(int); i++)
117
+	{
118
+		*ptr = -1;
119
+		ptr++;
120
+	}
121
+}
122
+
123
+void ttail_tm_print(const struct tm* tm)
124
+{
125
+	printf("year:%d month:%d mday:%d hour:%d min:%d sec:%d",
126
+		tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
127
+		tm->tm_sec);
128
+}
129
+

Loading…
Cancel
Save