forked from wmnsk/go-pfcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
end-time.go
45 lines (39 loc) · 1.12 KB
/
end-time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2019-2020 go-pfcp authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package ie
import (
"encoding/binary"
"io"
"time"
)
// NewEndTime creates a new EndTime IE.
func NewEndTime(ts time.Time) *IE {
u64sec := uint64(ts.Sub(time.Date(1900, time.January, 1, 0, 0, 0, 0, time.UTC))) / 1000000000
return newUint32ValIE(EndTime, uint32(u64sec))
}
// EndTime returns EndTime in time.Time if the type of IE matches.
func (i *IE) EndTime() (time.Time, error) {
if len(i.Payload) < 4 {
return time.Time{}, io.ErrUnexpectedEOF
}
switch i.Type {
case EndTime:
return time.Unix(int64(binary.BigEndian.Uint32(i.Payload[0:4])-2208988800), 0), nil
case UsageReportWithinSessionModificationResponse,
UsageReportWithinSessionDeletionResponse,
UsageReportWithinSessionReportRequest:
ies, err := i.UsageReport()
if err != nil {
return time.Time{}, err
}
for _, x := range ies {
if x.Type == EndTime {
return x.EndTime()
}
}
return time.Time{}, ErrIENotFound
default:
return time.Time{}, &InvalidTypeError{Type: i.Type}
}
}