Skip to content

Commit

Permalink
add range check for getYear(), getHour(), getMinute(), getSecond()
Browse files Browse the repository at this point in the history
  • Loading branch information
shaoziyang committed Nov 23, 2019
1 parent 3cdd29b commit 644351b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions ds1307.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace DS1307 {
//% weight=99 blockGap=8
//% parts=DS1307 trackArgs=0
export function getYear(): number {
return (HexToDec(getReg(DS1307_REG_YEAR)) + 2000)
return Math.min(HexToDec(getReg(DS1307_REG_YEAR)), 99) + 2000
}

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ namespace DS1307 {
//% weight=95 blockGap=8
//% parts=DS1307 trackArgs=0
export function getHour(): number {
return HexToDec(getReg(DS1307_REG_HOUR)) % 24
return Math.min(HexToDec(getReg(DS1307_REG_HOUR)), 23)
}

/**
Expand All @@ -190,7 +190,7 @@ namespace DS1307 {
//% weight=94 blockGap=8
//% parts=DS1307 trackArgs=0
export function getMinute(): number {
return HexToDec(getReg(DS1307_REG_MINUTE)) % 60
return Math.min(HexToDec(getReg(DS1307_REG_MINUTE)), 59)
}

/**
Expand All @@ -212,7 +212,7 @@ namespace DS1307 {
//% weight=93 blockGap=8
//% parts=DS1307 trackArgs=0
export function getSecond(): number {
return HexToDec(getReg(DS1307_REG_SECOND)) % 60
return Math.min(HexToDec(getReg(DS1307_REG_SECOND)), 59)
}

/**
Expand Down

0 comments on commit 644351b

Please sign in to comment.