1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
3502 * Constructor.
3503 *
3504 * @param style the style, not null
3505 */
3506 LocalizedOffsetIdPrinterParser(TextStyle style) {
3507 this.style = style;
3508 }
3509
3510 private static StringBuilder appendHMS(StringBuilder buf, int t) {
3511 return buf.append((char)(t / 10 + '0'))
3512 .append((char)(t % 10 + '0'));
3513 }
3514
3515 @Override
3516 public boolean format(DateTimePrintContext context, StringBuilder buf) {
3517 Long offsetSecs = context.getValue(OFFSET_SECONDS);
3518 if (offsetSecs == null) {
3519 return false;
3520 }
3521 String gmtText = "GMT"; // TODO: get localized version of 'GMT'
3522 if (gmtText != null) {
3523 buf.append(gmtText);
3524 }
3525 int totalSecs = Math.toIntExact(offsetSecs);
3526 if (totalSecs != 0) {
3527 int absHours = Math.abs((totalSecs / 3600) % 100); // anything larger than 99 silently dropped
3528 int absMinutes = Math.abs((totalSecs / 60) % 60);
3529 int absSeconds = Math.abs(totalSecs % 60);
3530 buf.append(totalSecs < 0 ? "-" : "+");
3531 if (style == TextStyle.FULL) {
3532 appendHMS(buf, absHours);
3533 buf.append(':');
3534 appendHMS(buf, absMinutes);
3535 if (absSeconds != 0) {
3536 buf.append(':');
3537 appendHMS(buf, absSeconds);
3538 }
3539 } else {
3540 if (absHours >= 10) {
3541 buf.append((char)(absHours / 10 + '0'));
3542 }
3543 buf.append((char)(absHours % 10 + '0'));
3544 if (absMinutes != 0 || absSeconds != 0) {
3548 buf.append(':');
3549 appendHMS(buf, absSeconds);
3550 }
3551 }
3552 }
3553 }
3554 return true;
3555 }
3556
3557 int getDigit(CharSequence text, int position) {
3558 char c = text.charAt(position);
3559 if (c < '0' || c > '9') {
3560 return -1;
3561 }
3562 return c - '0';
3563 }
3564
3565 @Override
3566 public int parse(DateTimeParseContext context, CharSequence text, int position) {
3567 int pos = position;
3568 int end = pos + text.length();
3569 String gmtText = "GMT"; // TODO: get localized version of 'GMT'
3570 if (gmtText != null) {
3571 if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) {
3572 return ~position;
3573 }
3574 pos += gmtText.length();
3575 }
3576 // parse normal plus/minus offset
3577 int negative = 0;
3578 if (pos == end) {
3579 return context.setParsedField(OFFSET_SECONDS, 0, position, pos);
3580 }
3581 char sign = text.charAt(pos); // IOOBE if invalid position
3582 if (sign == '+') {
3583 negative = 1;
3584 } else if (sign == '-') {
3585 negative = -1;
3586 } else {
3587 return context.setParsedField(OFFSET_SECONDS, 0, position, pos);
3588 }
3589 pos++;
3590 int h = 0;
3591 int m = 0;
3592 int s = 0;
3593 if (style == TextStyle.FULL) {
3594 int h1 = getDigit(text, pos++);
3595 int h2 = getDigit(text, pos++);
|
1 /*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
3502 * Constructor.
3503 *
3504 * @param style the style, not null
3505 */
3506 LocalizedOffsetIdPrinterParser(TextStyle style) {
3507 this.style = style;
3508 }
3509
3510 private static StringBuilder appendHMS(StringBuilder buf, int t) {
3511 return buf.append((char)(t / 10 + '0'))
3512 .append((char)(t % 10 + '0'));
3513 }
3514
3515 @Override
3516 public boolean format(DateTimePrintContext context, StringBuilder buf) {
3517 Long offsetSecs = context.getValue(OFFSET_SECONDS);
3518 if (offsetSecs == null) {
3519 return false;
3520 }
3521 String gmtText = "GMT"; // TODO: get localized version of 'GMT'
3522 buf.append(gmtText);
3523 int totalSecs = Math.toIntExact(offsetSecs);
3524 if (totalSecs != 0) {
3525 int absHours = Math.abs((totalSecs / 3600) % 100); // anything larger than 99 silently dropped
3526 int absMinutes = Math.abs((totalSecs / 60) % 60);
3527 int absSeconds = Math.abs(totalSecs % 60);
3528 buf.append(totalSecs < 0 ? "-" : "+");
3529 if (style == TextStyle.FULL) {
3530 appendHMS(buf, absHours);
3531 buf.append(':');
3532 appendHMS(buf, absMinutes);
3533 if (absSeconds != 0) {
3534 buf.append(':');
3535 appendHMS(buf, absSeconds);
3536 }
3537 } else {
3538 if (absHours >= 10) {
3539 buf.append((char)(absHours / 10 + '0'));
3540 }
3541 buf.append((char)(absHours % 10 + '0'));
3542 if (absMinutes != 0 || absSeconds != 0) {
3546 buf.append(':');
3547 appendHMS(buf, absSeconds);
3548 }
3549 }
3550 }
3551 }
3552 return true;
3553 }
3554
3555 int getDigit(CharSequence text, int position) {
3556 char c = text.charAt(position);
3557 if (c < '0' || c > '9') {
3558 return -1;
3559 }
3560 return c - '0';
3561 }
3562
3563 @Override
3564 public int parse(DateTimeParseContext context, CharSequence text, int position) {
3565 int pos = position;
3566 int end = text.length();
3567 String gmtText = "GMT"; // TODO: get localized version of 'GMT'
3568 if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) {
3569 return ~position;
3570 }
3571 pos += gmtText.length();
3572 // parse normal plus/minus offset
3573 int negative = 0;
3574 if (pos == end) {
3575 return context.setParsedField(OFFSET_SECONDS, 0, position, pos);
3576 }
3577 char sign = text.charAt(pos); // IOOBE if invalid position
3578 if (sign == '+') {
3579 negative = 1;
3580 } else if (sign == '-') {
3581 negative = -1;
3582 } else {
3583 return context.setParsedField(OFFSET_SECONDS, 0, position, pos);
3584 }
3585 pos++;
3586 int h = 0;
3587 int m = 0;
3588 int s = 0;
3589 if (style == TextStyle.FULL) {
3590 int h1 = getDigit(text, pos++);
3591 int h2 = getDigit(text, pos++);
|