使用perl和mysql日期格式提取星期几

时间:2012-06-06 17:59:59

标签: mysql perl datetime

我正在循环一系列连续日期。我需要找出哪些是周末所以可以丢弃它们。一种方法是通过perl(我的脚本编写的内容)或每次遍历循环查询来确定星期几。通常不会超过30个日期,通常为5个或更少。加载Date :: Time并使用perl或运行查询而不需要模块会更有效吗?如果perl是最好的方法,我可以使用一些帮助从YYYY-MM-DD格式中提取星期几。我可以使用1-7号或短名称mon-sun。

2012-05-01
2012-05-02
2012-05-03
2012-05-04
2012-05-05
2012-05-06

不确定这是否可行但是也许更合适的解决方案是写一个查询(因为我知道开始和结束并且它们是连续的)来计算x和y之间的天数,其中dayofweek not IN(6,7)

5 个答案:

答案 0 :(得分:3)

请参阅DateTime::Format::StrptimeDateTime

use 5.010;
use strictures;
use DateTime::Format::Strptime qw();
my $parser = DateTime::Format::Strptime->new(pattern => '%F');
for my $date (qw(
    2012-05-01
    2012-05-02
    2012-05-03
    2012-05-04
    2012-05-05
    2012-05-06
)) {
    my $dow = $parser->parse_datetime($date)->day_of_week;
    say "$date is a weekend day" if 6 == $dow or 7 == $dow;
}

答案 1 :(得分:2)

MySQL有day of week function你可以直接使用。

答案 2 :(得分:2)

最明显的解决方案是使用Time::Piece模块,该模块自v5.9以来一直是Perl的核心模块,因此可能不需要在您的系统上安装。

wday方法返回一周的数字日,其中1 ==星期日,因此对于周末,您要查找7(星期六)或1的值。可以调整此值,以便星期六用零表示而是(和周日1)写作

my $dow = $tp->wday % 7;

之后,周末的测试只是$dow < 2

以下是一些要演示的代码。

use strict;
use warnings;

use Time::Piece;

while (<DATA>) {
  chomp;
  my $tp = Time::Piece->strptime($_, '%Y-%m-%d');
  my $dow = $tp->wday % 7;
  print $_;
  print " (weekend)" if $dow < 2;
  print "\n";
}

__DATA__
2012-05-01
2012-05-02
2012-05-03
2012-05-04
2012-05-05
2012-05-06

<强>输出

2012-05-01
2012-05-02
2012-05-03
2012-05-04
2012-05-05 (weekend)
2012-05-06 (weekend)

答案 3 :(得分:2)

您可以使用核心Time::Local模块,然后使用localtime计算工作日。工作日0对应星期日,6星期六。

#! /usr/bin/env perl

use strict;
use warnings;

use Time::Local;

my @dates = qw(
  2012-05-01
  2012-05-02
  2012-05-03
  2012-05-04
  2012-05-05
  2012-05-06
);

my @days = qw/ Sun Mon Tue Wed Thu Fri Sat /;

foreach my $date (@dates) {
  my($yyyy,$mm,$dd) = split /-/, $date;
  my $time_t = timelocal 0, 0, 0, $dd, $mm-1, $yyyy-1900;
  my $wday = (localtime $time_t)[6];
  my $weekend = ($wday == 0 || $wday == 6) ? " *" : "";

  print "$date: $days[$wday] ($wday)$weekend\n";
}

输出:

2012-05-01: Tue (2)
2012-05-02: Wed (3)
2012-05-03: Thu (4)
2012-05-04: Fri (5)
2012-05-05: Sat (6) *
2012-05-06: Sun (0) *

为了好玩,你可以去Swiss Army Chainsaw并刮掉 cal 实用程序的输出。

#! /usr/bin/env perl

use strict;
use warnings;

use 5.10.0;  # for smart matching

sub weekday_type {
  my($date) = @_;
  die "$0: unexpected date '$date'"
    unless my($yyyy,$mm,$dd) =
             $date =~ /^([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})$/;

  my $calendar = `cal -m $mm $yyyy`;
  die "$0: cal -m $mm $yyyy failed" if $?;

  for (split /\n/, $calendar) {
    if (/^ \s* [0-9]{1,2} (?: \s+ [0-9]{1,2})* \s*$/x) {
      my @dates = split;
      my @weekend = splice @dates, @dates > 1 ? -2 : -1;
      return "weekend" if ($dd+0) ~~ @weekend;
    }
  }

  "weekday";
}

中使用它
my @dates = qw(
  2012-05-01
  2012-05-02
  2012-05-03
  2012-05-04
  2012-05-05
  2012-05-06
);

my @days = qw/ Sun Mon Tue Wed Thu Fri Sat /;

foreach my $date (@dates) {
  my $type = weekday_type $date;
  print "$date: $type\n";
}

输出:

2012-05-01: weekday
2012-05-02: weekday
2012-05-03: weekday
2012-05-04: weekday
2012-05-05: weekend
2012-05-06: weekend

我不建议在生产中这样做。

答案 4 :(得分:0)

use Date::Simple qw/date/;
use Date::Range;

my ( $start, $end ) = ( date('2012-05-02'), date('2012-05-16') );
my $range = Date::Range->new( $start, $end );
my @all_dates = $range->dates;

foreach my $d (@all_dates) {
  my $date  = Date::Simple->new($d);
  print $date->day_of_week." ".$d."<br />";
  }

#--OUTPUT--#

3 2012-05-02
4 2012-05-03
5 2012-05-04
6 2012-05-05
0 2012-05-06
1 2012-05-07
2 2012-05-08
3 2012-05-09
4 2012-05-10
5 2012-05-11
6 2012-05-12
0 2012-05-13
1 2012-05-14
2 2012-05-15
3 2012-05-16