如何在Perl格式化日期? [英] How do I format dates in Perl?

查看:158
本文介绍了如何在Perl格式化日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这是我的脚本:

 #! / usr / bin / perl -w 
#为标题插入HeaderDoc注释

#为标题插入一个模板HeaderDoc注释。
使用strict;

#获取文档路径
my $ headerPath =<&';'HEADERPATH';
%%% {PBXFilePath} %%%
HEADERPATH
chomp $ headerPath;
我的$ rootFileName =& rootFileNameFromPath($ headerPath);

print/ *;
print* $ rootFileName\\\
;
print* Project\\\
;
print* \\\
;
print*由我创建;
#在bash中将会是这样的:
#date +%d /%m /%y | awk'{printf%s\\\
,$ 1}';
print*版权所有2009 My_companie。保留所有权利。
print* \\\
;
print* / \\\
;

sub rootFileNameFromPath {
my $ path = shift;

我的@pathParts = split(m'/',$ path);
我的$ filename = pop(@pathParts);
我的$ rootFileName =$ filename;
$ rootFileName =〜s / \.h $ //;
返回$ rootFileName;
}

exit 0;

我刚刚修改了print命令,所以不要求我剩下的代码: )

解决方案

而不是删除 strict (!),使代码 strict clean?

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

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

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

也许更好(因为更熟悉的人看着Bash的问题):

 #在顶部,使用严格; 
使用POSIX qw / strftime /;

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

有趣的巧合: Perl Training Australia 发布半定期提示(您可以通过电子邮件或在线),而在今天,一个新的 c $ c> strftime 。


I'm modifying a pre-existing script in Xcode to customize my file headers. The script is Perl and it's not my best langage. :)

I just need to insert the current date in the header in dd/mm/yy format.

Here is my script :

#! /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;

I've just modified the print command so don't ask me for the rest of the code :)

解决方案

Rather than removing strict (!), why not just make the code strict clean?

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

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

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

Maybe even better (since more familiar looking to someone who asked in terms of Bash):

# At the top, under use strict;
use POSIX qw/strftime/;

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

Funny coincidence: Perl Training Australia publishes semi-regular tips (you can get them via email or online), and just today there's a new one on strftime.

这篇关于如何在Perl格式化日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆