惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
罗磊的独立博客
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
量子位
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
B
Blog RSS Feed
腾讯CDC
博客园_首页
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
博客园 - Franky
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
D
Docker
P
Privacy & Cybersecurity Law Blog
S
Securelist
V
V2EX
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
T
Tor Project blog
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog
AWS News Blog
AWS News Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Recent Announcements
Recent Announcements
Cloudbric
Cloudbric
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术

John Bokma's Hacking and Hiking

Failed to verify signature archive-contents.sig in Emacs Aquamacs 3.6 Hangs When Saving An Encrypted File PAR trouble How To Create a Bootable LibreELEC Installation using Mac OS Running pdflatex Using the Alpine Pandoc LaTeX Docker Image A Docker Image for Sass Timezones in Alpine Docker Containers A Tale of Three Docker Images Debugging a Perl Docker container Perl Time::Piece Unicode Issue Giving Docker Desktop for macOS a Second Chance Flashing another TP-Link TL-WDR4300 with OpenWrt firmware Rehousing two tarantulas Getting started with the Perl version of tumblelog on Ubuntu 18.04 LTS A visit to Avonturia De Vogelkelder Flashing a TP-Link TL-WDR4300 with OpenWrt firmware Mounting a VDI File in a Different VirtualBox Guest Wireless Headless Raspberry Pi - John Bokma A Matter of Time - John Bokma Hand coding an RSS 2.0 feed in Python RFC #822 and RFC #3339 dates in Perl Hand coding an RSS 2.0 feed in Perl Nav Element with no Heading Rewriting CommonMark Nodes in Perl "right" this time
RFC #822 and RFC #3339 dates in Python
John Bokma · 2019-10-10 · via John Bokma's Hacking and Hiking

October 9, 2019

When adding a JSON feed and an RSS feed to tumblelog I needed a publication date and time for each article published. As I don't have an actual publication time I decided to use the end of day, as explained in A Matter of Time.

Edit: this blog post has been updated with additional code to make the RFC #822 solution work for a non-USA locale.

The end of day

In order to obtain a datetime Python object for the local end of day for a given date I came up with the following idea:

  • parse the date with a time of 23:59:59 attached to it
  • create a new object using this result but in the local time zone using the astimezone constructor

I accomplished this in Python as follows:

def get_end_of_day(date):
    return datetime.strptime(
        f'{date} 23:59:59', '%Y-%m-%d %H:%M:%S').astimezone()

Calling this function with the date of today returns:

>>> get_end_of_day('2019-10-09')
datetime.datetime(2019, 10, 9, 23, 59, 59, tzinfo=datetime.timezone(datetime.tim
edelta(seconds=7200), 'CEST'))

Note the additional time zone data (tzinfo); Central European Summer Time, which is correct for my location.

RFC #822

For the RSS feed I needed the pubDate to be in RFC #822 format, which I obtained as follows:

ctime = end_of_day.ctime()
pub_date = (f'{ctime[0:3]}, {end_of_day.day:02d} {ctime[4:7]}'
                + end_of_day.strftime(' %Y %H:%M:%S %z'))

Note that the short month name and the short day name are obtained from ctime instead of returned by strftime. The reason for this is that ctime returns those names in the USA locale, a requirement for RFC #822.

Moreover, it's tempting to use %Z instead of %z but note that, for example, CEST is not valid according to the RSS feed validator, which refers to section 5.1 of RFC #822. Hence, why digits must be used instead for a general solution.

An example of a pub_date value is:

Wed, 09 Oct 2019 23:59:59 +0200

RFC #3339

For the JSON feed I needed a date_published in RFC #3339 format, which was even easier to obtain as the string version of a datetime is correct except for a space instead of a T:

date_published = str(end_of_day).replace(' ', 'T')

An example of a date_published value is:

2019-10-09T23:59:59+02:00

See Also