// @flow

const TIME_FORMAT = 'h:mma'
import {timezone} from '@frogpond/constants'
import moment from 'moment-timezone'

export const parseTime = (now: moment) => (
	time: string | false,
): null | moment => {
	// either pass `false` through or return a parsed time
	if (time === false) {
		return null
	}

	// interpret in Central time
	let m = moment.tz(time, TIME_FORMAT, true, timezone())

	// and set the date to today
	m.dayOfYear(now.dayOfYear())

	let sixMonthsAgo = moment(now).subtract(6, 'months')

	if (m.isBefore(sixMonthsAgo)) {
		m.add(1, 'year')
	}

	return m
}

// @flow

const TIME_FORMAT = 'h:mma'
import {timezone} from '@frogpond/constants'
import moment from 'moment-timezone'

// TODO: Do "dayOfYear" handling better so that we don't need to handle wrapping at
// the 6 month mark. (See #3375 for why this function changed.)
export const parseTime = (now: moment) => (
	time: string | false,
): null | moment => {
	// either pass `false` through or return a parsed time
	if (time === false) {
		return null
	}

	// interpret in Central time
	let m = moment.tz(time, TIME_FORMAT, true, timezone())

	// and set the date to today
	m.dayOfYear(now.dayOfYear())

	let sixMonthsAgo = moment(now).subtract(6, 'months')

	if (m.isBefore(sixMonthsAgo)) {
		m.add(1, 'year')
	}

	return m
}

Duplicate commentary on dayOfYear handling
parseTime: Duplicate commentary on dayOfYear handling

Suggested-by: Hawken Rives <172aa8a882025f3e2d2bd621702ac9e557631cef@fastmail.fm>
Signed-off-by: Kristofer Rye <1ed31cfd0b53bc3d1689a6fee6dbfc9507dffd22@gmail.com>
