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

推荐订阅源

SecWiki News
SecWiki News
量子位
The Cloudflare Blog
美团技术团队
T
The Exploit Database - CXSecurity.com
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
博客园 - 司徒正美
宝玉的分享
宝玉的分享
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Secure Thoughts
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
Jina AI
Jina AI
博客园 - 聂微东
A
Arctic Wolf
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
T
Tailwind CSS Blog
The Hacker News
The Hacker News
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
TaoSecurity Blog
TaoSecurity Blog
Project Zero
Project Zero
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cloudbric
Cloudbric
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Troy Hunt's Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V2EX - 技术
V2EX - 技术
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog

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 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 RFC #822 and RFC #3339 dates in Python Hand coding an RSS 2.0 feed in Perl Nav Element with no Heading Rewriting CommonMark Nodes in Perl "right" this time
Perl Time::Piece Unicode Issue
John Bokma · 2021-06-04 · via John Bokma's Hacking and Hiking

June 3, 2021

The 25th of November 2019 while working on tumblelog I ran into an issue with the Perl module Time::Piece when generating month names. The result I expected was a Unicode month name. What I got was Mojibake.

To demonstrate this issue consider the following script, which I named tpbug.pl:

#!/usr/bin/perl

use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

use Time::Piece;

for my $month ( 1..12 ) {
    my $date = sprintf '2021-%02d-01', $month;
    my $tp = Time::Piece->strptime( $date, '%Y-%m-%d' );
    print $tp->strftime( '%B' ), ' ';
}
print "\n";

This will generate a list of the 12 month names. If we change the language using the LANG environment variable, for example to Russian, the bug shows its ugly head:

$ LANG=ru_RU.UTF-8 perl tpbug.pl
Time::Piece bug 97539 Mojibake
Time::Piece bug 97539 Mojibake.

This bug was reported as Bug #97539 back in 2014 (not by me).

To fix this issue in tumblelog I wrote a small helper function:

sub decode_utf8 {
    # UTF8 encoding for the Time::Piece strftime method, see bug #97539
    # https://rt.cpan.org/Public/Bug/Display.html?id=97539
    return decode( 'UTF-8', shift, Encode::FB_CROAK )
}

The helper function converts the octets generated by Time::Piece, assuming its UTF-8 data (and croaking if not), into a string in Perl's internal format. Since the open pragma was used to set stdout to UTF-8 this should give the desired result: month names printed on the terminal in UTF-8.

The updated and working Perl script becomes:

#!/usr/bin/perl

use strict;
use warnings;
use open ':std', ':encoding(UTF-8)';

use Encode 'decode';
use Time::Piece;

for my $month ( 1..12 ) {
    my $date = sprintf '2021-%02d-01', $month;
    my $tp = Time::Piece->strptime( $date, '%Y-%m-%d' );
    print decode_utf8( $tp->strftime( '%B' ) ), ' ';
}
print "\n";

sub decode_utf8 {
    # UTF8 encoding for the Time::Piece strftime method, see bug #97539
    # https://rt.cpan.org/Public/Bug/Display.html?id=97539
    return decode( 'UTF-8', shift, Encode::FB_CROAK )
}

Running this script, which I named tpok.pl results in the expected output:

$ LANG=ru_RU.UTF-8 perl tpok.pl 
января февраля марта апреля мая июня июля августа сентября октября ноября декабр
я 

Edit: Sinan Unur (nanis) recommends the Perl module Unicode::UTF8 over Encode with the hand-rolled helper in a discussion on Hacker News.

Related