Resource icon

OBS Lua Datetime digital clock 2019-12-24

albinou

New Member
albinou submitted a new resource:

Datetime - This scipt transforms a text source into a digital clock (format can be selected).

This scipt transforms a text source into a digital clock. The datetime format is configurable and uses the same syntax than the Lua os.date() call, i.e.:
Code:
%a    abbreviated weekday name (e.g., Wed)
%A    full weekday name (e.g., Wednesday)
%b    abbreviated month name (e.g., Sep)
%B    full month name (e.g., September)
%c    date and time (e.g., 09/16/98 23:48:10)
%d    day of the month (16) [01-31]
%H    hour, using a 24-hour clock (23) [00-23]
%I    hour, using a 12-hour clock (11)...

Read more about this resource...
 

fyrkantis

New Member
Thank you, this is very useful. However, it would be nice to be able to add several text sources with clocks, and have an ISO8601 compliant date order option (year-month-day).
 

djbauer

New Member
Do we just copy the txt to a .txt file and install that as a script?
Some more info would be nice.
 

baltakatei

New Member
Do we just copy the txt to a .txt file and install that as a script?
Some more info would be nice.
  1. Click grey "Go to download" button on top right of this page.
  2. Save the datetime.lua file somewhere.
  3. Open OBS.
  4. In the Sources list (to the right of the Scenes list), right-click to create a new text source (ex: "Text (FreeType 2)"). Name it something unique (ex: "clock1"). All that matters is that a text source appears; you don't have to populate it with anything or format anything yet. The script will do that later.
  5. Go to the Tools dropdown menu, select "Scripts".
  6. Click the "+" button to add a new item to the Loaded Scripts list. Navigate to the find the datetime.lua file you saved earlier and Open it.
  7. Now, select the new entry in the Loaded Scripts list. A "Description" should appear to the right of the list with "Datetime format" and "Text Source" fields below.
  8. Change the "Datetime format" field to what you want (ex: "%Y-%m-%dT%H:%M:%S%z" for "2020-06-11T01:28:13+00:00" (ISO-8601)).
  9. Change the "Text Source" field to exactly match the name of the source you created earlier (i.e.: "clock1").
  10. Press the "Close" button on the "Scripts" window.
  11. Now, the text source you added should automatically be populated with the current time using the format from the "Datetime format" field you filled in earlier. Right-click the text source (i.e. "clock1"), select "Properties" and format the text as you'd like it (click "Select font" to modify font type and size; change "color 1" and "color 2" field to change text color, etc.).
The above steps were done using OBS 0.0.1 (linux) on a Debian 10 GNU/Linux machine. OBS was installed via these steps.

This text post by
Steven Baltakatei Sandoval is licensed under CC BY-SA 4.0.
 

djbauer

New Member
  1. Click grey "Go to download" button on top right of this page.
  2. Save the datetime.lua file somewhere.
  3. Open OBS.
  4. In the Sources list (to the right of the Scenes list), right-click to create a new text source (ex: "Text (FreeType 2)"). Name it something unique (ex: "clock1"). All that matters is that a text source appears; you don't have to populate it with anything or format anything yet. The script will do that later.
  5. Go to the Tools dropdown menu, select "Scripts".
  6. Click the "+" button to add a new item to the Loaded Scripts list. Navigate to the find the datetime.lua file you saved earlier and Open it.
  7. Now, select the new entry in the Loaded Scripts list. A "Description" should appear to the right of the list with "Datetime format" and "Text Source" fields below.
  8. Change the "Datetime format" field to what you want (ex: "%Y-%m-%dT%H:%M:%S%z" for "2020-06-11T01:28:13+00:00" (ISO-8601)).
  9. Change the "Text Source" field to exactly match the name of the source you created earlier (i.e.: "clock1").
  10. Press the "Close" button on the "Scripts" window.
  11. Now, the text source you added should automatically be populated with the current time using the format from the "Datetime format" field you filled in earlier. Right-click the text source (i.e. "clock1"), select "Properties" and format the text as you'd like it (click "Select font" to modify font type and size; change "color 1" and "color 2" field to change text color, etc.).
The above steps were done using OBS 0.0.1 (linux) on a Debian 10 GNU/Linux machine. OBS was installed via these steps.

This text post by
Steven Baltakatei Sandoval is licensed under CC BY-SA 4.0.
So it's a right click and Save As thing... kept sending me to the page with the code...
Thanks dude!
 

BlackRedBird

New Member
I just tried this script on a computer with a Dutch Windows language version. Unfortunately, the date using this script still used English wording for date values. Is it possible to change the locale, so the date is displayed in other languages?
 

Samogrey

New Member
Is it possible to make a line break between two elements? I wanna put the Date and Time on two different lines.
 

tokyoskier

New Member
Thank you! this is great! Would it be possible to add or substruct a value and make it possible to show different timezone?
 

Alvise

New Member
Ciao come faccio a togliere i secondi dal time e quindi avere il timer con solo le ore e i minuti?
Hi how can I remove the seconds from the time and then have the timer with only the hours and minutes?
 

Prvi

New Member
Hi,
I'm using OBS v26.1.1 and in Tools->Scripts I can't select Text Source. It works if I write directly name of my Text source.
To fix this change line 111 from
source_id = obs.obs_source_get_id(source)
to
source_id = obs.obs_source_get_unversioned_id(source)

And then you can select Text Source.
Regards.
 

Richard Gagnon

New Member
I have done several modifications to the script to accommodate timezone in the format specifiers and appending st, nd, rd th to the day of month.

Here is the modified set_datetime_text function

Note: there is a better code ... LUA does not properly deal with string to number conversions :(

function set_datetime_text(source, format)
if string.find(format, "%%dx") ~= nil then
local s = "thstndrd"
local i = 0
local d = os.date("%d")
if d[-1] == '1' then
i = 2
elseif d[-1] == '2' then
i = 4
elseif d[-1] == '3' then
i = 6
end
format = format.gsub(format, "%%dx", "%%d"..s.sub(s, i, 2))
end
local text = os.date(format)
local settings = obs.obs_data_create()

obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
end

and the modified script_description function


function script_description()
return "Sets a text source to act as a clock when the source is active.\
\
The datetime format can use the following tags:\
\
%a abbreviated weekday name (e.g., Wed)\
%A full weekday name (e.g., Wednesday)\
%b abbreviated month name (e.g., Sep)\
%B full month name (e.g., September)\
%c date and time (e.g., 09/16/98 23:48:10)\
%d day of the month (16) [01-31]\
%dx day of month with st,rd,th,nd (01st) [01-31]\
%H hour, using a 24-hour clock (23) [00-23]\
%I hour, using a 12-hour clock (11) [01-12]\
%M minute (48) [00-59]\
%m month (09) [01-12]\
%p either \"am\" or \"pm\" (pm)\
%S second (10) [00-61]\
%w weekday (3) [0-6 = Sunday-Saturday]\
%x date (e.g., 09/16/98)\
%X time (e.g., 23:48:10)\
%Y full year (1998)\
%y two-digit year (98) [00-99]\
%z timezone (0600)\
%Z timezone (Mountain Standard Time)\
%% the character `%´"
end
 

Richard Gagnon

New Member
Apologies for the changes. I have finally had a chance to run a battery of tests on the append of th, nd, st, rd to the day and have made the following script changes to the previous script I posted. This one properly addresses the removal of preceding '0' in range if days 1 to 9 and treats 11 to 13 with a th.
days between 4 and 20 and 24 to 30 will all end with th
days 1, 21, 31 will end with st
days 2, 22 will end with nd
days 3, 23 will end with rd

function set_datetime_text(source, format)
if string.find(format, "%%dx") ~= nil then
local s = "thstndrd"
local i = 0
local x = 0
local d = os.date("%d")
d="24"
x = tonumber(d) -- make the day string a number
if x < 4 or x > 20 then
i = tonumber(d.sub(d, -1)) * 2 -- get the number value of the last digit and multiple by 2 for indexing into s
if i > 6 then
i = 0
end
end

format = format.gsub(format, "%%dx", tostring(x)..string.sub(s, i + 1, i + 2))
end
local text = os.date(format)
local settings = obs.obs_data_create()

obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
end
 

Richard Gagnon

New Member
Apologies for the changes. I have finally had a chance to run a battery of tests on the append of th, nd, st, rd to the day and have made the following script changes to the previous script I posted. This one properly addresses the removal of preceding '0' in range if days 1 to 9 and treats 11 to 13 with a th.
days between 4 and 20 and 24 to 30 will all end with th
days 1, 21, 31 will end with st
days 2, 22 will end with nd
days 3, 23 will end with rd

function set_datetime_text(source, format)
if string.find(format, "%%dx") ~= nil then
local s = "thstndrd"
local i = 0
local x = 0
local d = os.date("%d")
d="24"
x = tonumber(d) -- make the day string a number
if x < 4 or x > 20 then
i = tonumber(d.sub(d, -1)) * 2 -- get the number value of the last digit and multiple by 2 for indexing into s
if i > 6 then
i = 0
end
end

format = format.gsub(format, "%%dx", tostring(x)..string.sub(s, i + 1, i + 2))
end
local text = os.date(format)
local settings = obs.obs_data_create()

obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
end
Forgot to remove the d="24" line please do so yourself
 

Richard Gagnon

New Member
Here is another fix to allow the text selection for the script dropdown to be populated

function script_properties()
local props = obs.obs_properties_create()

obs.obs_properties_add_text(props, "format", "Datetime format", obs.OBS_TEXT_DEFAULT)

local p = obs.obs_properties_add_list(props, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
local sources = obs.obs_enum_sources()
if sources ~= nil then
for _, source in ipairs(sources) do
-- THIS LINE IS INCORRECT use following line source_id = obs.obs_source_get_id(source)
source_id = obs.obs_source_get_unversioned_id(source)
if source_id == "text_gdiplus" or source_id == "text_ft2_source" then
local name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)
end
end
end
obs.source_list_release(sources)

return props
end
 

Wayne01

Member
Well at first thank you for the installation guide
At second, can I use "enter" somehow in the date box menu to transfer the string?

So it would look like this:

Streamer Time:
Fri 19 : 11:77 AM

Instead of

Streamer Time: Fri 19 : 11:77 AM

Ha?
 

Johnnybravo

New Member
Hola a todos, a mi me funciona perfecto el script pero me sale todo en ingles, es decir los dias de semana, meses en ingles.. hay alguna forma de que salga en español?
 

erikfdev

New Member
I just tried this script on a computer with a Dutch Windows language version. Unfortunately, the date using this script still used English wording for date values. Is it possible to change the locale, so the date is displayed in other languages?
A very late reply to BlackRedBird, but I had the same issue and I solved it.
There is a difference in behavior of OBS for this issue on Linux and on Windows (I don't know on Mac). This script works fine on Linux; the date is in Dutch.
The English texts only show up on Windows. The reason is that OBS-LUA on Windows does not take the locale settings into account.
A call to os.setlocale() returns "C".

SO, as I live in the Dutch speaking part of Belgium, I modified the function set_datetime_text() as follows (note the setlocale() calls) :
Lua:
function set_datetime_text(source, format)
   -- save the current locale "time" setting
    local locale_backup = os.setlocale(nil, "time")
   -- set our own "time" locale setting
    os.setlocale("nl_BE.UTF-8", "time")
    
    local text = os.date(format)
    -- restore the original "time" locale setting
    os.setlocale(locale_backup, "time")
    
    local settings = obs.obs_data_create()
    ...
    ...

So, if you live in the Netherlands, then use "nl_NL.UTF-8" or just plain "nl_NL" as parameter for the setlocale() call.

I guess this would could also be an answer to the question of Johnnybravo but then with Spanish settings. I think "es_ES.UTF-8" will do for him.
 
Top