Skip to content

Instantly share code, notes, and snippets.

@takihito
Created June 5, 2014 10:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takihito/c6afdb145ce69e388fda to your computer and use it in GitHub Desktop.
Save takihito/c6afdb145ce69e388fda to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# crontab -l | ./cron2cal > ~/tmp/cron.csv
use utf8;
use strict;
use warnings;
use Encode;
use DateTime;
my $comment;
my $cron;
print "Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private\n";
while ( <STDIN> ) {
my $stdin = $_;
$cron = "";
chomp $stdin;
$comment = $stdin if $stdin =~ /^#/;
$cron = $stdin if $stdin =~ /^(\d)/;
next unless $cron;
my ($minute, $hour, $day, $month, $week, @com ) = split(' ', $cron);
# 明確な時刻指定がないもの(範囲指定/,分単位定期実行)は取り除く
next if $minute !~ /^\d+$/ || $hour !~ /^\d+$/;
my $dt = DateTime->now->set_time_zone('Asia/Tokyo');
$dt->add(days => 1 );
if ( $month =~ /^\d+$/ ) {
$dt->set_month($month) if $month =~ /^\d+$/;
} elsif( $day =~ /^\d+$/ && $day < $dt->day) {
$dt->add(months => 1 );
}
$dt->set_day($day) if $day =~ /^\d+$/;
$dt->set_hour($hour) if $hour =~ /^\d+$/;
$dt->set_minute($minute) if $minute =~ /^\d+$/;
$dt->set_second(0);
my $Subject = "cron:".$comment;
my $Start_Date = $dt->strftime("%Y-%m-%d");
my $Start_Time = $dt->strftime("%H:%M:%S");
my $End_Date = $dt->add(minutes => 15 )->strftime("%Y-%m-%d");
my $End_Time = $dt->strftime("%H:%M:%S");
my $All_Day_Event = 'False';
my $Description = join(' ', @com);
my $Location = "crontab";
my $Private = 'True';
my $row = join(',', $Subject,$Start_Date,$Start_Time,$End_Date,$End_Time,$All_Day_Event,$Description,$Location,$Private);
printf("%s\n", $row);
}
@takihito
Copy link
Author

takihito commented Jun 5, 2014

こんな感じで実行して

$ crontab -l  | ./cron2cal > ~/tmp/cron.csv

Googleカレンダーへcron.csvをインポートすると、翌日のカレンダーにcrontabスケジュールが表示されます

https://support.google.com/calendar/answer/45656?hl=ja

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment