xlab / at

AT is a framework written in Go for communication with AT-compatible devices like Huawei modems via serial port.
MIT License
120 stars 53 forks source link

Incorrect calculation of timezone offset sign #11

Closed oxxxtor closed 2 years ago

oxxxtor commented 5 years ago

func (t *Timestamp) ReadFrom(octets []byte) in sms.go calculate timezone offset as follow:

diff := time.Duration(blocks[6]) * 15 * time.Minute
if blocks[6]>>3&0x01 == 1 { // bit 3 = GMT offset sgn
    // was negative, so make UTC
    date = date.Add(diff)
} else {
    // was positive, so make UTC
    date = date.Add(-diff)
}

In accordance with GSM 03.**: The Time Zone indicates the difference, expressed in quarters of an hour, between the local time and GMT. In the first of the two semi-octets, the first bit (bit 3 of the seventh octet of the TP-Service CentreTime-Stamp ield) represents the algebraic sign of this difference (0 : positive, 1 : negative)

But semi-octets was swapped and recoded to decimal with DecodeSemi: for example, zone +03:00 trasmitted like 0x21, but after DecodeSemi looks like 12, or zone -03:00 trasmitted like 0x29, but after DecodeSemi looks like 92,

I.e. negative offset after DecodeSemi will greater than 80 (bit 3 in seventh octet is 0x08 )

I propose to replace the code with something like follow:

if blocks[6] >= 80 {
    // was negative, so make UTC
    diff := time.Duration(blocks[6]-80) * 15 * time.Minute
    date = date.Add(diff)
} else {
    // was positive, so make UTC
    diff := time.Duration(blocks[6]) * 15 * time.Minute
    date = date.Add(-diff)
}

Also func (t Timestamp) PDU() needs to be modified

xlab commented 5 years ago

@oxxxtor thank you for this contribution! If you could make a PR, I'd appreciate it very much.

oxxxtor commented 5 years ago

@xlab ok, I will try to do it a little later

dmke commented 2 years ago

@xlab, I believe this can be closed.