I am trying to understand how yyTimezone is calculated in code below:
| bare_time '+' tUNUMBER {
/* "7:14+0700" */
yyDSTmode = DSToff;
yyTimezone = - ($3 % 100 + ($3 / 100) * 60);
}
| bare_time '-' tUNUMBER {
/* "19:14:12-0530" */
yyDSTmode = DSToff;
yyTimezone = + ($3 % 100 + ($3 / 100) * 60);
}
How I understand is, lets say the timestamp is 2011-01-02T10:15:20-04:00; this means its 0400 hours behind UTC. So to convert it into UTC, you add 0400 hours to it and it becomes 2011-01-02T14:15:20. Is my understanding correct?
How is that achieved in the codeblock I pasted above?
The input would encode the offset like
-0400. The0400part of that would be returned as thetUNUMBERtoken (presumably holding an unsigned value). This token is matched by the grammar rules, and can be used as$3.To get the actual offset in minutes from the value
400, you first have to split it up in two halves. The hours part can be obtained with$3 / 100(ie.4), and the minutes part with$3 % 100(ie.0). Since there are 60 minutes in an hour, you multiply the hours by 60, and add the minutes to that ($3 % 100 + ($3 / 100) * 60), which would give the value240. Then all that’s left, is to add the sign, and store it inyyTimezone.After all that,
yyTimezonewill contain the timezone offset in minutes.