Skip to content

Commit 6ef9f57

Browse files
committed
add support for custom time formats
1 parent 46abee6 commit 6ef9f57

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

gtklock.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ void gtklock_focus_window(struct GtkLock *gtklock, struct Window* win) {
4949
}
5050

5151
void gtklock_update_clocks(struct GtkLock *gtklock) {
52-
time_t now = time(&now);
53-
struct tm *now_tm = localtime(&now);
54-
if(now_tm == NULL) return;
55-
snprintf(gtklock->time, 8, "%02d:%02d", now_tm->tm_hour, now_tm->tm_min);
52+
GDateTime *time = g_date_time_new_now_local();
53+
if(time == NULL) return;
54+
if(gtklock->time) g_free(gtklock->time);
55+
gtklock->time = g_date_time_format(time, gtklock->time_format ? gtklock->time_format : "%R");
56+
g_date_time_unref(time);
57+
5658
for(guint idx = 0; idx < gtklock->windows->len; idx++) {
5759
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
5860
window_update_clock(ctx);
@@ -76,7 +78,7 @@ struct GtkLock* create_gtklock(void) {
7678
}
7779

7880
void gtklock_activate(struct GtkLock *gtklock) {
79-
gtklock->draw_clock_source = g_timeout_add_seconds(5, gtklock_update_clocks_handler, gtklock);
81+
gtklock->draw_clock_source = g_timeout_add_seconds(1, gtklock_update_clocks_handler, gtklock);
8082
gtklock_update_clocks(gtklock);
8183
if(gtklock->use_input_inhibit) input_inhibitor_get();
8284
}

include/gtklock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ struct GtkLock {
2020

2121
gboolean use_layer_shell;
2222
gboolean use_input_inhibit;
23-
char time[8];
23+
char *time;
24+
char *time_format;
2425
};
2526

2627
extern struct GtkLock *gtklock;

source.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static char *config_path = NULL;
2525
static char *style_path = NULL;
2626
static char *module_path = NULL;
2727
static char *background_path = NULL;
28+
static char *time_format = NULL;
2829

2930
static GOptionEntry main_entries[] = {
3031
{ "daemonize", 'd', 0, G_OPTION_ARG_NONE, &should_daemonize, "Detach from the controlling terminal after locking", NULL },
@@ -37,6 +38,7 @@ static GOptionEntry config_entries[] = {
3738
{ "style", 's', 0, G_OPTION_ARG_FILENAME, &style_path, "Load CSS style file", NULL },
3839
{ "module", 'm', 0, G_OPTION_ARG_FILENAME, &module_path, "Load gtklock module", NULL },
3940
{ "background", 'b', 0, G_OPTION_ARG_FILENAME, &background_path, "Load background", NULL },
41+
{ "time-format", 't', 0, G_OPTION_ARG_STRING, &time_format, "Set time format", NULL },
4042
};
4143

4244
static GOptionEntry debug_entries[] = {
@@ -239,6 +241,8 @@ int main(int argc, char **argv) {
239241
GModule *module = NULL;
240242
if(module_path != NULL) module = module_load(module_path);
241243

244+
gtklock->time_format = time_format;
245+
242246
g_signal_connect(gtklock->app, "activate", G_CALLBACK(activate), NULL);
243247
int status = g_application_run(G_APPLICATION(gtklock->app), argc, argv);
244248

0 commit comments

Comments
 (0)