question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting timezoneOffset gives incorrect time when parsing relative time

See original GitHub issue
process.env.TZ='utc';

var relative = chrono.parse("in two minutes");
relative[0].start.assign('timezoneOffset', 120);

console.log(relative[0].start.date()); //2017-02-09T07:21:17.000Z

var absolute_date = new Date();
absolute_date.setMinutes(absolute_date.getMinutes()+2);

var absolute = chrono.parse(`${absolute_date.getHours()}:${absolute_date.getMinutes()}`);
relative[0].start.assign('timezoneOffset', 120);

console.log(absolute[0].start.date()); //2017-02-09T09:21:00.000Z

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ptxyzcommented, Jun 17, 2017

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 timezoneOffeset However, if I do set this, then situations like ‘now’ still present a problem because it appears that the implied time values originate from system time. So it will imply the system hour, minute, second based on the system time.

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 timezoneOffset value only in cases where the implied values are not derived from system time. Since I don’t know the nature of the text ahead of time, an ideal situation would allow me to notify the parser to expect inputs to be in specified timezone, and then situations like ‘now’ or ‘in two minutes’, where timezone is irrelevant, would simply ignore that value.

I’ve also provided a runkit notebook to illustrate the problem. https://runkit.com/pthoresen/5944c4065e466600126d5001

1reaction
wanasitcommented, Jun 17, 2017

I think you miss understand the Chrono’s ParsedComponents and assign('timezoneOffset', ..).

The component is machine’s timezone independent. The component’s timezoneOffset is the parsed timezone or the timezone mentioned in the input.

> chrono.parse('Today 12PM JST')[0].start.get('timezoneOffset')
540
> chrono.parse('Today 12PM UTC')[0].start.get('timezoneOffset')
0
> chrono.parse('Today 12PM')[0].start.get('timezoneOffset')
undefined

The date() function create Javascript Date object from the component, which includes adjusting the parsed timezone into your local system (The Date object is also actually machine independent timestamp, but we still need to adjust the mentioned timezone into correct timestamp). If the component doesn’t have timezone then, only that point, it assume that the parsed timezone is the same as the current timezone.

> chrono.parse('Today 12PM JST')[0].start.date().toString()
'Sat Jun 17 2017 12:00:00 GMT+0900 (UTC)'
> chrono.parse('Today 12PM UTC')[0].start.date().toString()
'Sat Jun 17 2017 21:00:00 GMT+0900 (UTC)'

// Because I am in Japan (JST)
> chrono.parse('Today 12PM')[0].start.date().toString()
'Sat Jun 17 2017 12:00:00 GMT+0900 (UTC)'

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.

results.forEach(function(result) {
  if (! result.start.isCertain('timezoneOffset')) {
    result.start.assign('timezoneOffset', XXX)
  }
})

But, if you are thinking about using assign('timezoneOffset', ..) to transform the output to into a specific timezone, then you are doing it wrong.

Again, ‘timezoneOffset’ is all about the timezone mentioned in the text, not output or machine timezone.

  • Do use assign(‘timezoneOffset’, …) if you know the timezone of the text/input.
  • Do NOT use assign(‘timezoneOffset’, …) to select output timezone.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Parser.parse changes to wrong time zone - Stack Overflow
I am using from dateutil import parser to parse an input but for some reason it is giving me back the wrong timezone....
Read more >
Handling Time Zone in JavaScript - TOAST UI - Medium
The difference of time between UTC standard time and the local time is called “offset”, which is expressed in this way: +09:00 ,...
Read more >
How to handle Time Zones in JavaScript - Bits and Pieces
ISOString — It can parse a text containing any time zone's numeric UTC offset. The output Date object does not keep the original...
Read more >
Converting between DateTime and DateTimeOffset
LocalDateTime property to perform both a type and a time zone conversion. The sample output is for a machine set to the Pacific...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
This problem of managing two different versions of the time, relative to the ... Once you know how to parse a date and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found