问题
http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/usr.bin/tar/Attic/getdate.y?rev=1.9.12.1;content-type=text%2Fplain;hideattic=0
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?
回答1:
The input would encode the offset like -0400
. The 0400
part of that would be returned as the tUNUMBER
token (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 value 240
. Then all that's left, is to add the sign, and store it in yyTimezone
.
After all that, yyTimezone
will contain the timezone offset in minutes.
来源:https://stackoverflow.com/questions/7185737/getdate-y-grammar-doubts