Guest User

formatTime() but with comments

a guest
Aug 31st, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function formatTime(time: number, format: string): string {
  2.   // create 400 element array with year lengths
  3.   var yearLengthArray : number[] = [...Array(400).keys()].map(i =>
  4.     i % 4 ?
  5.       365 :
  6.       i % 100 ?
  7.         366 :
  8.         i % 400 ?
  9.           365 :
  10.           366
  11.   ),
  12.     yearTimeStamp : number = time,
  13.     year : number = 1970,
  14.     yearLengthIndex : number = 370,
  15.     month : number = 0,
  16.     monthTimeStamp : number
  17.  
  18.   while (yearTimeStamp >= 0) // loop through years and subtract the correct time
  19.     monthTimeStamp = yearTimeStamp, // reusing monthTimeStamp here because it works somehow
  20.     yearTimeStamp -= yearLengthArray[yearLengthIndex] * 86400,
  21.     year++,
  22.     yearLengthIndex=(yearLengthIndex + 1) % 400
  23.  
  24.   while (monthTimeStamp >= 0) // loop through months and subtract the correct time
  25.     yearTimeStamp = monthTimeStamp, // yearTimeStamp is now unused so we can reuse it here
  26.     monthTimeStamp -= [
  27.       [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
  28.       [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  29.     ]
  30.       [yearLengthArray[yearLengthIndex] - 365] // get "if leap year" by subtracting 365 from length
  31.       [month] * 86400,
  32.     month++
  33.  
  34.   format = format.replace('\\', '\\\\') // escape any \'s from the format
  35.  
  36.   Object.entries({ // compute values for each format option as well as hold regexp's for selecting any unescaped format option
  37.     d: [
  38.       `0${Math.floor(1 + yearTimeStamp / 86400)}`.slice(-2),
  39.       /((?:[^\\]|^)(?:\\\\)*(?!\\))d/g
  40.     ],
  41.     A: [
  42.       [
  43.         "Thurs", "Fri", "Satur", "Sun", "Mon", "Tues", "Wednes"
  44.       ][Math.floor(time / 86400) % 7] + "day",
  45.       /((?:[^\\]|^)(?:\\\\)*(?!\\))A/g
  46.     ],
  47.     B: [
  48.       [
  49.         "January", "February", "March", "April", "May", "June",
  50.         "July", "August", "September", "October", "November", "December"
  51.       ][month - 1],
  52.       /((?:[^\\]|^)(?:\\\\)*(?!\\))B/g
  53.     ],
  54.     Y: [
  55.       (year - 1).toString(),
  56.       /((?:[^\\]|^)(?:\\\\)*(?!\\))Y/g
  57.     ],
  58.     H: [
  59.       `0${Math.floor(time / 3600) % 24}`.slice(-2),
  60.       /((?:[^\\]|^)(?:\\\\)*(?!\\))H/g
  61.     ],
  62.     h: [
  63.       `0${Math.floor(time / 3600) % 12}`.slice(-2),
  64.       /((?:[^\\]|^)(?:\\\\)*(?!\\))h/g
  65.     ],
  66.     M: [
  67.       `0${Math.floor(time / 60) % 60}`.slice(-2),
  68.       /((?:[^\\]|^)(?:\\\\)*(?!\\))M/g
  69.     ],
  70.     S: [
  71.       `0${time % 60}`.slice(-2),
  72.       /((?:[^\\]|^)(?:\\\\)*(?!\\))S/g
  73.     ],
  74.     p: [
  75.       (Math.floor(time / 43200) % 2)?"pm":"am",
  76.       /((?:[^\\]|^)(?:\\\\)*(?!\\))p/g
  77.     ]
  78.   }).forEach((i)=>
  79.     format = format.replace(
  80.       i[1][1], // selection regexp
  81.       // escape every character of result as to prevent "T10ursday" (where the result of a previous replacement is treated as a format option)
  82.       `$1\\${[...i[1][0] as string].join('\\')}`
  83.     )
  84.   )
  85.  
  86.   return format.replace(/\\(.)/g, '$1') // unescape the result
  87. }
Advertisement
Add Comment
Please, Sign In to add comment