Code:
--Time Zone Clock by Neon612 (2011)
require 'cairo'
local clock_settings = {
{
time_zone = 0, -- The time zone of the clock, (default: 0 = GMT)
clock_r = 25, -- Radius of clock face (not including border) in pixels (required)
xc = 50, -- X coordinates, relative to top left corner, in pixels (required)
yc = 50, -- Y coordinates, relative to top left corner, in pixels (required)
face_color = {0xFFFFFF, 1}, -- Color of the clock face, and alpha (default: white with 100% alpha)
hands_color = {0x000000, 0.5}, -- Color of hands, and alpha (default: black with 50% alpha)
show_seconds = true, -- Show second hand, use a Conky update_interval > 1s (default: false)
font = "Droid Sans Mono", -- Font to display time and time zone (default: Droid Sans Mono)
font_size = 18, -- Font size (default: 18)
font_color = {0xFFFFFF, 1}, -- Font color (default: white)
show_time = true, -- Show the time below the clock face (default: false)
show_zone = true, -- Show the time zone (default: false)
},
{
time_zone = -0100,
clock_r = 25,
xc = 150,
yc = 50,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0200,
clock_r = 25,
xc = 250,
yc = 50,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0300,
clock_r = 25,
xc = 350,
yc = 50,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0400,
clock_r = 25,
xc = 450,
yc = 50,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0500,
clock_r = 25,
xc = 50,
yc = 150,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0600,
clock_r = 25,
xc = 150,
yc = 150,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0700,
clock_r = 25,
xc = 250,
yc = 150,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0800,
clock_r = 25,
xc = 350,
yc = 150,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -0900,
clock_r = 25,
xc = 450,
yc = 150,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -1000,
clock_r = 25,
xc = 50,
yc = 250,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -1100,
clock_r = 25,
xc = 150,
yc = 250,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = -1200,
clock_r = 25,
xc = 250,
yc = 250,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0100,
clock_r = 25,
xc = 350,
yc = 250,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0200,
clock_r = 25,
xc = 450,
yc = 250,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0300,
clock_r = 25,
xc = 50,
yc = 350,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0400,
clock_r = 25,
xc = 150,
yc = 350,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0500,
clock_r = 25,
xc = 250,
yc = 350,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0600,
clock_r = 25,
xc = 350,
yc = 350,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0700,
clock_r = 25,
xc = 450,
yc = 350,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0800,
clock_r = 25,
xc = 50,
yc = 450,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 0900,
clock_r = 25,
xc = 150,
yc = 450,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 1000,
clock_r = 25,
xc = 250,
yc = 450,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 1100,
clock_r = 25,
xc = 350,
yc = 450,
show_seconds = true,
show_time = true,
show_zone = true,
},
{
time_zone = 1200,
clock_r = 25,
xc = 450,
yc = 450,
show_seconds = true,
show_time = true,
show_zone = true,
},
}
function conky_draw_clock()
if conky_window == nil then return end
local w = conky_window.width
local h = conky_window.height
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, w, h)
cr = cairo_create(cs)
local updates = tonumber(conky_parse('${updates}'))
if updates > 5 then
for i in pairs(clock_settings) do
draw_clock(clock_settings[i])
end
end
end
function draw_clock(t)
local time_zone = t.time_zone
local clock_r = t.clock_r
local xc = t.xc
local yc = t.yc
local face_color = t.face_color
local hands_color = t.hands_color
local show_seconds = t.show_seconds
local font = t.font
local font_size = t.font_sie
local font_color = t.font_color
local show_time = t.show_time
local show_zone = t.show_zone
--set defaults
if time_zone == nil then time_zone = 0 end
if face_color == nil then face_color = {0xFFFFFF, 1} end
if hands_color == nil then hands_color = {0x000000, 0.5} end
if show_seconds == nil then show_seconds = false end
if font == nil then font = "Droid Sans Mono" end
if font_size == nil then font_size = 18 end
if font_color == nil then font_color = {0xFFFFFF, 1} end
if show_time == nil then show_time = false end
if show_zone == nil then show_zone = false end
--draw face
cairo_arc(cr, xc, yc, clock_r, 0, 2 * math.pi)
cairo_set_source_rgba(cr, rgb_to_r_g_b(face_color))
cairo_fill_preserve(cr)
cairo_stroke(cr)
-- Grab time
local hours = os.date("!%I")
local mins = os.date("!%M")
local secs = os.date("!%S")
local hr_shift = tonumber(time_zone) / 100
local min_shift = tonumber(time_zone) % 100
-- local time_diff = os.difftime(tonumber(hours), tonumber(hours) + hr_shift)
-- print(time_diff)
local hours = tonumber(hours) + math.floor(hr_shift)
local mins = tonumber(mins) + min_shift
if hours < 0 then
hours = 12 + hours
elseif hours > 12 then
hours = hours - 12
elseif hours == 0 then
hours = 12
end
if mins < 10 then
mins = "0" .. mins
end
secs_arc = (2 * math.pi / 60) * secs
mins_arc = (2 * math.pi / 60) * mins
hours_arc = (2 * math.pi / 12) * hours + mins_arc / 12
-- Draw hour hand
xh = xc + 0.7 * clock_r * math.sin(hours_arc)
yh = yc - 0.7 * clock_r * math.cos(hours_arc)
cairo_move_to(cr, xc, yc)
cairo_line_to(cr, xh, yh)
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND)
cairo_set_line_width(cr, 5)
cairo_set_source_rgba(cr, rgb_to_r_g_b(hands_color))
cairo_stroke(cr)
-- Draw minute hand
xm = xc + 0.9 * clock_r * math.sin(mins_arc)
ym = yc - 0.9 * clock_r * math.cos(mins_arc)
cairo_move_to(cr, xc, yc)
cairo_line_to(cr, xm, ym)
cairo_set_line_width(cr, 3)
cairo_stroke(cr)
-- Draw seconds hand
if show_seconds then
xs = xc + 0.9 * clock_r * math.sin(secs_arc)
ys = yc - 0.9 * clock_r * math.cos(secs_arc)
cairo_move_to(cr, xc, yc)
cairo_line_to(cr, xs, ys)
cairo_set_line_width(cr, 1)
cairo_stroke(cr)
end
--setup font, font size, and font color
cairo_set_source_rgba(cr, rgb_to_r_g_b(font_color))
cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL)
cairo_set_font_size(cr, font_size)
--print time below clock
if show_time then
cairo_move_to(cr, xc - clock_r, yc + clock_r + 20)
cairo_show_text(cr, hours .. ":" .. mins .. ":" .. secs)
cairo_stroke(cr)
end
if show_zone then
if time_zone > 0 and (time_zone/100) < 10 then
time_zone = "+0" .. time_zone
elseif (time_zone/100) >= 10 then
time_zone = "+" .. time_zone
elseif time_zone < 0 and (time_zone/100) > -10 then
time_zone = "-0" .. math.abs(time_zone)
end
cairo_move_to(cr, xc - clock_r, yc + clock_r + 40)
cairo_show_text(cr, time_zone)
cairo_stroke(cr)
end
end
function rgb_to_r_g_b(col_a)
return ((col_a[1] / 0x10000) % 0x100) / 255., ((col_a[1] / 0x100) % 0x100) / 255., (col_a[1] % 0x100) / 255., col_a[2]
end
If you have used any of wlourf's scripts you should know how the settings table works. The only things required are clock radius, and x and y coordinates for the center. Everything else has a default setting.
Bookmarks