F. Grela
F. Grela

Reputation: 61

Use different language than locale for strftime by perl script

My OS is in polish language. I need print month name in english by perl script.

Locale:

LANG=pl_PL.UTF8
LANGUAGE=
LC_CTYPE=en_US.UTF-8
LC_NUMERIC=pl_PL.UTF-8
LC_TIME="pl_PL.UTF8"
LC_COLLATE="pl_PL.UTF8"
LC_MONETARY=pl_PL.UTF-8
LC_MESSAGES="pl_PL.UTF8"
LC_PAPER=pl_PL.UTF-8
LC_NAME=pl_PL.UTF-8
LC_ADDRESS=pl_PL.UTF-8
LC_TELEPHONE=pl_PL.UTF-8
LC_MEASUREMENT=pl_PL.UTF-8
LC_IDENTIFICATION=pl_PL.UTF-8
LC_ALL=

Script:

#!/usr/bin/perl -w
use strict;
use POSIX;

print strftime "%b %d, %Y", gmtime(time());

Output: kwi 11, 2018 Expected: Mar 11, 2018

Is there any smarter solution than do s/kwi/Mar/?

Upvotes: 2

Views: 1223

Answers (1)

Georg Mavridis
Georg Mavridis

Reputation: 2341

POSIX::setlocale is what you need

#!/usr/bin/perl -w
use strict;
use POSIX qw(setlocale strftime);
print strftime "%a %b %d, %Y", gmtime(time());
setlocale(POSIX::LC_ALL,'en_US.utf8');
print strftime "%a %b %d, %Y", gmtime(time());
setlocale(&POSIX::LC_ALL,'de_DE.utf8');
print strftime "%a %b %d, %Y", gmtime(time());

perldoc POSIX should give additional info. Also see this about setting and resetting the locale.

Upvotes: 4

Related Questions