-1

過去3か月の日曜日をperlスクリプトで表示したい

たとえば、今日が 2013 年 1 月 20 日の日曜日で、今から 3 か月前の日曜日であるとします。

  2013-01-20
  .
  .
  2013-01-06
  .
  .
  2012-12-30

  2012-12-02
  .
  .
  2012-11-25
  .
  .
  2012-11-04

現在の日時に基づいて、過去 3 か月の日曜日を変更する必要があります。

Linux用のkshスクリプトで同じことが必要です

前もって感謝します。


これがコードです..それは先週の日曜日を与えています..しかし私は過去3か月の日曜日が必要です

#!/usr/bin/perl

$today = date(time);
$weekend = date2(time);

sub date {
     my($time) = @_;     

     @when = localtime($time);
     $dow=$when[6];
     $when[5]+=1900;
     $when[4]++;
     $date = $when[5] . "-" . $when[4] . "-" . $when[3];

     return $date;
}


sub date2 {
     my($time) = @_;     # incoming parameters

     $offset = 0;
     $offset = 60*60*24*$dow;
     @when = localtime($time - $offset);
     $when[5]+=1900;
     $when[4]++;
     $date = $when[5] . "-" . $when[4] . "-" . $when[3];

     return $date;
}


print "$weekend \n";

ありがとう !!

4

2 に答える 2

0

ksh の場合 (pdksh および GNU coreutils date でテスト済み):

timestamp=`date +%s`
date=`date --date=@$timestamp +%F`
month=`date --date=@$timestamp +%Y-%m`
for months in 1 2 3; do
    while [[ $month == `date --date=@$timestamp +%Y-%m` ]]
    do
        if [[ 7 == `date --date=@$timestamp +%u` ]]; then echo $date; fi
        let timestamp-=12*60*60
        date=`date --date=@$timestamp +%F`
    done
    month=`date --date=@$timestamp +%Y-%m`
done | uniq
于 2013-01-20T18:08:56.383 に答える
0

Perl の DateTime モジュールを使用した簡単なソリューション。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use DateTime;

# Get the current date and time
my $now = DateTime->now;

# Work out the previous Sunday
while ($now->day_of_week != 7) {
  $now->subtract(days => 1);
}

# Go back 13 weeks from the previous Sunday
my $then = $now->clone;
$then->subtract(weeks => 13);

# Decrement $now by a week at a time until
# you reach $then
while ($now >= $then) {
  say $now->ymd;
  $now->subtract(weeks => 1);
}
于 2013-01-21T13:19:38.623 に答える