• 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

97.96
/time/zoned_date_time.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
///| A datetime with a time zone and offset in the ISO 8601 calendar system.
16
struct ZonedDateTime {
17
  datetime : PlainDateTime
18
  zone : Zone
19
  offset : ZoneOffset
20
}
21

22
///| Creates a ZonedDateTime from year, month, day, hour, minute, second and a time zone.
23
/// The default time zone is UTC+0.
24
pub fn date_time(
25
  year : Int,
26
  month : Int,
27
  day : Int,
28
  hour~ : Int = 0,
29
  minute~ : Int = 0,
30
  second~ : Int = 0,
31
  zone~ : Zone = utc_zone
32
) -> ZonedDateTime!Error {
33
  ZonedDateTime::of!(year, month, day, hour~, minute~, second~, zone~)
1✔
34
}
35

36
///| Creates a ZonedDateTime from elapsed seconds since the unix epoch and a time zone.
37
/// The default time zone is UTC+0.
38
pub fn unix(
39
  second : Int64,
40
  nanosecond~ : Int = 0,
41
  zone~ : Zone = utc_zone
42
) -> ZonedDateTime!Error {
43
  ZonedDateTime::from_unix_second!(second, nanosecond~, zone~)
1✔
44
}
45

46
///| Creates a ZonedDateTime from year, month, day, hour, minute and second.
47
/// The default time zone is UTC+0.
48
pub fn ZonedDateTime::of(
49
  year : Int,
50
  month : Int,
51
  day : Int,
52
  hour~ : Int = 0,
53
  minute~ : Int = 0,
54
  second~ : Int = 0,
55
  zone~ : Zone = utc_zone
56
) -> ZonedDateTime!Error {
57
  let datetime = PlainDateTime::of!(year, month, day, hour~, minute~, second~)
23✔
58
  create_from_plain(datetime, zone)
23✔
59
}
60

61
///| Creates a ZonedDateTime from a PlainDateTime and a time zone.
62
/// The default time zone is UTC+0.
63
pub fn ZonedDateTime::from_plain_datetime(
64
  datetime : PlainDateTime,
65
  zone~ : Zone = utc_zone
66
) -> ZonedDateTime {
67
  create_from_plain(datetime, zone)
1✔
68
}
69

70
///| Creates a ZonedDateTime from elapsed seconds since the unix epoch and a time zone.
71
/// The default time zone is UTC+0.
72
pub fn ZonedDateTime::from_unix_second(
73
  second : Int64,
74
  nanosecond~ : Int = 0,
75
  zone~ : Zone = utc_zone
76
) -> ZonedDateTime!Error {
77
  let offset = zone.lookup_offset(second)
4✔
78
  let datetime = PlainDateTime::from_unix_second!(second, nanosecond, offset)
79
  { datetime, zone, offset }
4✔
80
}
81

82
///| Returns a string representing this datetime, like "2008-08-08T20:00:00+8:00[Asia/Beijing]"
83
pub fn to_string(self : ZonedDateTime) -> String {
84
  let buf = StringBuilder::new(size_hint=0)
24✔
85
  buf.write_string(self.datetime.to_string())
86
  buf.write_string(self.offset.to_string())
87
  if self.zone != utc_zone {
88
    buf.write_char('[')
21✔
89
    buf.write_string(self.zone.to_string())
90
    buf.write_char(']')
91
  }
92
  buf.to_string()
93
}
94

95
///|
96
pub impl Show for ZonedDateTime with output(
97
  self : ZonedDateTime,
98
  logger : Logger
99
) -> Unit {
100
  logger.write_string(self.to_string())
24✔
101
}
102

103
///| Returns the elapsed seconds since the unix epoch.
104
pub fn to_unix_second(self : ZonedDateTime) -> Int64 {
105
  self.datetime.to_unix_second() - self.offset.seconds().to_int64()
3✔
106
}
107

108
///| Returns the date part of this datetime, without timezone.
109
pub fn to_plain_date(self : ZonedDateTime) -> PlainDate {
110
  self.datetime.to_plain_date()
1✔
111
}
112

113
///| Returns the time part of this datetime, without timezone.
114
pub fn to_plain_time(self : ZonedDateTime) -> PlainTime {
115
  self.datetime.to_plain_time()
1✔
116
}
117

118
///| Returns the datetime part of this datetime, without timezone.
119
pub fn to_plain_date_time(self : ZonedDateTime) -> PlainDateTime {
120
  self.datetime
1✔
121
}
122

123
///| Returns the era of this datetime.
124
pub fn era(self : ZonedDateTime) -> String {
125
  self.datetime.era()
1✔
126
}
127

128
///| Returns the year of era of this datetime.
129
pub fn era_year(self : ZonedDateTime) -> Int {
130
  self.datetime.era_year()
1✔
131
}
132

133
///| Returns the year of this datetime.
134
pub fn year(self : ZonedDateTime) -> Int {
UNCOV
135
  self.datetime.year()
×
136
}
137

138
///| Returns the month of this datetime.
139
pub fn month(self : ZonedDateTime) -> Int {
140
  self.datetime.month()
1✔
141
}
142

143
///| Returns the day of month of this datetime.
144
pub fn day(self : ZonedDateTime) -> Int {
145
  self.datetime.day()
1✔
146
}
147

148
///| Returns the weekday of this datetime.
149
pub fn weekday(self : ZonedDateTime) -> Weekday {
150
  self.datetime.weekday()
1✔
151
}
152

153
///| Returns the ordinal day of year of this datetime.
154
pub fn ordinal(self : ZonedDateTime) -> Int {
155
  self.datetime.ordinal()
1✔
156
}
157

158
///| Returns the number of days in a month of this datetime.
159
pub fn days_in_week(self : ZonedDateTime) -> Int {
160
  self.datetime.days_in_week()
1✔
161
}
162

163
///| Returns the number of days in a month of this datetime.
164
pub fn days_in_month(self : ZonedDateTime) -> Int {
165
  self.datetime.days_in_month()
1✔
166
}
167

168
///| Returns the number of days in a year of this datetime.
169
pub fn days_in_year(self : ZonedDateTime) -> Int {
170
  self.datetime.days_in_year()
1✔
171
}
172

173
///| Returns the number of months in a year of this datetime.
174
pub fn months_in_year(self : ZonedDateTime) -> Int {
175
  self.datetime.months_in_year()
1✔
176
}
177

178
///| Checks if this datetime is in a leap year.
179
pub fn in_leap_year(self : ZonedDateTime) -> Bool {
180
  self.datetime.in_leap_year()
1✔
181
}
182

183
///| Returns the hour of this datetime.
184
pub fn hour(self : ZonedDateTime) -> Int {
185
  self.datetime.hour()
1✔
186
}
187

188
///| Returns the minute of this datetime.
189
pub fn minute(self : ZonedDateTime) -> Int {
190
  self.datetime.minute()
1✔
191
}
192

193
///| Returns the second of this datetime.
194
pub fn second(self : ZonedDateTime) -> Int {
195
  self.datetime.second()
1✔
196
}
197

198
///| Returns the nanosecond of this datetime.
199
pub fn nanosecond(self : ZonedDateTime) -> Int {
200
  self.datetime.nanosecond()
1✔
201
}
202

203
///| Returns the time zone of this datetime.
204
pub fn zone(self : ZonedDateTime) -> Zone {
205
  self.zone
1✔
206
}
207

208
///| Returns the time offset of this datetime.
209
pub fn offset(self : ZonedDateTime) -> ZoneOffset {
210
  self.offset
1✔
211
}
212

213
///| Adds specified years to this datetime, and returns a new datetime.
214
pub fn add_years(self : ZonedDateTime, years : Int64) -> ZonedDateTime!Error {
215
  create_from_plain(self.datetime.add_years!(years), self.zone)
1✔
216
}
217

218
///| Adds specified months to this datetime, and returns a new datetime.
219
pub fn add_months(self : ZonedDateTime, months : Int64) -> ZonedDateTime!Error {
220
  create_from_plain(self.datetime.add_months!(months), self.zone)
1✔
221
}
222

223
///| Adds specified weeks to this datetime, and returns a new datetime.
224
pub fn add_weeks(self : ZonedDateTime, weeks : Int64) -> ZonedDateTime!Error {
225
  create_from_plain(self.datetime.add_weeks!(weeks), self.zone)
1✔
226
}
227

228
///| Adds specified days to this datetime, and returns a new datetime.
229
pub fn add_days(self : ZonedDateTime, days : Int64) -> ZonedDateTime!Error {
230
  create_from_plain(self.datetime.add_days!(days), self.zone)
1✔
231
}
232

233
///| Adds specified hours to this datetime, and returns a new datetime.
234
pub fn add_hours(self : ZonedDateTime, hours : Int64) -> ZonedDateTime!Error {
235
  create_from_plain(self.datetime.add_hours!(hours), self.zone)
1✔
236
}
237

238
///| Adds specified minutes to this datetime, and returns a new datetime.
239
pub fn add_minutes(
240
  self : ZonedDateTime,
241
  minutes : Int64
242
) -> ZonedDateTime!Error {
243
  create_from_plain(self.datetime.add_minutes!(minutes), self.zone)
1✔
244
}
245

246
///| Adds specified seconds to this datetime, and returns a new datetime.
247
pub fn add_seconds(
248
  self : ZonedDateTime,
249
  seconds : Int64
250
) -> ZonedDateTime!Error {
251
  create_from_plain(self.datetime.add_seconds!(seconds), self.zone)
1✔
252
}
253

254
///| Adds specified nanoseconds to this datetime, and returns a new datetime.
255
pub fn add_nanoseconds(
256
  self : ZonedDateTime,
257
  nanoseconds : Int64
258
) -> ZonedDateTime!Error {
259
  create_from_plain(self.datetime.add_nanoseconds!(nanoseconds), self.zone)
1✔
260
}
261

262
///| Returns a new datetime with the specified year.
263
pub fn with_year(self : ZonedDateTime, year : Int) -> ZonedDateTime!Error {
264
  create_from_plain(self.datetime.with_year!(year), self.zone)
1✔
265
}
266

267
///| Returns a new datetime with the specified month.
268
pub fn with_month(self : ZonedDateTime, month : Int) -> ZonedDateTime!Error {
269
  create_from_plain(self.datetime.with_month!(month), self.zone)
1✔
270
}
271

272
///| Returns a new datetime with the specified day of the month.
273
pub fn with_day(self : ZonedDateTime, day : Int) -> ZonedDateTime!Error {
274
  create_from_plain(self.datetime.with_day!(day), self.zone)
1✔
275
}
276

277
///| Returns a new datetime with the specified ordinal day of the year.
278
pub fn with_ordinal(self : ZonedDateTime, ordinal : Int) -> ZonedDateTime!Error {
279
  create_from_plain(self.datetime.with_ordinal!(ordinal), self.zone)
1✔
280
}
281

282
///| Returns a new datetime with the specified hour.
283
pub fn with_hour(self : ZonedDateTime, hour : Int) -> ZonedDateTime!Error {
284
  create_from_plain(self.datetime.with_hour!(hour), self.zone)
1✔
285
}
286

287
///| Returns a new datetime with the specified minute.
288
pub fn with_minute(self : ZonedDateTime, minute : Int) -> ZonedDateTime!Error {
289
  create_from_plain(self.datetime.with_minute!(minute), self.zone)
1✔
290
}
291

292
///| Returns a new datetime with the specified second.
293
pub fn with_second(self : ZonedDateTime, second : Int) -> ZonedDateTime!Error {
294
  create_from_plain(self.datetime.with_second!(second), self.zone)
1✔
295
}
296

297
///| Returns a new datetime with the specified nanosecond.
298
pub fn with_nanosecond(
299
  self : ZonedDateTime,
300
  nanosecond : Int
301
) -> ZonedDateTime!Error {
302
  create_from_plain(self.datetime.with_nanosecond!(nanosecond), self.zone)
1✔
303
}
304

305
///|
306
fn create_from_plain(datetime : PlainDateTime, zone : Zone) -> ZonedDateTime {
307
  let offset = zone.lookup_offset(datetime.to_unix_second())
40✔
308
  { datetime, zone, offset }
309
}
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