Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom formats for label timestamps #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ Either way, the thread will reappear back in your inbox at the date+time you spe

1. **Set up the script:**
* Go to [script.google.com](https://script.google.com/), and create a new script, called "Tickler" or something.
* Paste in code from the `gmail-tickler.gs` file in this repository. At the top of the script you will find some options to configure. If you are using the tickle-by-email-address feature, you must configure your email address.
* Paste in code from the `gmail-tickler.gs` file in this repository. At the top of the script you will find some options to configure.
* If you are using the tickle-by-email-address feature, you must configure your email address.
* If you want to change the timestamp format, you can use the tokens described [here](http://momentjs.com/docs/#/parsing/string-format/), and you can escape characters by wrapping them in `[square brackets]`. Note that this format must fully-specify a date and time for the script to work properly. If you decide to change this timestamp in the future, you must either empty or re-name existing labels manually.
* Go to `File -> New -> Script file`, create a new file named `moment` and paste in code from the `moment.gs` file in this repository.

2. **Set timezone:** To make sure the Tickler script runs in your native timezone, open the script editor in Google Apps, and go to the "File" menu, "Project properties." There you will find a timezone configuration.

Expand Down
40 changes: 4 additions & 36 deletions gmail-tickler.gs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var TICKLE = {
label_prefix: BASE_LABEL + '/@',
default_time: "8:00", // hh:mm, 24-hour format

timestamp_format: "YYYY-MM-DD[ at ]HH:mm", // must fully-specify a timestamp, see README
cleanup_labels: true, // remove the label containing the initial tickle-command
cleanup_exempt: // ... unless it's one of these:
[ "tomorrow", "sun", "mon", "tue",
Expand Down Expand Up @@ -127,7 +128,7 @@ function tickleLabel(lbl) {
errorLabel(lbl, target);

} else {
var newlblname = TICKLE.label_prefix + "/" + date2str(target);
var newlblname = TICKLE.label_prefix + "/" + moment(target).format(TICKLE.timestamp_format);
Logger.log("tickleLabel: moving " + threads.length + " items to label " + newlblname);

if (DRY_RUN) return;
Expand Down Expand Up @@ -165,7 +166,7 @@ function emailTickleLabel(lbl) {
errorLabel(lbl, target);

} else {
var newlblname = TICKLE.label_prefix + "/" + date2str(target);
var newlblname = TICKLE.label_prefix + "/" + moment(target).format(TICKLE.timestamp_format);
Logger.log("emailTickleLabel: moving thread to label " + newlblname);

if (DRY_RUN) return;
Expand Down Expand Up @@ -205,7 +206,7 @@ function extractEmailTickleInfo(t) {
function restoreLabel(lbl) {
// TICKLE.label guaranteed to be prefix of lbl.getName(); +1 is for trailing slash
var datestr = lbl.getName().substr( TICKLE.label_prefix.length + 1 );
var target = str2date(datestr);
var target = moment(datestr, TICKLE.timestamp_format).toDate();
if (!target) {
if (!datestr) errorLabel(lbl, "don't understand " + datestr);
return;
Expand Down Expand Up @@ -249,39 +250,6 @@ function errorLabel(lbl, errMsg) {
}
}



function twodigit (v) {
if (v < 10) v = "0" + v;
return "" + v;
}

function date2str (d) {
var Y, M, D, h, m;
Y = d.getFullYear();
M = twodigit( d.getMonth() + 1 );
D = twodigit( d.getDate() );
h = twodigit( d.getHours() );
m = twodigit( d.getMinutes() );

return Y + "-" + M + "-" + D + " at " + h + ":" + m;
}

function str2date (s) {
var matches = s.match(/^(\d{4})-(\d\d)-(\d\d) at (\d\d):(\d\d)$/);
if (!matches) return;

var d = new Date();
d.setYear( matches[1] );
d.setMonth( matches[2]-1 );
d.setDate( matches[3] );
d.setHours( matches[4] );
d.setMinutes( matches[5] );
d.setSeconds(0);
d.setMilliseconds(0);
return d;
}

var DOW = { sun:0, mon:1, tue:2, wed:3, thu:4, fri:5, sat:6 };
var MONTHS = { jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6, aug:7, sep:8, oct:9, nov:10, dec:11 };

Expand Down
Loading