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

Age displays as "-1871" in email if contact has year of birth #166

Closed
Rabattkarte opened this issue Apr 1, 2020 · 2 comments
Closed

Age displays as "-1871" in email if contact has year of birth #166

Rabattkarte opened this issue Apr 1, 2020 · 2 comments
Assignees
Labels
bug Related to a bug or a critical problem in the code. minor change Minor, small, non-breaking changes to the code. solved The problem has been solved and the code is waiting to be deployed in production.

Comments

@Rabattkarte
Copy link

Steps to reproduce

What action or series of actions is the cause of the issue?

  1. Get a reminder for a birthday including the year, e.g. "8. April 1991"
  2. see age as "-1871" in email

Expected behavior

I expect the real age to be displayed in the email.

Current behavior

I see age as "-1871", which obviously is wrong.

Context

Google Contacts Events Notifier (Version 5.0.1)

Birthday of the contact for testing purposes: 8. April 1991.

Extended description

not applicable.

@Rabattkarte
Copy link
Author

Debugging line 607 of code.gs reveals, that date.getYear() seems broken:

// line.push(Math.round(date.getYear() - event.getProp('year')));
line.push('date.getYear(): ', date.getYear(), ', event.getProp(\'year\'): ', event.getProp('year'), ', calc: ', Math.round(date.getYear() - event.getProp('year')));

Output:

Years: date.getYear(): 119, event.getProp('year'): 2018, calc: -1899

@GioBonvi
Copy link
Owner

GioBonvi commented Apr 8, 2020

Hello @Rabattkarte: thank you for bringing this up.
This problem was already flagged by another user, but I could not reproduce it, so I was not able to debug it, however you found the source of the problem and by looking aroud some more I think it is linked to the new V8 engine Google Script runs on.

  • The Google Script platform is switching to a Javascript V8 engine

    Historically, Apps Script has been powered by Mozilla's Rhino JavaScript interpreter. While Rhino provided a convenient way for Apps Script to execute developer scripts, it also tied Apps Script to a specific JavaScript version (ES5). Apps Script developers can't use more modern JavaScript syntax and features in scripts using the Rhino runtime.
    To address this concern, Apps Script is now supported by the V8 runtime that powers Chrome and Node.js.

  • I haven't looked deeply into this, however it seems users can switch old projects to use the new engine with a click on a notification which appears at the top of the page and new users installing the script for the first time may have it enabled by default.
  • In the old engine the date.getYear() method returned the full 4-digit year, while in the new engine date.getYear() returns the years passed since 1900: the full 4-digit year is returned by date.getFullYear() instead. (This was described here)
    // Using the new engine.
    var date = new Date(2020, 1, 1);
    console.log(date.getYear());     // "120"
    console.log(date.getFullYear()); // "2020"
  • The origin of the bug is probably a mismatch beteween the (deprecated) getYear() method of a JavaScript Date Object and the (perfectly valid) getYear() method of a Google Script DateField Object which is used here in the code.

I am pushing a fix and releasing an updated version to address this. I have looked into the list of caveats regarding migration to the new engine and the rest of the code should work OK: the tests I have run confirm this.

Thank you again @Rabattkarte for solving this!

@GioBonvi GioBonvi self-assigned this Apr 8, 2020
@GioBonvi GioBonvi added bug Related to a bug or a critical problem in the code. minor change Minor, small, non-breaking changes to the code. solved The problem has been solved and the code is waiting to be deployed in production. labels Apr 8, 2020
GioBonvi added a commit that referenced this issue Apr 8, 2020
The Google Script platform is activating support for the V8
JavaScript engine, abandoning the old Rhino interpreter.
In the old engine the Date.prototype.getYear() method returned the
full 4-digit year of a date, while in the new engine it returns
only the number of years since 1900.
This change led to issue #166.

The solution was simply to use the proper method:
Date.prototype.getFullYear().

See:
https://developers.google.com/apps-script/guides/v8-runtime
https://developers.google.com/apps-script/guides/v8-runtime/migration
@GioBonvi GioBonvi closed this as completed Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Related to a bug or a critical problem in the code. minor change Minor, small, non-breaking changes to the code. solved The problem has been solved and the code is waiting to be deployed in production.
Projects
None yet
Development

No branches or pull requests

2 participants