Skip to content

Commit

Permalink
Fix currentDate time zone issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ka7eh committed Feb 15, 2021
1 parent 20ec145 commit 5c32168
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 33 deletions.
28 changes: 14 additions & 14 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import {isInDowntime} from '../src/check'
import {isInDowntime, getUTCAdjustments} from '../src/check'

describe('isInDowntime should', () => {
const currentDate = new Date(Date.UTC(2021, 1, 13, 12, 53))
const tz = 3.5
const utcAdjustments = getUTCAdjustments(3.5)

test('return false when no downtime is provided', () => {
expect(isInDowntime(currentDate, tz)).toBe(false)
expect(isInDowntime(currentDate, utcAdjustments)).toBe(false)
})

test('throw error when downtime does not match pattern', () => {
expect(() => isInDowntime(currentDate, tz, '6:00-7:00')).toThrow(
'Invalid downtime'
)
expect(() =>
isInDowntime(currentDate, utcAdjustments, '6:00-7:00')
).toThrow('Invalid downtime')
})

test('throw error when downtime is not valid time', () => {
expect(() => isInDowntime(currentDate, tz, '17:00-24:00')).toThrow(
'Invalid downtime'
)
expect(() =>
isInDowntime(currentDate, utcAdjustments, '17:00-24:00')
).toThrow('Invalid downtime')
})

test('throw error when downtime start time is after end time', () => {
expect(() => isInDowntime(currentDate, tz, '16:30-07:00')).toThrow(
'Invalid downtime'
)
expect(() =>
isInDowntime(currentDate, utcAdjustments, '16:30-07:00')
).toThrow('Invalid downtime')
})

test('return true when in downtime', () => {
expect(isInDowntime(currentDate, tz, '16:00-23:59')).toBe(true)
expect(isInDowntime(currentDate, utcAdjustments, '16:00-23:59')).toBe(true)
})

test('return false when not in downtime', () => {
expect(isInDowntime(currentDate, tz, '16:30-23:59')).toBe(false)
expect(isInDowntime(currentDate, utcAdjustments, '16:30-23:59')).toBe(false)
})
})
24 changes: 17 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "no-weekend-merge",
"version": "0.1.0",
"version": "0.1.1",
"description": "An action to prevent merges on weekends, or whenever that a merge should not happend",
"main": "lib/main.js",
"scripts": {
Expand Down
27 changes: 19 additions & 8 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
interface UTCAdjustments {
hours: number
minutes: number
}

interface DowntimePattern {
[k: string]: string
fromHour: string
Expand All @@ -6,9 +11,18 @@ interface DowntimePattern {
toMinute: string
}

export const getUTCAdjustments = (tz: number): UTCAdjustments => {
const hours = parseInt(tz.toString(), 10)
const minutes = (tz % hours) * 60
return {
hours,
minutes
}
}

export const isInDowntime = (
date: Date,
tz: number,
utcAdjustments: UTCAdjustments,
downtime?: string
): boolean => {
if (!downtime) {
Expand Down Expand Up @@ -52,23 +66,20 @@ export const isInDowntime = (
throw new Error('Invalid downtime')
}

const hourAdjustment = parseInt(tz.toString(), 10)
const minuteAdjustment = (tz % hourAdjustment) * 60

const start = Date.UTC(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
fromHour - hourAdjustment,
fromMinute - minuteAdjustment
fromHour - utcAdjustments.hours,
fromMinute - utcAdjustments.minutes
)

const end = Date.UTC(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
toHour - hourAdjustment,
toMinute - minuteAdjustment
toHour - utcAdjustments.hours,
toMinute - utcAdjustments.minutes
)

return date.getTime() >= start && date.getTime() <= end
Expand Down
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import {isInDowntime} from './check'
import {isInDowntime, getUTCAdjustments} from './check'

const days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']

Expand All @@ -8,13 +8,20 @@ async function run(): Promise<void> {

try {
const tz = parseFloat(core.getInput('tz'))
const utcAdjustments = getUTCAdjustments(tz)

currentDate.setUTCHours(currentDate.getUTCHours() + utcAdjustments.hours)
currentDate.setUTCMinutes(
currentDate.getUTCMinutes() + utcAdjustments.minutes
)

core.debug(`TZ: ${tz} - Day: ${currentDate.getUTCDay()}`)

const downtimes = core.getInput(days[currentDate.getUTCDay()]) || ''
core.debug(`Downtimes: ${downtimes}`)

for (const downtime of downtimes.split(',')) {
if (isInDowntime(currentDate, tz, downtime)) {
if (isInDowntime(currentDate, utcAdjustments, downtime)) {
core.setFailed(
`The PR cannot be merged at this time (${currentDate}) with the current settings (${downtime}).`
)
Expand Down

0 comments on commit 5c32168

Please sign in to comment.