瀏覽代碼

gmtime_fmt

hewei 6 年之前
父節點
當前提交
8caf37bfda
共有 5 個文件被更改,包括 34 次插入10 次删除
  1. 2 1
      Makefile
  2. 17 0
      base/htime.c
  3. 8 0
      base/htime.h
  4. 2 6
      http/HttpMessage.cpp
  5. 5 3
      http/server/FileCache.cpp

+ 2 - 1
Makefile

@@ -15,10 +15,11 @@ prepare:
 	$(RM) base/RAII.o
 
 libhv: prepare
+	$(RM) include
 	$(MAKEF) TARGET=$@ TARGET_TYPE=SHARED SRCDIRS=". base utils event http http/client http/server protocol"
 	$(MAKEF) TARGET=$@ TARGET_TYPE=STATIC SRCDIRS=". base utils event http http/client http/server protocol"
 	$(MKDIR) include
-	$(CP) $(INSTALL_HEADERS) include/
+	$(CP) $(INSTALL_HEADERS) include
 
 install:
 	$(MKDIR) -p $(INSTALL_INCDIR)

+ 17 - 0
base/htime.c

@@ -85,6 +85,23 @@ time_t datetime_mktime(datetime_t* dt) {
     return mktime(&tm);
 }
 
+char* datetime_fmt(datetime_t* dt, char* buf) {
+    sprintf(buf, DATETIME_FMT,
+        dt->year, dt->month, dt->day,
+        dt->hour, dt->min, dt->sec, dt->ms);
+    return buf;
+}
+
+char* gmtime_fmt(time_t time, char* buf) {
+    struct tm* tm = gmtime(&time);
+    //strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", tm);
+    sprintf(buf, GMTIME_FMT,
+        s_weekdays[tm->tm_wday],
+        tm->tm_mday, s_months[tm->tm_mon], tm->tm_year + 1900,
+        tm->tm_hour, tm->tm_min, tm->tm_sec);
+    return buf;
+}
+
 int days_of_month(int month, int year) {
     if (month < 1 || month > 12) {
         return 0;

+ 8 - 0
base/htime.h

@@ -105,6 +105,14 @@ static inline unsigned long long timestamp_ms() {
 datetime_t datetime_now();
 time_t     datetime_mktime(datetime_t* dt);
 
+#define DATETIME_FMT        "%04d-%02d-%02d %02d:%02d:%02d.%03d"
+#define DATETIME_FMT_BUFLEN 24
+char* datetime_fmt(datetime_t* dt, char* buf);
+
+#define GMTIME_FMT          "%.3s, %02d %.3s %04d %02d:%02d:%02d GMT"
+#define GMTIME_FMT_BUFLEN   30
+char* gmtime_fmt(time_t time, char* buf);
+
 datetime_t* datetime_past(datetime_t* dt, int days DEFAULT(1));
 datetime_t* datetime_future(datetime_t* dt, int days DEFAULT(1));
 

+ 2 - 6
http/HttpMessage.cpp

@@ -2,6 +2,7 @@
 
 #include <string.h>
 
+#include "htime.h"
 #include "http_parser.h" // for http_parser_url
 
 void HttpMessage::FillContentType() {
@@ -261,7 +262,6 @@ std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
     return str;
 }
 
-#include <time.h>
 std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
     char c_str[256] = {0};
     std::string str;
@@ -269,11 +269,7 @@ std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
     snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
     str += c_str;
     if (is_dump_headers) {
-        // Date:
-        time_t tt;
-        time(&tt);
-        strftime(c_str, sizeof(c_str), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
-        headers["Date"] = c_str;
+        headers["Date"] = gmtime_fmt(time(NULL), c_str);
         DumpHeaders(str);
     }
     str += "\r\n";

+ 5 - 3
http/server/FileCache.cpp

@@ -1,10 +1,13 @@
 #include "FileCache.h"
 
 #include "hscope.h"
+#include "htime.h"
 
 #include "httpdef.h" // for http_content_type_str_by_suffix
 #include "http_page.h" //make_index_of_page
 
+#define ETAG_FMT    "\"%zx-%zx\""
+
 file_cache_t* FileCache::Open(const char* filepath, void* ctx) {
     std::lock_guard<std::mutex> locker(mutex_);
     file_cache_t* fc = Get(filepath);
@@ -64,9 +67,8 @@ file_cache_t* FileCache::Open(const char* filepath, void* ctx) {
             memcpy(fc->filebuf.base, page.c_str(), page.size());
             fc->content_type = http_content_type_str(TEXT_HTML);
         }
-        time_t tt = fc->st.st_mtime;
-        strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
-        snprintf(fc->etag, sizeof(fc->etag), "\"%zx-%zx\"", fc->st.st_mtime, fc->st.st_size);
+        gmtime_fmt(fc->st.st_mtime, fc->last_modified);
+        snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, fc->st.st_mtime, fc->st.st_size);
     }
     return fc;
 }