Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Problem with Timer.fmt()

pillapilla Pelotas, BR
edited November 16 in Mongoose OS

Hello

I am trying to get just the hour for current time using Timer.fmt(). However, the following code:

 let now = Timer.now();
 let timestamp = Timer.fmt("%Y-%m-%d %H:%M:%S", now);
 print('++++', JSON.stringify({now: now}));
 print('++++', JSON.stringify({now: timestamp}));
 let hour = Timer.fmt("%H",now);
 print("Hour is ", JSON.stringify({hour: hour}));}

Produces:

[Nov 16 10:40:02.944] ++++ {"now":1542372001.753862}
[Nov 16 10:40:02.952] ++++ {"now":"2018-11-16 12:40:02"} 
[Nov 16 10:40:02.964] Hour is  {"hour":"  "}

Am I doing something wrong? Thanks.

Comments

  • No, you don't.
    It seems to be related to the 5 characters strings in mjs.

    function formatTime(fmt, time) {
      if (!fmt) return 'invalid format';
      let res = 0, t = Math.round(time || Timer.now()), s = '      ';
      while (res === 0) {
        res = Timer._f(s, s.length, fmt, t);
        if (res === -1) return 'invalid time';
        if (res === 0) s += '     ';
      }
      return s.slice(0, res);
    }
    

    formatTime is a copy of Timer.fmt with s initialized to 6 blanks instead of 5.

Sign In or Register to comment.