gpt4 book ai didi

perl - 如何在 Perl 中格式化日期?

转载 作者:行者123 更新时间:2023-12-04 13:02:11 26 4
gpt4 key购买 nike

我正在修改 Xcode 中预先存在的脚本来自定义我的文件头。脚本是 Perl,它不是我最好的语言。 :)

我只需要在标题中以 dd/mm/yy 格式插入当前日期。

这是我的脚本:

#! /usr/bin/perl -w
# Insert HeaderDoc comment for a header
#
# Inserts a template HeaderDoc comment for the header.
use strict;

# get path to document
my $headerPath = <<'HEADERPATH';
%%%{PBXFilePath}%%%
HEADERPATH
chomp $headerPath;
my $rootFileName = &rootFileNameFromPath($headerPath);

print "/*";
print " * $rootFileName\n";
print " * Project\n";
print " *\n";
print " * Created by Me on ";
# in bash it would be something like that :
# date +%d/%m/%y | awk '{printf "%s\n", $1}';
print " * Copyright 2009 My_companie. All rights reserved.\n";
print " *\n";
print " */\n";

sub rootFileNameFromPath {
my $path = shift;

my @pathParts = split (m'/', $path);
my $filename = pop (@pathParts);
my $rootFileName = "$filename";
$rootFileName =~ s/\.h$//;
return $rootFileName;
}

exit 0;

我刚刚修改了打印命令,所以不要问我其余的代码:)

最佳答案

而不是删除 strict (!),为什么不直接制作代码 strict干净的?

my ($mday, $mon, $year) = (localtime(time))[3, 4, 5];

$mon += 1;
$year += 1900;

printf "%02d/%02d/%02d\n", $mday, $mon, $year % 100;

也许更好(因为对于用 Bash 提问的人来说更熟悉):
# At the top, under use strict;
use POSIX qw/strftime/;

# then later...
my $date = strftime "%d/%m/%y", localtime;
print "$date\n";

有趣的巧合: Perl Training Australia半定期出版 tips (您可以通过电子邮件或在线获取它们),而就在今天还有 a new onestrftime .

关于perl - 如何在 Perl 中格式化日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/575188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com