浏览代码

httpd: create www.zip with UTC timestamps

Create www.zip with UTC timestamps; converting it from some unknown
local time on the fly is both inefficient and incorrect.
H. Peter Anvin 3 年之前
父节点
当前提交
f7fbccdec9
共有 5 个文件被更改,包括 37 次插入11 次删除
  1. 7 3
      esp32/Makefile
  2. 30 8
      esp32/max80/httpd.c
  3. 二进制
      esp32/output/max80.ino.bin
  4. 二进制
      fpga/output/v1.fw
  5. 二进制
      fpga/output/v2.fw

+ 7 - 3
esp32/Makefile

@@ -23,12 +23,16 @@ $(TARGET): $(shell find $(SKETCH) -type f) $(GENFILES)
 zip:
 	mkdir -p zip
 	cd www && \
-	$(ZIP) -9DrpX -FS ../zip/www.zip . \
-		-x '.*' -x '#*' -x '*~' -x '*.bak'
+		TZ=GMT0 \
+		$(ZIP) -9DrpX -FS ../zip/www.zip . \
+			-x '.*' -x '#*' -x '*~' -x '*.bak'
 
 # Ugly hack but needed to avoid unnecessary rebuilds
 www.zip: zip
-	if [ ! -f $@ ] || [ zip/$@ -nt $@ ]; then cp -f zip/$@ $@; fi
+	if [ ! -f $@ ] || [ zip/$@ -nt $@ ]; then \
+		cp -f zip/$@ $@; \
+		rm -f build/sketch/www.c build/sketch/www.c.*; \
+	fi
 
 upload: $(TARGET)
 	$(ARDUINO_CLI) upload -i $(TARGET) -p $(PORT) -b $(BOARD) $(SKETCH)

+ 30 - 8
esp32/max80/httpd.c

@@ -16,25 +16,47 @@
 static httpd_handle_t httpd;
 
 #define TIMEBUF_LEN 32
-static const char *http_date(time_t when, char *timebuf)
+static const char *http_date(const struct tm *when)
 {
+    static char timebuf[TIMEBUF_LEN];
+
     strftime(timebuf, TIMEBUF_LEN,
-	     "%a, %d %b %Y %H:%M:%S GMT", gmtime(&when));
+	     "%a, %d %b %Y %H:%M:%S GMT", when);
 
     return timebuf;
 }
 
 static const char *http_now(void)
 {
-    static char timebuf[TIMEBUF_LEN];
-    return http_date(time(NULL), timebuf);
+    time_t t = time(NULL);
+    return http_date(gmtime(&t));
+}
+
+static struct tm *set_weekday(struct tm *tm)
+{
+    /*
+     * A variation on Zeller's congruence.
+     */
+    unsigned int c, y, m, d;
+
+    y = tm->tm_year + 1900;
+    m = tm->tm_mon + 2;
+    d = tm->tm_mday;
+
+    if (m < 4) {
+	m += 12;
+	y--;
+    }
+
+    c = y/100;
+
+    tm->tm_wday = (d + ((13*m)/5) + y + (y >> 2) - c + (c >> 2) + 6) % 7;
+    return tm;
 }
 
 static const char *http_dos_date(uint32_t dos_date)
 {
-    static char timebuf[TIMEBUF_LEN];
     struct tm tm;
-    time_t t;
 
     tm.tm_sec   = (dos_date << 1) & 63;
     tm.tm_min   = (dos_date >> 5) & 63;
@@ -42,9 +64,9 @@ static const char *http_dos_date(uint32_t dos_date)
     tm.tm_mday  = (dos_date >> 16) & 31;
     tm.tm_mon   = ((dos_date >> 21) & 15) - 1;
     tm.tm_year  = (dos_date >> 25) + 80;
-    tm.tm_isdst = -1;
+    tm.tm_isdst = 0;		/* Times are stored in GMT */
 
-    return http_date(mktime(&tm), timebuf);
+    return http_date(set_weekday(&tm));
 }
 
 static esp_err_t httpd_error(httpd_req_t *req,

二进制
esp32/output/max80.ino.bin


二进制
fpga/output/v1.fw


二进制
fpga/output/v2.fw