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.

Andrei
  • 42,814
  • 35
  • 154
  • 218
  • 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. – peterh Sep 10 '15 at 12:57

2 Answers2

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.

Andy E
  • 338,112
  • 86
  • 474
  • 445
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.

Community
  • 1
  • 1
Ivan Solntsev
  • 2,081
  • 2
  • 31
  • 40
  • I understand that I can hardcode +N hours but I need support of time zones. – Andrei May 22 '14 at 08:14
  • You cat try guess timezone by IP address if you have server-side in your app. http://stackoverflow.com/questions/743505/how-to-get-time-zone-through-ip-address-in-php – Ivan Solntsev May 22 '14 at 08:26