all files / src/timePicker/ timeUtils.js

0% Statements 0/65
0% Branches 0/50
0% Functions 0/9
0% Lines 0/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                                                                       
/**
 * material-ui timeUtils.js
 * https://github.com/callemall/material-ui/blob/master/src/TimePicker/timeUtils.js
 */
export function addHours (d, hours) {
  const newDate = clone(d)
  newDate.setHours(d.getHours() + hours)
  return newDate
}
 
export function addMinutes (d, minutes) {
  const newDate = clone(d)
  newDate.setMinutes(d.getMinutes() + minutes)
  return newDate
}
 
export function addSeconds (d, seconds) {
  const newDate = clone(d)
  newDate.setSeconds(d.getMinutes() + seconds)
  return newDate
}
 
function clone (d) {
  return new Date(d.getTime())
}
 
/**
 * @param date [Date] A Date object.
 * @param format [String] One of 'ampm', '24hr', defaults to 'ampm'.
 * @param pedantic [Boolean] Check time-picker/time-picker.jsx file.
 *
 * @return String A string representing the formatted time.
 */
export function formatTime (date, format = 'ampm', pedantic = false) {
  if (!date) return ''
  let hours = date.getHours()
  let mins = date.getMinutes().toString()
 
  if (format === 'ampm') {
    const isAM = hours < 12
    hours = hours % 12
    const additional = isAM ? ' am' : ' pm'
    hours = (hours || 12).toString()
 
    if (mins.length < 2) mins = `0${mins}`
 
    if (pedantic) {
      // Treat midday/midnight specially http://www.nist.gov/pml/div688/times.cfm
      if (hours === '12' && mins === '00') {
        return additional === ' pm' ? '12 noon' : '12 midnight'
      }
    }
 
    return hours + (mins === '00' ? '' : `:${mins}`) + additional
  }
 
  hours = hours.toString()
 
  if (hours.length < 2) hours = `0${hours}`
  if (mins.length < 2) mins = `0${mins}`
 
  return `${hours}:${mins}`
}
 
export function strToTime (str, format = 'ampm', pedantic = false) {
  const date = new Date()
  if (!str) return date
  let mtype = ''
  let index = -1
  if (format === 'ampm') {
    index = str.indexOf('am')
    if (index === -1) index = str.indexOf('midnight')
    if (index !== -1) {
      mtype = 'am'
    } else {
      mtype = 'pm'
      index = str.indexOf('pm')
      if (index === -1) index = str.indexOf('noon')
    }
  }
 
  if (index !== -1) str = str.substring(0, index).trim()
 
  const times = str.split(':')
  let hour = Number(times[0].trim())
  if (mtype === 'pm') {
    hour += 12
  }
  if (hour >= 24) hour = 0
  let minute = times.length > 1 ? Number(times[1]) : 0
  date.setMinutes(minute)
  date.setHours(hour)
  return date
}
 
export function rad2deg (rad) {
  return rad * 57.29577951308232
}
 
export function getTouchEventOffsetValues (event) {
  const el = event.target
  const boundingRect = el.getBoundingClientRect()
 
  return {
    offsetX: event.clientX - boundingRect.left,
    offsetY: event.clientY - boundingRect.top
  }
}
 
export function isInner (props) {
  if (props.type !== 'hour') {
    return false
  }
  return props.value < 1 || props.value > 12
}