brinkiepie
5/7/2016 - 4:07 PM

time formatting auxiliaries taken from plug_p0ne

time formatting auxiliaries taken from plug_p0ne

# from p0ne.auxiliaries.ls
aux =
    pad: (num) ->
        if 0 <= num < 10 then "0#num"
        else             then "#num"

    # time formatting
    timezoneOffset: new Date().getTimezoneOffset!
    getTime: (t = Date.now!) !->
        return new Date(t - @timezoneOffset *60_000min_to_ms).toISOString! .replace(/.+?T|\..+/g, '')
    getDateTime: (t = Date.now!) !->
        return new Date(t - @timezoneOffset *60_000min_to_ms).toISOString! .replace(/T|\..+/g, ' ')
    getDate: (t = Date.now!) !->
        return new Date(t - @timezoneOffset *60_000min_to_ms).toISOString! .replace(/T.+/g, '')
    getISOTime: (t = new Date)!->
        return t.toISOString! .replace(/T|\..+/g, " ")
    parseISOTime: (t) !->
        return new Date(t) - @timezoneOffset *60_000min_to_ms

    # show a timespan (in ms) in a human friendly format (e.g. "2 hours")
    humanTime: (diff, shortFormat) !->
        if diff < 0
            return "-#{@humanTime -diff}"
        else if not shortFormat and diff < 2_000ms # keep in sync with ago()
            return "just now"
        b = [60to_min, 60to_h, 24to_days, 360.25to_years]; c = 0
        diff /= 1000to_s
        while diff > 2*b[c] then diff /= b[c++]
        if shortFormat
            return "#{Math.round diff}#{'smhdy'[c]}"
        else
            return "#{Math.round(diff)} #{<[ seconds minutes hours days years ]>[c]}"

    # show a timespan (in s) in a format like "mm:ss" or "hh:mm:ss" etc
    mediaTime: (dur) !->
        return "-#{@mediaTime -dur}" if dur < 0
        # usually the user would rather read 580 hours as "580:00:00" instead of "24:04:00:00"
        m=0
        if dur >= 60
            m = ~~(dur / 60); dur %= 60
            if m >= 60
                h = ~~(m / 60); m %= 60
        return "#{if h then @pad(h)+":" else ''}#{@pad(m)}:#{@pad(~~dur)}"

    # create string saying how long ago a given timestamp (in ms since epoche) is
    ago: (d) !->
        d = Date.now! - d
        if d < 2_000ms # keep in sync with humanTime()
            return "just now"
        else
            return "#{@humanTime(d)} ago"
// from p0ne.auxiliaries.ls
var aux;
aux = {
  pad: function(num){
    if (0 <= num && num < 10) {
      return "0" + num;
    } else {
      return num + "";
    }
  },

  // time formatting
  timezoneOffset: new Date().getTimezoneOffset(),
  getTime: function(t){
    t == null && (t = Date.now());
    return new Date(t - this.timezoneOffset * 60000).toISOString().replace(/.+?T|\..+/g, '');
  },
  getDateTime: function(t){
    t == null && (t = Date.now());
    return new Date(t - this.timezoneOffset * 60000).toISOString().replace(/T|\..+/g, ' ');
  },
  getDate: function(t){
    t == null && (t = Date.now());
    return new Date(t - this.timezoneOffset * 60000).toISOString().replace(/T.+/g, '');
  },
  getISOTime: function(t){
    t == null && (t = new Date);
    return t.toISOString().replace(/T|\..+/g, " ");
  },
  parseISOTime: function(t){
    return new Date(t) - this.timezoneOffset * 60000;
  },

  // show a timespan (in ms) in a human friendly format (e.g. "2 hours")
  humanTime: function(diff, shortFormat){
    var b, c;
    if (diff < 0) {
      return "-" + this.humanTime(-diff);
    } else if (!shortFormat && diff < 2000) {
      return "just now";
    }
    b = [60, 60, 24, 360.25];
    c = 0;
    diff /= 1000;
    while (diff > 2 * b[c]) {
      diff /= b[c++];
    }
    if (shortFormat) {
      return Math.round(diff) + "" + 'smhdy'[c];
    } else {
      return Math.round(diff) + " " + ['seconds', 'minutes', 'hours', 'days', 'years'][c];
    }
  },

  // show a timespan (in s) in a format like "mm:ss" or "hh:mm:ss" etc
  mediaTime: function(dur){
    var m, h;
    if (dur < 0) {
      return "-" + this.mediaTime(-dur);
    }
    // usually the user would rather read 580 hours as "580:00:00" instead of "24:04:00:00"
    m = 0;
    if (dur >= 60) {
      m = ~~(dur / 60);
      dur %= 60;
      if (m >= 60) {
        h = ~~(m / 60);
        m %= 60;
      }
    }
    return (h ? this.pad(h) + ":" : '') + "" + this.pad(m) + ":" + this.pad(~~dur);
  },

  // create string saying how long ago a given timestamp (in ms since epoche) is
  ago: function(d){
    d = Date.now() - d;
    if (d < 2000) {
      return "just now";
    } else {
      return this.humanTime(d) + " ago";
    }
  }
};