Skip to content

Instantly share code, notes, and snippets.

@hwada
Created July 21, 2020 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwada/fa5f34e81604ebaf3887e1112c71ecd1 to your computer and use it in GitHub Desktop.
Save hwada/fa5f34e81604ebaf3887e1112c71ecd1 to your computer and use it in GitHub Desktop.
function parseTime(text) {
{
const match = /(\d\d?)時(\d\d?)分/.exec(text);
if (match && match.length === 3) {
return [match[1], match[2]];
}
}
{
const match = /(\d\d?)時/.exec(text);
if (match && match.length === 2) {
return [match[1], 0];
}
}
{
const match = /(\d\d?):(\d\d?)/.exec(text);
console.log(match);
if (match && match.length >= 3) {
return [match[1], match[2]];
}
}
return [0, 0];
}
function parseDate(text) {
const [h, m] = parseTime(text);
{
const match = /(\d*?)年(\d*?)月(\d*?)日/.exec(text);
if (match && match.length >= 4) {
return new Date(match[1], match[2] - 1, match[3], h, m);
}
}
{
const match = /(\d*?)月(\d*?)日/.exec(text);
if (match && match.length >= 3) {
const year = (new Date()).getFullYear(); // 年がなければ今年にする
return new Date(year, match[1] - 1, match[2], h, m);
}
}
{
// yyyy/mm/dd
const match = /(\d\d\d\d)\/(\d\d?)\/(\d\d?)/.exec(text);
if (match && match.length >= 4) {
return new Date(match[1], match[2] - 1, match[3], h, m);
}
}
{
// mm/dd
const match = /(\d\d?)\/(\d\d?)/.exec(text);
if (match && match.length >= 3) {
const year = (new Date()).getFullYear(); // 年がなければ今年にする
return new Date(year, match[1] - 1, match[2], h, m);
}
}
{
if (text.indexOf('本日') >= 0) {
const dt = new Date();
return new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(), h, m);
}
}
return null;
}
function trigger(tweet) {
const calendar = CalendarApp.getDefaultCalendar();
const text = tweet.text;
const lines = text.split('\n');
const title = lines[0];
const startTime = parseDate(text);
if (!startTime) {
return;
}
const option = {
description: text
}
calendar.createEvent(title, startTime, startTime, option);
}
function doPost(e) {
var params = JSON.parse(e.postData.getDataAsString());
trigger(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment