Easy one: WF: Minutes to sleep doesn't work

Hi there,

I wanna calculate with the minutes to sleep but obviously run into troubles.

The first part works - the debuggers says 79200 => 22 Std.:
var usersleepTime = UserProfile.getProfile().sleepTime;
But neither am I sure wether this is already the remaining seconds from now on or only the sleep time I set the watch to and furthermore I can only show it when I put it "toString".
And that only shows "Obj: 164".
But what I wanna get is a Lang.Number I can calculate with.
Can anybody help me.
Guess it's pretty simple but non of my google actions help me. 
Thanks a lot !!!
Marcus
  • Das geht nicht:

    var now = Time.now();
            var delta = usersleepTime.subtract(now);


    Das ging auch nicht:

    var
    usersleepTime = UserProfile.getProfile().sleepTime.value();


    und tolong() oder so geht auch nicht.
  • Hi, 

    first of all, the forum language is English, so you'll have a better chance to get help when you stick to that. 

    Secondly, did you read the api reference?

    var sleepTime as Time.Duration or Null

    Typical sleep time as configured by the user.

    Stored as a Duration past local midnight.

    So you should be able to do everything with it that a normal duration also provides. 

  • That's exactly my problem - I can't deal with the duration feature as I want to. 

    I mean according to the api reference a difference between time.moment and duration should also work but why doesn't this work then?

    var now = Time.now();

            var delta = usersleepTime.subtract(now);

    And why can't I get it displayed?
  • With sleep time, you only have hours and minutes if I recall. while with now() it's both date and time

    So when you subtract now from sleep time, you wind up with a screwy value

    Also remember the current time could be 22:17 and the sleep time, 1:32 (the next day)

  • Ok this explains why that doesn't work.

    I another function I'm using

    var akttime = Sys.getClockTime();
    var deltahours = akttime.hour-anothertime.hour;

    and use if statements to avoid this problem.
    But that doesn't work with duration either.

    So any suggestions?

    I mean there must be a way to solve such a simple task. 
  • Since sleepTime is expressed in terms of seconds past local midnight, you can determine the current time in terms of seconds past local midnight and compare the two values. Then you can do whatever you want with the result, including converting it to minutes and formatting it for display.

    (The docs for Time, Moment and Duration are helpful in this case)

    e.g.

    // Returns:
    // - null if the sleep time is unavailable
    // - The number of seconds until today's sleep time:
    //   >= 0 if the sleep time is now or in the future
    //   < 0 if the sleep time is in the past
    function secondsToSleep() {
        var userProfile = UserProfile.getProfile();
        if (userProfile.sleepTime == null) {
            return null;
        }
    
        // get a Moment for the current time
        var now = Time.now();
        // get a Moment for midnight today (the beginning of the current day)
        var today = Time.today();
        // current time (in seconds past midnight) = now - today
        var currentSecondsPastMidnight = now.compare(today);
    
        return userProfile.sleepTime.value() - currentSecondsPastMidnight;
    }

    Note that this function returns a negative number if the current time is already past today's sleep time. e.g. If the your sleep time is 10:00 PM (22:00) and the current time is 10:01 PM (22:01), then this function will return -60. It can easily be tweaked to return the time until *tomorrow's* sleep time in this case: if the result is negative, add one day's worth of seconds (24 * 60 * 60) to the result.

  • Take the current hour and minutes, convert to minutes (min+hour*60)

     do the same with sleeptime.

    if the sleep mins are less than the now mins add 24*60 mins to sleep mins.(the next day)

    subtract now mins from sleep minutes.  That gives you minutes until sleep time, which you can then show as hh:mm

  • Great !!!    Thanks !!!!!

    That's what I was looking for!

    Thanks a lot!

    I'm so happy it's no actually working and learned how to deal with duration events for further tasks to come!

    Thanks again and have a nice day!!!

    Thanks!