Samsung SmartTV get current time
Asked Answered
G

2

5

How can I get current time for Samsung SmartTV app written on HTML, CSS and JavaScript?

The problem is in Javascript Date object. When I call new Date() on the web environment, I receive system Date value with current time. But when I instantiate date on SmartTV it returns incorrect result. I expected the same behavior as on the web. In TV settings time is set to be automatically determined.

Correct time returns only when I set time zone manually on first TV boot. I suppose it is bug of Samsung SmartTV platform.

Gesture answered 22/5, 2014 at 7:17 Comment(1)
I don't think it would be too broad. Dear Andrei, maybe if you would insert some code samples... and vote to reopen your own quesiton as well.Trabeated
N
4

I had the same problem. new Date() on Samsung Smart TVs doesn't get the time according to the current setting, but instead gets the underlying system time. It always returns a UTC timestamp, too.

Fortunately, there's an API function, GetEpochTime(), that allows you to (sort of) fix it.

Here's the code I'm using on Samsung platforms:

(function (OldDate) {
    // Get a reference to Samsung's TIME API
    var time = document.createElement('object');
    time.setAttribute('classid', 'clsid:SAMSUNG-INFOLINK-TIME');
    document.body.appendChild(time);

    // Replace the existing Date() constructor
    window.Date = function (a, b, c, d, e, f, g) {
        switch (arguments.length) {
            case 0:
                return new OldDate(time.GetEpochTime() * 1000);

            case 1: return new OldDate(a);
            case 2: return new OldDate(a, b);
            case 3: return new OldDate(a, b, c);
            case 4: return new OldDate(a, b, c, d);
            case 5: return new OldDate(a, b, c, d, e);
            case 6: return new OldDate(a, b, c, d, e, f);
            case 7: return new OldDate(a, b, c, d, e, f, g);
        }
    };

    // Copy static function properties
    Date.now   = function () { return time.GetEpochTime() * 1000; };
    Date.UTC   = OldDate.UTC;
    Date.parse = OldDate.parse;
})(Date);

new Date() now works fine for displaying the current time on my 2013 Samsung Smart TV. You might still have problems when comparing timestamps to the current time, however.

Neille answered 24/6, 2014 at 18:34 Comment(0)
B
3

In most cases time on TV is correct, it is just in different time zone.

For example i have 2 devices near me at this moment, LG TV display date with GMT+3, and Samsung with GMT+1. Both devices display correct time.

If you need to display it with specific time zone, you can add time zone offset to date:

var date = new Date();
var offset = 4 * 3600000;//Moscow(GMT+4)
var userOffset = date.getTimezoneOffset() * 60000;
var fixedTime = new Date(date.getTime() + offset + userOffset);

If internal TV clock is not reliable, you can use web-services to get current time.

Butterfield answered 22/5, 2014 at 7:27 Comment(2)
I understand that I can hardcode +N hours but I need support of time zones.Gesture
You cat try guess timezone by IP address if you have server-side in your app. #744005Butterfield

© 2022 - 2024 — McMap. All rights reserved.