Нечетное поведение, когда Java преобразовывает интервал в байт?

В идеальном мире у вас есть идентификатор часового пояса региона, а не статический TZ, подобный тому, который у вас есть, потому что они не осведомлены о DST и не понимают политику времени. Если ваш единственный вариант - работать с форматом GMTxy, вы можете использовать Etc/GMTxy, см. Таблицу ниже:

https://github.com/eggert/tz/blob/2017b/etcetera# L36-L42

и https://momentjs.com/timezone/docs/#/zone-object/offset/

Так что это будет что-то как:

const timezone = `Etc/GMT${parseInt(data.time_zone.replace(/GMT/, ''))}`
moment(date).tz(timezone).format()

(обратите внимание, я не проверил это полностью)

128
задан Pacerier 28 May 2019 в 23:40
поделиться

3 ответа

In Java, an int is 32 bits. A byte is 8 bits .

Most primitive types in Java are signed, and byte, short, int, and long are encoded in two's complement. (The char type is unsigned, and the concept of a sign is not applicable to boolean.)

In this number scheme the most significant bit specifies the sign of the number. If more bits are needed, the most significant bit ("MSB") is simply copied to the new MSB.

So if you have byte 255: 11111111 and you want to represent it as an int (32 bits) you simply copy the 1 to the left 24 times.

Now, one way to read a negative two's complement number is to start with the least significant bit, move left until you find the first 1, then invert every bit afterwards. The resulting number is the positive version of that number

For example: 11111111 goes to 00000001 = -1. This is what Java will display as the value.

What you probably want to do is know the unsigned value of the byte.

You can accomplish this with a bitmask that deletes everything but the least significant 8 bits. (0xff)

So:

byte signedByte = -1;
int unsignedByte = signedByte & (0xff);

System.out.println("Signed: " + signedByte + " Unsigned: " + unsignedByte);

Would print out: "Signed: -1 Unsigned: 255"

What's actually happening here?

We are using bitwise AND to mask all of the extraneous sign bits (the 1's to the left of the least significant 8 bits.) When an int is converted into a byte, Java chops-off the left-most 24 bits

1111111111111111111111111010101
&
0000000000000000000000001111111
=
0000000000000000000000001010101

Since the 32nd bit is now the sign bit instead of the 8th bit (and we set the sign bit to 0 which is positive), the original 8 bits from the byte are read by Java as a positive value.

168
ответ дан 24 November 2019 в 00:35
поделиться

байт в Java подписан, поэтому он имеет диапазон от -2 ^ 7 до 2 ^ 7-1, то есть от -128 до 127. Поскольку 132 больше 127, вы в конечном итоге перейдете к 132-256 = -124. То есть по существу 256 (2 ^ 8) добавляется или вычитается, пока не попадет в диапазон.

Для получения дополнительной информации вы можете прочитать дополнение до двух .

23
ответ дан 24 November 2019 в 00:35
поделиться

132 is outside the range of a byte which is -128 to 127 (Byte.MIN_VALUE to Byte.MAX_VALUE) Instead the top bit of the 8-bit value is treated as the signed which indicates it is negative in this case. So the number is 132 - 256 = -124.

16
ответ дан 24 November 2019 в 00:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: