strptime.c Source File

Reference Documentation

Platform
Intel® PAC
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.10
Napatech Software Suite: examples/common/strptime.c Source File
strptime.c
Go to the documentation of this file.
1 // Copyright 2009 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <time.h>
16 #include <ctype.h>
17 #include <string.h>
18 
19 #ifdef WIN32
20 
21 #include "strptime.h"
22 
23 #undef NULL
24 #define NULL 0
25 
26 // Implement strptime under windows
27 static const char* kWeekFull[] = {
28  "Sunday", "Monday", "Tuesday", "Wednesday",
29  "Thursday", "Friday", "Saturday"
30 };
31 
32 static const char* kWeekAbbr[] = {
33  "Sun", "Mon", "Tue", "Wed",
34  "Thu", "Fri", "Sat"
35 };
36 
37 static const char* kMonthFull[] = {
38  "January", "February", "March", "April", "May", "June",
39  "July", "August", "September", "October", "November", "December"
40 };
41 
42 static const char* kMonthAbbr[] = {
43  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
44  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
45 };
46 
47 static const char* _parse_num(const char* s, int low, int high, int* value) {
48  const char* p = s;
49  for (*value = 0; *p != NULL && isdigit(*p); ++p) {
50  *value = (*value) * 10 + (int)(*p) - (int)('0');
51  }
52 
53  if (p == s || *value < low || *value > high) return NULL;
54  return p;
55 }
56 
57 static char* _strptime(const char *s, const char *format, struct tm *tm) {
58  int len = 0;
59  int i;
60  while (*format != NULL && *s != NULL) {
61  if (*format != '%') {
62  if (*s != *format) return NULL;
63 
64  ++format;
65  ++s;
66  continue;
67  }
68 
69  ++format;
70  switch (*format) {
71  // weekday name.
72  case 'a':
73  case 'A':
74  tm->tm_wday = -1;
75  for (i = 0; i < 7; ++i) {
76  len = (int)(strlen(kWeekAbbr[i]));
77  if (_strnicmp(kWeekAbbr[i], s, len) == 0) {
78  tm->tm_wday = i;
79  break;
80  }
81 
82  len = (int)(strlen(kWeekFull[i]));
83  if (_strnicmp(kWeekFull[i], s, len) == 0) {
84  tm->tm_wday = i;
85  break;
86  }
87  }
88  if (tm->tm_wday == -1) return NULL;
89  s += len;
90  break;
91 
92  // month name.
93  case 'b':
94  case 'B':
95  case 'h':
96  {
97  int i;
98  tm->tm_mon = -1;
99  for (i = 0; i < 12; ++i) {
100  len = (int)(strlen(kMonthAbbr[i]));
101  if (_strnicmp(kMonthAbbr[i], s, len) == 0) {
102  tm->tm_mon = i;
103  break;
104  }
105 
106  len = (int)(strlen(kMonthFull[i]));
107  if (_strnicmp(kMonthFull[i], s, len) == 0) {
108  tm->tm_mon = i;
109  break;
110  }
111  }
112  if (tm->tm_mon == -1) return NULL;
113  s += len;
114  }
115  break;
116 
117  // month [1, 12].
118  case 'm':
119  s = _parse_num(s, 1, 12, &tm->tm_mon);
120  if (s == NULL) return NULL;
121  --tm->tm_mon;
122  break;
123 
124  // day [1, 31].
125  case 'd':
126  case 'e':
127  s = _parse_num(s, 1, 31, &tm->tm_mday);
128  if (s == NULL) return NULL;
129  break;
130 
131  // hour [0, 23].
132  case 'H':
133  s = _parse_num(s, 0, 23, &tm->tm_hour);
134  if (s == NULL) return NULL;
135  break;
136 
137  // minute [0, 59]
138  case 'M':
139  s = _parse_num(s, 0, 59, &tm->tm_min);
140  if (s == NULL) return NULL;
141  break;
142 
143  // seconds [0, 60]. 60 is for leap year.
144  case 'S':
145  s = _parse_num(s, 0, 60, &tm->tm_sec);
146  if (s == NULL) return NULL;
147  break;
148 
149  // year [1900, 9999].
150  case 'Y':
151  s = _parse_num(s, 1900, 9999, &tm->tm_year);
152  if (s == NULL) return NULL;
153  tm->tm_year -= 1900;
154  break;
155 
156  // year [0, 99].
157  case 'y':
158  s = _parse_num(s, 0, 99, &tm->tm_year);
159  if (s == NULL) return NULL;
160  if (tm->tm_year <= 68) {
161  tm->tm_year += 100;
162  }
163  break;
164 
165  // arbitray whitespace.
166  case 't':
167  case 'n':
168  while (isspace(*s)) ++s;
169  break;
170 
171  // '%'.
172  case '%':
173  if (*s != '%') return NULL;
174  ++s;
175  break;
176 
177  // All the other format are not supported.
178  default:
179  return NULL;
180  }
181  ++format;
182  }
183 
184  if (*format != NULL) {
185  return NULL;
186  } else {
187  return (char*)(s);
188  }
189 }
190 
191 char* strptime(const char *buf, const char *fmt, struct tm *tm) {
192  return _strptime(buf, fmt, tm);
193 }
194 #endif // WIN32
195 
196 
197 
198