-
Notifications
You must be signed in to change notification settings - Fork 344
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
Setting timezoneOffset gives incorrect time when parsing relative time #175
Comments
I also am experiencing this problem. I wonder if it's possible to add a feature to specify the implied or known timezone before the parse occurs? For instance, if I'm in GMT at 00:00 on January 1 and I parse |
|
I think you miss understand the Chrono's The component is machine's timezone independent. The component's
The
So, if you know that all of your text/input will come from a specific timezone, you could write a refiner that assign the timezone to all the results.
But, if you are thinking about using Again, 'timezoneOffset' is all about the timezone mentioned in the text, not output or machine timezone.
|
Thank you @wanasit for the clear and detailed response. The inner workings of your library are much clearer now after reading this. Respectfully, I still feel that there is a bug in specific cases where the implied values are derived from system time. In these cases, specifying a timezone will result in a time shift. Logically, I understand how this case occurs and it very well may be a case of 'working as designed' To give you some background, I'm processing text from all over the world. I do know ahead of time which timezone the text will originate from, so I feel comfortable using a refiner as you suggested to set a known value for When I then tell it that I have a known timezone, it's still using the implied values that originated from my system, combined with a known timezone offset, which results in a time shift. The implication of this is that I would need to conditionally apply the I've also provided a runkit notebook to illustrate the problem. |
@pthoresen Thank you for explaining what are you working on. Here I think, then, you need to adjust your reference time in addition to the timezone. In example, if I want to interpret "now" correctly, I need to know
The reference time should be the time when the text is written. If you don't know that time, you simply cannot know what "now" means. I am not sure how do you store the time, but if you have Javascript's Date object, the Date object is already timezone-independent timestamp. var date1 = new Date('Sat Jun 17 2017 12:00:00 GMT+0900 (UTC)')
var date2 = new Date('Sat Jun 17 2017 03:00:00 UTC')
console.log(date1.getTime())
console.log(date2.getTime()) The both outputs of code above should print "1497668400000" regardless of your computer timezone. If you have the correct reference time and timezone for the input, Chrono should be able to create the correct/precise output. // Suppose the text is written at this time. At this exact moment.
var ref = new Date('Sat Jun 17 2017 12:00:00 GMT+0900 (UTC)')
// Parse with your custom timzoneOffset=120 chrono
console.log( custom.parseDate('this Sunday', ref) );
console.log( custom.parseDate('this Sunday', ref).getTime() ); My first line output print "Sun Jun 11 2017 19:00:00 GMT+0900 (JST)", you will get a different text (but the same meaning) depending on your computer format. However, the second output will always print "1497175200000". So, if you correctly pass the "complete" input (text + reference time + timezone), Chrono should work the same way and conclude the same result regardless of local timezone. |
I am assuming the var chronoNode = require("chrono-node")
console.log(new Date())
// Fri Feb 07 2020 08:23:24 GMT+0700 (WIB)
const result = chronoNode.parse('now', new Date())[0].start;
const result2 = chronoNode.parse('in 30 mins', new Date())[0].start;
const result3 = chronoNode.parse('today at 10pm', new Date())[0].start;
result.assign('timezoneOffset', 8 * 60);
result2.assign('timezoneOffset', 8 * 60);
result3.assign('timezoneOffset', 8 * 60);
console.log('result (now)', result.date())
// Fri Feb 07 2020 07:23:24 GMT+0700 (WIB) => This is wrong, should be Fri Feb 07 2020 08:23:24 GMT+0700 (WIB)
console.log('result2 (in 30 mins)', result2.date())
// Fri Feb 07 2020 07:53:24 GMT+0700 (WIB) => This is wrong, should be Fri Feb 07 2020 08:53:24 GMT+0700 (WIB)
console.log('result3 (today at 10pm)', result3.date())
// Fri Feb 07 2020 21:00:00 GMT+0700 (WIB) => This is correct Did I miss any step? |
I'm facing the same issue when parsing with reference to user timezone. |
Hello. Sorry for very my slow reply. I am actually not sure what is the problem with @muhajirdev's scenario. First, you are in GMT+0700 timezone. console.log(new Date())
// Fri Feb 07 2020 08:23:24 GMT+0700 (WIB)
const result = chronoNode.parse('now', new Date())[0].start;
// result = { hour: 8, minute: 23, second: 24, ..., timezone: 420 (GMT+0700) } Then, you are assigning result.assign('timezoneOffset', 8 * 60);
// result = { hour: 8, minute: 23, second: 24, ..., timezone: 480 (GMT+0800) }
...
console.log('result (now)', result.date())
// Fri Feb 07 2020 07:23:24 GMT+0700 (WIB) => This is expected 8:23 in GMT+0800 is 7:23 in your timezone |
@wanasit I'm not sure about @muhajirdev's example but consider this:
Then we assign his timezone: Now the first input is parsed correctly, but the second is not.
Which were supposed to have the same result. I think the fix should be that when the relative parser is used, the |
@tamirvs ok. Thank you for the explanation. I think I understand what wrong now. When we are parsing relative time mention, we should also take into account the reference's timezone (just like we do the reference's hour and minute). This has been implemented in 344026e patch (v2.1.10) I also added the scenario you mentioned in the const refDate = new Date("Sun Nov 29 2020 13:24:13 GMT+0900 (JST)");
{
const text = "tomorrow at 5pm";
const result = chrono.parse(text, refDate)[0] as ParsingResult;
result.start.imply("timezoneOffset", 60);
expect(result).toBeDate(new Date("Sun Dec 1 2020 1:00:00 GMT+0900 (JST)"));
expect(result).toBeDate(new Date("Sun Nov 30 2020 17:00:00 GMT+0100"));
}
{
const text = "in 10 minutes";
const result = chrono.parse(text, refDate)[0] as ParsingResult;
result.start.imply("timezoneOffset", 60);
expect(result).toBeDate(new Date("Sun Nov 29 2020 13:34:13 GMT+0900 (JST)"));
expect(result).toBeDate(new Date("Sun Nov 29 2020 5:34:13 GMT+0100"));
} Note that you suppose to use
|
@wanasit It looks like you solved it! thanks! |
The text was updated successfully, but these errors were encountered: