Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect result on iPhone #90

Closed
urbian opened this issue Dec 7, 2011 · 6 comments
Closed

incorrect result on iPhone #90

urbian opened this issue Dec 7, 2011 · 6 comments

Comments

@urbian
Copy link

urbian commented Dec 7, 2011

Just ran a quick test app on the iPhone simulator and it seems to measure time differently.

I was getting the diff of two moments and on a desktop browser the result was 1 hour, on iPhone it was 1 year

@urbian
Copy link
Author

urbian commented Dec 7, 2011

here's the code i'm running, excuse the mess, it's just a quick prototype

<!doctype html>
<html lang=en>
<meta charset=utf-8>

<title>What's next?</title>

<meta name=description content="See what's next up on your agenda">
<meta name=author content="Wayne Ashley Berry">

<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1.0">


<link rel=stylesheet href=css/style.css>

<div id=container>

    <h1 id=clock></h1>

    <h2></h2>

</div>

<script src=js/libs/jquery.js></script>
<script src=js/libs/moment.js></script>
<script src=js/libs/mustache.js></script>
<script src=js/utils/underscore.js></script>

<script>

    var clock = $('h1');
    var message = $('h2');
    var today = moment().format('YYYY-MM-DD');

    var tasks = [
        {time: '05:00', task: 'Wake Up'},
        {time: '06:00', task: 'Go for a jog'},
        {time: '13:00', task: 'Lunch Break'},
        {time: '18:00', task: 'Finish work'},
        {time: '19:00', task: 'Make dinner'},
        {time: '21:00', task: 'Play Minecraft'}
    ];

    var tickTock = function () {
        var time = moment().format('h:mma');
        var html = Mustache.to_html("It's {{time}} ", {time: time});
        clock.html(html);
    };

    // make the clock go tick-tock
    var loop = function () {
        setInterval(function () {
            tickTock();
        }, 6000);
    };

    // spit out some html
    var renderTask = function (task, time) {
        var html = Mustache.to_html("{{task}} in {{time}} time, then {{next}}", {task: task, time: time, next: tasks[1].task.toLowerCase()});
        message.html(html);
    };

    // take the next task, work out the time difference, then render
    var taskly = function () {
        var task = tasks[0];
        var nextString = moment().format('YYYY-MM-DD') + ' ' + task.time;
        var next = moment(nextString);
        var diff = next.fromNow(true);
        // fix grammar
        diff = diff.replace('an hour', 'an hours');
        diff = diff.replace('a minute', 'a minutes');
        renderTask(task.task, diff);
    };

    // find past events, delete them
    // then we can assume tasks[0] is the next event
    var next = function () {
        var now = moment();
        var i = 0;
        // delete past events
        _.each(tasks, function (t) {
            var time = moment(now.format('YYYY-MM-DD') + ' ' + t.time);
            var diff = time.diff(now, 'minutes');
            if (diff < 0) {
                delete tasks[i];
            }
            i++;
        });
        // remove undefined objects in array
        tasks = _.flatten(tasks);
        // work out the time difference and render
        taskly();
    };

    next();
    tickTock();
    loop();


</script>

@timrwood
Copy link
Member

timrwood commented Dec 7, 2011

Possibly a string parsing issue.

var next = moment(nextString); // change to the following
var next = moment(nextString, 'YYYY-MM-DD HH:mm'); // manually specify format

Although it's probably better to not use formatting and parsing, as its slower than direct manipulation.

var tasks = [
    {time: '05:00', hour:5, minute:0, task: 'Wake Up'},
    {time: '06:00', hour:6, minute:0, task: 'Play Minecraft'},
    {time: '13:00', hour:13, minute:0, task: 'Play Minecraft'},
    {time: '18:00', hour:18, minute:0, task: 'Play Minecraft'},
    {time: '19:00', hour:19, minute:0, task: 'Play Minecraft'},
    {time: '21:00', hour:21, minute:0, task: 'Play Minecraft'}
];
var taskly = function () {
    var task = tasks[0];
    // change to using manip methods instead of format and parse
    var next = moment().hours(task.hour).minutes(task.minute);
    var diff = next.fromNow(true);
    // fix grammar
    diff = diff.replace('an hour', 'an hours');
    diff = diff.replace('a minute', 'a minutes');
    renderTask(task.task, diff);
};

Also, I fixed your tasks list :D

@urbian
Copy link
Author

urbian commented Dec 7, 2011

Hahaha, thanks :) will check it out in the morning and test on different devices

@jgwhite
Copy link
Contributor

jgwhite commented Dec 11, 2011

Could this issue be iOS4 dependent? Seems like a few things are improved and fixed in iOS5.

@timrwood
Copy link
Member

Any update @knnktr ?

@timrwood
Copy link
Member

Closing, issue, let me know if there are still problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants