Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. 'use strict'
  2. /*@flow*/
  3.  
  4. // Creates a function that clones dates and shifts by the
  5. // seconds specified.
  6. // Example:
  7. // >>> import { seconds } from './shift'
  8. // >>> const fiveSecondsLater = seconds(5)
  9. // >>> const d = new Date()
  10. // >>> fiveSecondsLater(d) - d
  11. // 5000
  12. export const seconds
  13. : (sec:number) => (date:Date) => Date
  14. = sec => date => {
  15. const shifted = new Date(date) // clone
  16. shifted.setSeconds(shifted.getSeconds() + sec)
  17. return shifted
  18. };
  19.  
  20. // Creates a function that clones dates and shifts them by the
  21. // minutes specified.
  22. // Example:
  23. // >>> import { minutes } from './shift'
  24. // >>> const minutesWindow = (lo, hi) => date =>
  25. // >>> [minutes(lo), minutes(hi)].map(f => f(date))
  26. // >>> const within2MinWindow = minutesWindow(-1, 1)
  27. // >>> const d = new Date('2016-07-01T18:00:00Z')
  28. // >>> within2MinWindow(d)
  29. // [ 2016-07-01T17:59:00.000Z, 2016-07-01T18:01:00.000Z ]
  30. export const minutes
  31. : (min:number) => (date:Date) => Date
  32. = min =>
  33. seconds(min * 60)
  34.  
  35. // Creates a function that clones dates and shifts them by the
  36. // hours specified.
  37. // Example:
  38. // >>> import { hours } from './shift'
  39. // >>> const halfDaySooner = hours(-12)
  40. // >>> const d = new Date()
  41. // >>> halfDaySooner(d) - d
  42. // -43200000
  43. export const hours
  44. : (hr:number) => (date:Date) => Date
  45. = hr =>
  46. seconds(hr * 3600)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement