首页
社区
课程
招聘
[原创]自己造轮子,http时间格式的实现。
发表于: 2013-9-23 15:03 4732

[原创]自己造轮子,http时间格式的实现。

2013-9-23 15:03
4732
HTTP存在三种时间格式,下面来看下官方的文档。
3.3 Date/Time Formats
3.3.1 Full Date
HTTP applications have historically allowed three different formats for the representation of date/time stamps:
Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
Sun Nov 6 08:49:37 1994
; ANSI C's asctime() format
The first format is preferred as an Internet standard and represents a fixed-length subset of that defined by RFC 1123
[8] (an update to RFC 822 [9]). The second format is in common use, but is based on the obsolete RFC 850 [12] date
format and lacks a four-digit year. HTTP/1.1 clients and servers that parse the date value MUST accept all three
formats (for compatibility with HTTP/1.0), though they MUST only generate the RFC 1123 format for representing
HTTP-date values in header fields. See section 19.3 for further information.
Note: Recipients of date values are encouraged to be robust in accepting date values that may have been
sent by non-HTTP applications, as is sometimes the case when retrieving or posting messages via
proxies/gateways to SMTP or NNTP.
All HTTP date/time stamps MUST be represented in Greenwich Mean Time (GMT), without exception. For the
purposes of HTTP, GMT is exactly equal to UTC (Coordinated Universal Time). This is indicated in the first two
formats by the inclusion of “GMT” as the three-letter abbreviation for time zone, and MUST be assumed when
reading the asctime format. HTTP-date is case sensitive and MUST NOT include additional LWS beyond that
specifically included as SP in the grammar.
HTTP-date =
rfc1123-date =
rfc850-date =
asctime-date =
date1 =
date2 =
date3 =
time =
wkday =
     |
    =
   |
  =
 |
weekday
month
Fielding, et al
rfc1123-date | rfc850-date | asctime-date
wkday "," SP date1 SP time SP "GMT"
weekday "," SP date2 SP time SP "GMT"
wkday SP date3 SP time SP 4DIGIT
2DIGIT SP month SP 4DIGIT
; day month year (e.g., 02 Jun 1982)
2DIGIT "-" month "-" 2DIGIT
; day-month-year (e.g., 02-Jun-82)
month SP ( 2DIGIT | ( SP 1DIGIT ))
; month day (e.g., Jun 2)
2DIGIT ":" 2DIGIT ":" 2DIGIT
; 00:00:00 - 23:59:59
"Mon" | "Tue" | "Wed"
"Thu" | "Fri" | "Sat" | "Sun"
"Monday" | "Tuesday" | "Wednesday"
"Thursday" | "Friday" | "Saturday" | "Sunday"
"Jan" | "Feb" | "Mar" | "Apr"
"May" | "Jun" | "Jul" | "Aug"
Standards Track
[Page 15]
RFC 2616
HTTP/1.1
June, 1999
| "Sep" | "Oct" | "Nov" | "Dec"
Note: HTTP requirements for the date/time stamp format apply only to their usage within the protocol
stream. Clients and servers are not required to use these formats for user presentation, request logging, etc.

根据上面的数据,可以写出如下代码来实现。
namespace vws {
bool vws_http_time::fmt(vws_http_time_const::TYPE type,
		char* pszBuffer, size_t nSize) {
	static char pszDateFmt[][48] = {
			{ "%3s, %02d %3s %04d %02d:%02d:%02d GMT" }, {
					"%s, %02d-%3s-%02d %02d:%02d:%02d GMT" }, {
					"%3s %3s %02d %02d:%02d:%02d %04d" } };
	static char* pszBuf = new char[PATH_MAX];
	if (pszBuf == NULL) {
		return false;
	} else {
		memset(pszBuf, 0, PATH_MAX);
	}
	time_t tt = time(NULL);
	struct tm* t = localtime(&tt);
	switch (type) {
	case vws_http_time_const::RFC1123_DATE:
		sprintf(pszBuf, pszDateFmt[0],
				vws_http_time::getWkdayString(t->tm_wday), t->tm_mday,
				vws_http_time::getMonthString(t->tm_mon), (t->tm_year + 1900),
				t->tm_hour, t->tm_min, t->tm_sec);
		break;
	case vws_http_time_const::RFC850_DATE:
		sprintf(pszBuf, pszDateFmt[1], vws_http_time::getWeekdayString(
				t->tm_wday), t->tm_mday, vws_http_time::getMonthString(
				t->tm_mon), (t->tm_year + 1900), t->tm_hour, t->tm_min,
				t->tm_sec);
		break;
	case vws_http_time_const::ASCTIME_DATE:
		sprintf(pszBuf, pszDateFmt[2],
				vws_http_time::getWkdayString(t->tm_wday),
				vws_http_time::getMonthString(t->tm_mon), t->tm_mday,
				t->tm_hour, t->tm_min, t->tm_sec, (t->tm_year + 1900));
		break;
	default:
		return false;
	}
	size_t nLength = strlen(pszBuf);
	if (nSize > nLength) {
		memset(pszBuffer, 0, nSize);
		strncpy(pszBuffer, pszBuf, nLength);
		delete[] pszBuf;
		return false;
	} else {
		return false;
	}
}

char* vws_http_time::getWkdayString(int nDay) {

	enum WKDAY_E {
		Sun = 0, Mon, Tue, Wed, Thu, Fri, Sat
	};
	static char pszWkday[8][4] = { { "Sun" }, { "Mon" }, { "Tue" }, { "Wed" },
			{ "Thu" }, { "Fri" }, { "Sat" }, { 0 } };
	if (nDay < Mon && nDay > Sat) {
		return NULL;
	}
	return pszWkday[nDay];
}

char* vws_http_time::getWeekdayString(int nWeekday) {
	enum WEEKDAY_E {
		Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
	};
	static char pszWeekday[8][10] = { { "Sunday" }, { "Monday" },
			{ "Tuesday" }, { "Wednesday" }, { "Thursday" }, { "Friday" }, {
					"Saturday" }, { 0 } };
	if (nWeekday < Monday && nWeekday > Saturday) {
		return NULL;
	}
	return pszWeekday[nWeekday];
}

char* vws_http_time::getMonthString(int nMonth) {
	enum MONTH_E {
		Jan = 0, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
	};
	static char pszMonth[][4] = { { "Jan" }, { "Feb" }, { "Mar" }, { "Apr" }, {
			"May" }, { "Jun" }, { "Jul" }, { "Aug" }, { "Sep" }, { "Oct" }, {
			"Nov" }, { "Dec" }, { 0 } };
	if (nMonth < Jan && nMonth > Dec) {
		return NULL;
	}
	return pszMonth[nMonth];
}
}

节目完毕,谢谢观看。

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 1453
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
没人支持啊  我来顶你一下
2013-9-23 17:51
0
雪    币: 231
活跃值: (2631)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
3
mark下……
2013-9-23 17:56
0
雪    币: 4
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
我也来支持一下
2013-9-23 21:16
0
游客
登录 | 注册 方可回帖
返回
//