Home Reference Source

lib/make/time.js

  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4.  
  5. const make = require('../make')
  6. const random = require('../random')
  7.  
  8. class time extends make {
  9. static unit () {
  10. return random.pick([
  11. 's', 'ms'
  12. ])
  13. }
  14.  
  15. static datetime () {
  16. switch (random.number(2)) {
  17. case 0:
  18. return new Date(new Date().getTime() + random.number())
  19. case 1:
  20. return new Date(new Date().getTime() - random.number())
  21. }
  22. }
  23.  
  24. static date () {
  25. return time.datetime().toDateString()
  26. }
  27.  
  28. static time () {
  29. return time.datetime().toTimeString()
  30. }
  31.  
  32. static iso () {
  33. return time.datetime().toISOString()
  34. }
  35.  
  36. static epoch () {
  37. return Math.floor(time.datetime() / 1000)
  38. }
  39.  
  40. static any () {
  41. return make.number.any() + time.unit()
  42. }
  43. }
  44.  
  45. module.exports = time