1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

time: add new method year_day and the tests for it (#18107)

This commit is contained in:
acimnotes 2023-05-12 13:27:20 +07:00 committed by GitHub
parent 447b45ca8c
commit 790afbce94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -267,6 +267,16 @@ pub fn (t Time) day_of_week() int {
return day_of_week(t.year, t.month, t.day)
}
// year_day returns the current day of the year as an integer.
// See also #Time.custom_format .
pub fn (t Time) year_day() int {
yday := t.day + time.days_before[t.month - 1]
if is_leap_year(t.year) && t.month > 2 {
return yday + 1
}
return yday
}
// weekday_str returns the current day as a string 3 letter abbreviation.
pub fn (t Time) weekday_str() string {
i := t.day_of_week() - 1

View File

@ -142,6 +142,25 @@ fn test_day_of_week() {
}
}
fn test_year_day() {
// testing if December 31st in a leap year is numbered as 366
assert time.parse('2024-12-31 20:00:00')!.year_day() == 366
// testing December 31st's number in a non leap year
assert time.parse('2025-12-31 20:00:00')!.year_day() == 365
assert time.parse('2024-02-28 20:00:00')!.year_day() == 59
assert time.parse('2024-02-29 20:00:00')!.year_day() == 60
assert time.parse('2024-03-01 20:00:00')!.year_day() == 61
assert time.parse('2024-03-02 20:00:00')!.year_day() == 62
assert time.parse('2025-02-28 20:00:00')!.year_day() == 59
assert time.parse('2025-03-01 20:00:00')!.year_day() == 60
assert time.parse('2024-01-01 20:00:00')!.year_day() == 1
assert time.parse('2025-01-01 20:00:00')!.year_day() == 1
}
fn test_weekday_str() {
day_names := ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for i, name in day_names {