• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 301

10 Dec 2024 06:19AM UTC coverage: 85.204% (-2.6%) from 87.841%
301

Pull #78

github

web-flow
Merge b830031f4 into 91f0fdf48
Pull Request #78: feat: new package encoding

105 of 161 new or added lines in 3 files covered. (65.22%)

124 existing lines in 29 files now uncovered.

1169 of 1372 relevant lines covered (85.2%)

434.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/time/zone_offset.mbt
1
// Copyright 2024 International Digital Economy Academy
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
///| Time offset from UTC.
16
struct ZoneOffset {
17
  id : String
18
  seconds : Int
19
  dst : Bool
20
}
21

22
///|
23
let max_offset_hours = 18
24

25
///|
26
let min_offset_hours = -18
27

28
///|
29
let max_offset_seconds : Int = max_offset_hours * seconds_per_hour.to_int()
30

31
///|
32
let min_offset_seconds : Int = min_offset_hours * seconds_per_hour.to_int()
33

34
///| UTC+0 offset.
35
pub let utc_offset : ZoneOffset = ZoneOffset::{
36
  id: "Z",
37
  seconds: 0,
38
  dst: false,
39
}
40

41
///| Creates a offset from hours, minutes and seconds.
42
pub fn ZoneOffset::of(
43
  hours~ : Int = 0,
44
  minutes~ : Int = 0,
45
  seconds~ : Int = 0
46
) -> ZoneOffset!Error {
47
  validate_offset!(hours, minutes, seconds)
18✔
48
  let secs = hours * seconds_per_hour.to_int() +
6✔
49
    minutes * seconds_per_minute.to_int() +
50
    seconds
51
  ZoneOffset::from_seconds!(secs)
6✔
52
}
53

54
///| Creates a offset from seconds.
55
pub fn ZoneOffset::from_seconds(seconds : Int) -> ZoneOffset!Error {
56
  if seconds == 0 {
42✔
57
    return utc_offset
2✔
58
  }
59
  if seconds < min_offset_seconds || seconds > max_offset_seconds {
UNCOV
60
    fail!(invalid_offset_err)
×
61
  }
62
  { id: create_id(seconds), seconds, dst: false }
63
}
64

65
///| Returns the offset id.
66
pub fn to_string(self : ZoneOffset) -> String {
67
  self.id
38✔
68
}
69

70
///|
71
pub impl Show for ZoneOffset with output(self : ZoneOffset, logger : Logger) {
72
  logger.write_string(self.to_string())
14✔
73
}
74

75
///|
76
pub fn op_equal(self : ZoneOffset, other : ZoneOffset) -> Bool {
77
  self.seconds == other.seconds
5✔
78
}
79

80
///|
81
pub fn compare(self : ZoneOffset, other : ZoneOffset) -> Int {
82
  other.seconds - self.seconds
2✔
83
}
84

85
///| Returns the offset id.
86
pub fn id(self : ZoneOffset) -> String {
87
  self.id
1✔
88
}
89

90
///| Returns the total seconds of this offset.
91
pub fn seconds(self : ZoneOffset) -> Int {
92
  self.seconds
10✔
93
}
94

95
///| Checks if this offset is daylight saving time.
96
pub fn is_dst(self : ZoneOffset) -> Bool {
97
  self.dst
1✔
98
}
99

100
///|
101
fn create_id(secs : Int) -> String {
102
  if secs == 0 {
38✔
UNCOV
103
    return "Z"
×
104
  }
105
  let abs_secs = secs.abs()
106
  let offset_hours = abs_secs / seconds_per_hour.to_int()
107
  let offset_mins = abs_secs %
108
    seconds_per_hour.to_int() /
109
    seconds_per_minute.to_int()
110
  let offset_secs = abs_secs % seconds_per_minute.to_int()
111
  let buf = StringBuilder::new(size_hint=9)
112
  buf.write_char(if secs < 0 { '-' } else { '+' })
4✔
113
  buf.write_string(add_prefix_zero(offset_hours.to_string(), 2))
114
  buf.write_char(':')
115
  buf.write_string(add_prefix_zero(offset_mins.to_string(), 2))
116
  if offset_secs > 0 {
117
    buf.write_char(':')
3✔
118
    buf.write_string(add_prefix_zero(offset_secs.to_string(), 2))
119
  }
120
  buf.to_string()
121
}
122

123
///|
124
fn validate_offset(h : Int, m : Int, s : Int) -> Unit!Error {
125
  if h < min_offset_hours || h > max_offset_hours {
18✔
126
    fail!(invalid_offset_err)
×
127
  }
128
  if m < -59 || m > 59 {
129
    fail!(invalid_offset_err)
×
130
  }
131
  if s < -59 || s > 59 {
132
    fail!(invalid_offset_err)
×
133
  }
134
  if (h > 0 && (m < 0 || s < 0)) || (h < 0 && (m > 0 || s > 0)) {
UNCOV
135
    fail!(invalid_offset_err)
×
136
  }
137
  if (m > 0 && s < 0) || (m < 0 && s > 0) {
UNCOV
138
    fail!(invalid_offset_err)
×
139
  }
140
  if h.abs() == 18 && (m != 0 || s != 0) {
UNCOV
141
    fail!(invalid_offset_err)
×
142
  }
143
  ()
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc