本帖最后由 Echo11 于 2022-2-23 13:08 编辑
(由于没有权限单独发帖。在此回复了。)
[日期时间]批处理生成邮件头里的日期格式 (更新时间:Wed, 23 Feb 2022 12:29:34 +0800)
如:Date: Wed, 23 Feb 2022 12:29:34 +0800
在用批处理发送邮件前需要生成“mail”本身,而mail包含mail header 和 mail content。mail header 遵从RFC 5322标准。
Win系统中无法直接获取这种格式的日期时间,因此我写了这个批处理来干这件事。
一、RFC 5322中关于日期的格式要求如下(非标准原文):
## Mail Date Reference
date = day month year ; 应当表示本地时间
time = time-of-day zone
----------------------------------------------------------------------------------------------
day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
day = ([FWS] 1*2DIGIT FWS) / obs-day ; 一个月的第几天
month = "Jan" / "Feb" / "Mar" / "Apr" /
"May" / "Jun" / "Jul" / "Aug" /
"Sep" / "Oct" / "Nov" / "Dec"
year = (FWS 4*DIGIT FWS) / obs-year ; 四位数字
----------------------------------------------------------------------------------------------
time-of-day = hour ":" minute [ ":" second ] ; 应当表示本地时间, 一天中的时:分[:秒],
; 范围00:00:00 - 23:59:60
hour = 2DIGIT / obs-hour
minute = 2DIGIT / obs-minute
second = 2DIGIT / obs-second
zone = (FWS ( "+" / "-" ) 4DIGIT) / obs-zone
; date与time-of-day偏离UTC或GMT的偏差\
; +表示ahead of(即east of)UTC, -表示behind(即west of) UTC
; 前两个数字表示hours偏差, 后两个数字表示minutes偏差
; +hhmm表示 +(hh * 60 + mm) 分钟, -hhmm表示 -(hh * 60 + mm) 分钟
; "+0000"用于表示UTC的时区
; "-0000"用于表示本地时区生成的时间, date-time不包含本地时区的信息
sample:
Date: Wed, 23 Feb 2022 12:29:34 +0800
二、我的批处理- :MailDate
- REM 获取以下邮件头中的日期时间
- REM Date: Wed, 23 Feb 2022 12:29:34 +0800
- @echo off
- for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
- set "year=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "day=%dt:~6,2%" & set "time-of-day=%dt:~8,2%:%dt:~10,2%:%dt:~12,2%"
-
- set "dn1=Mon" & set "dn2=Tue" & set "dn3=Wed" & set "dn4=Thu" & set "dn5=Fri" & set "dn6=Sat" & set "dn7=Sun"
- set "mon01=Jan" & set "mon02=Feb" & set "mon03=Mar" & set "mon04=Apr" & set "mon05=May" & set "mon06=Jun" & set "mon07=Jul" & set "mon08=Aug" & set "mon09=Sep" & set "mon10=Oct" & set "mon11=Nov" & set "mon12=Dec"
-
- setlocal enabledelayedexpansion
- for /f "tokens=2 delims==" %%b in ('wmic path win32_localtime Get DayOfWeek /value') do set "dow=%%b"
- set "day-name=!dn%dow%!" & set month=!mon%MM%!
- for /f "tokens=2 delims=^(^)" %%t in ('wmic TimeZone Get Caption /value') do set "zone=%%t"
- set "zone=!zone:U=!" & set "zone=!zone:T=!" & set "zone=!zone:C=!" & set "zone=!zone::=!"
-
- echo.Mail Date:
- echo.Date: %day-name%, %day% %month% %year% %time-of-day% %zone%
-
- endlocal
- pause>nul
- EXIT /B 0
复制代码 三、执行结果如下:- Mail Date:
- Date: Wed, 23 Feb 2022 12:43:00 +0800
复制代码
|