mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: add 'use -- instead of -= 1'
This commit is contained in:
parent
6d6b2fdda8
commit
7b18e5d198
@ -1606,6 +1606,13 @@ fn ($v.name mut $v.typ) ${p.cur_fn.name}(...) {
|
|||||||
p.gen(' += ')
|
p.gen(' += ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.minus_assign {
|
||||||
|
next := p.peek_token()
|
||||||
|
if next.tok == .number && next.lit == '1' {
|
||||||
|
p.error('use `--` instead of `-= 1`')
|
||||||
|
}
|
||||||
|
p.gen(' -= ')
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
p.gen(' ' + p.tok.str() + ' ')
|
p.gen(' ' + p.tok.str() + ' ')
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ pub fn unix(abs int) Time {
|
|||||||
mut day_offset := abs / seconds_per_day
|
mut day_offset := abs / seconds_per_day
|
||||||
if abs % seconds_per_day < 0 {
|
if abs % seconds_per_day < 0 {
|
||||||
// Compensate for round towards zero on integers as we want floored instead
|
// Compensate for round towards zero on integers as we want floored instead
|
||||||
day_offset -= 1
|
day_offset--
|
||||||
}
|
}
|
||||||
year,month,day := calculate_date_from_offset(day_offset)
|
year,month,day := calculate_date_from_offset(day_offset)
|
||||||
hr,min,sec := calculate_time_from_offset(abs % seconds_per_day)
|
hr,min,sec := calculate_time_from_offset(abs % seconds_per_day)
|
||||||
@ -26,63 +26,60 @@ pub fn unix(abs int) Time {
|
|||||||
[inline]
|
[inline]
|
||||||
fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
fn calculate_date_from_offset(day_offset_ int) (int,int,int) {
|
||||||
mut day_offset := day_offset_
|
mut day_offset := day_offset_
|
||||||
|
|
||||||
// Move offset to year 2001 as it's the start of a new 400-year cycle
|
// Move offset to year 2001 as it's the start of a new 400-year cycle
|
||||||
// Code below this rely on the fact that the day_offset is lined up with the 400-year cycle
|
// Code below this rely on the fact that the day_offset is lined up with the 400-year cycle
|
||||||
// 1970-2000 (inclusive) has 31 years (8 of which are leap years)
|
// 1970-2000 (inclusive) has 31 years (8 of which are leap years)
|
||||||
mut year := 2001
|
mut year := 2001
|
||||||
day_offset -= 31 * 365 + 8
|
day_offset -= 31 * 365 + 8
|
||||||
|
|
||||||
// Account for 400 year cycle
|
// Account for 400 year cycle
|
||||||
year += (day_offset / days_per_400_years) * 400
|
year += (day_offset / days_per_400_years) * 400
|
||||||
day_offset %= days_per_400_years
|
day_offset %= days_per_400_years
|
||||||
|
|
||||||
// Account for 100 year cycle
|
// Account for 100 year cycle
|
||||||
if day_offset == days_per_100_years * 4 {
|
if day_offset == days_per_100_years * 4 {
|
||||||
year += 300
|
year += 300
|
||||||
day_offset -= days_per_100_years * 3
|
day_offset -= days_per_100_years * 3
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
year += (day_offset / days_per_100_years) * 100
|
year += (day_offset / days_per_100_years) * 100
|
||||||
day_offset %= days_per_100_years
|
day_offset %= days_per_100_years
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account for 4 year cycle
|
// Account for 4 year cycle
|
||||||
if day_offset == days_per_4_years * 25 {
|
if day_offset == days_per_4_years * 25 {
|
||||||
year += 96
|
year += 96
|
||||||
day_offset -= days_per_4_years * 24
|
day_offset -= days_per_4_years * 24
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
year += (day_offset / days_per_4_years) * 4
|
year += (day_offset / days_per_4_years) * 4
|
||||||
day_offset %= days_per_4_years
|
day_offset %= days_per_4_years
|
||||||
}
|
}
|
||||||
|
|
||||||
// Account for every year
|
// Account for every year
|
||||||
if day_offset == 365 * 4 {
|
if day_offset == 365 * 4 {
|
||||||
year += 3
|
year += 3
|
||||||
day_offset -= 365 * 3
|
day_offset -= 365 * 3
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
year += (day_offset / 365)
|
year += (day_offset / 365)
|
||||||
day_offset %= 365
|
day_offset %= 365
|
||||||
}
|
}
|
||||||
|
|
||||||
if day_offset < 0 {
|
if day_offset < 0 {
|
||||||
year -= 1
|
year--
|
||||||
if is_leap_year(year) {
|
if is_leap_year(year) {
|
||||||
day_offset += 366
|
day_offset += 366
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
day_offset += 365
|
day_offset += 365
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_leap_year(year) {
|
if is_leap_year(year) {
|
||||||
if day_offset > 31 + 29 - 1 {
|
if day_offset > 31 + 29 - 1 {
|
||||||
// After leap day; pretend it wasn't there.
|
// After leap day; pretend it wasn't there.
|
||||||
day_offset--
|
day_offset--
|
||||||
} else if day_offset == 31 + 29 - 1 {
|
}
|
||||||
|
else if day_offset == 31 + 29 - 1 {
|
||||||
// Leap day.
|
// Leap day.
|
||||||
return year,2,29
|
return year,2,29
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mut estimated_month := day_offset / 31
|
mut estimated_month := day_offset / 31
|
||||||
for day_offset >= days_before[estimated_month + 1] {
|
for day_offset >= days_before[estimated_month + 1] {
|
||||||
estimated_month++
|
estimated_month++
|
||||||
@ -93,9 +90,7 @@ fn calculate_date_from_offset(day_offset_ int) (int, int, int) {
|
|||||||
}
|
}
|
||||||
estimated_month--
|
estimated_month--
|
||||||
}
|
}
|
||||||
|
|
||||||
day_offset -= days_before[estimated_month]
|
day_offset -= days_before[estimated_month]
|
||||||
|
|
||||||
return year,estimated_month + 1,day_offset + 1
|
return year,estimated_month + 1,day_offset + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,12 +100,9 @@ fn calculate_time_from_offset(second_offset_ int) (int, int, int) {
|
|||||||
if second_offset < 0 {
|
if second_offset < 0 {
|
||||||
second_offset += seconds_per_day
|
second_offset += seconds_per_day
|
||||||
}
|
}
|
||||||
|
|
||||||
hour := second_offset / seconds_per_hour
|
hour := second_offset / seconds_per_hour
|
||||||
second_offset %= seconds_per_hour
|
second_offset %= seconds_per_hour
|
||||||
|
|
||||||
min := second_offset / seconds_per_minute
|
min := second_offset / seconds_per_minute
|
||||||
second_offset %= seconds_per_minute
|
second_offset %= seconds_per_minute
|
||||||
|
|
||||||
return hour,min,second_offset
|
return hour,min,second_offset
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user