You are not logged in.

#1 2008-11-23 18:46:48

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

writing a clock app

I'm finally getting around to setting me up a dzen. I want to display the time in minutes (hh:mm). I'm not sure how best to go about this.

Ideally, I'd ask the kernel to tell me whenever the minute changes on the system clock. Reading time(7) and the man pages in its "SEE ALSO" section has led me to believe that there's no way to do this. Did I miss something?

Otherwise, I guess I have to poll for the time periodically using ctime(3) or some such function. Boo hoo.

Offline

#2 2008-11-23 18:54:49

thayer
Fellow
From: Vancouver, BC
Registered: 2007-05-20
Posts: 1,560
Website

Re: writing a clock app

I might be misunderstanding your dilemma, but what about this?

$ date +%H:%M

thayer williams ~ cinderwick.ca

Offline

#3 2008-11-23 18:59:26

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: writing a clock app

and wrap it around while true; do date....; sleep 5; done

Offline

#4 2008-11-23 20:05:47

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: writing a clock app

Yeah. I guess I'm just a perfectionist: I don't want to call "date" every second when I only need it once per minute. But hey, it's not that bad. Thanks.

Offline

#5 2008-11-23 20:23:40

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: writing a clock app

I don't see what's wrong with conky....


The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#6 2008-11-23 20:24:51

string
Member
Registered: 2008-11-03
Posts: 286

Re: writing a clock app

peets wrote:

Yeah. I guess I'm just a perfectionist: I don't want to call "date" every second when I only need it once per minute. But hey, it's not that bad. Thanks.

"sleep 60" then?

Offline

#7 2008-11-23 20:26:09

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: writing a clock app

Improvement of script:

while true; do date && sleep 1 && clear; done


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#8 2008-11-23 20:29:04

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: writing a clock app

#!/bin/bash
while [ 1 ]
do
    echo -e -n "\r$(date +"%H:%M")"
    SEC=$(date +"%S")
    sleep $((60-$SEC))
done

This will display the hour and minute once every full minute then sleep the remaining seconds until the next minute. It resynchronizes itself every loop too so there's no need to worry about its interval lapsing.

Last edited by Xyne (2008-11-23 20:48:27)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#9 2008-11-24 06:36:38

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: writing a clock app

Xyne, that's pretty smart.
In the meantime, I went ahead and wrote this:

#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>

void print_time(int signum)
{
    char buf[21];
    struct timeval now;
    gettimeofday(&now, NULL);
    strftime(buf, sizeof buf, "%a %Y-%m-%d %H:%M", localtime(&now.tv_sec));
    printf("%s\n", buf);
}

int main()
{
    print_time(0);

    /* set up SIGALRM-handling callback */
    struct sigaction alarm;
    alarm.sa_handler = print_time;
    sigemptyset(&alarm.sa_mask);
    alarm.sa_flags = 0;
    sigaction(SIGALRM, &alarm, NULL);

    /* set up timer. Sends a SIGALRM when countdown reaches 0,
       then starts counting down again from 60 seconds. */
    struct timeval interval = {60, 0};
    struct timeval timeleft;
    struct timeval now;
    gettimeofday(&now, NULL);
    struct tm *now_tm = localtime(&now.tv_sec);
    if(now.tv_usec == 0) {
        timeleft.tv_sec = 60 - now_tm->tm_sec;
    } else {
        timeleft.tv_sec = 59 - now_tm->tm_sec;
        timeleft.tv_usec = 1000000 - now.tv_usec;
    }
    struct itimerval timer = {interval, timeleft};
    setitimer(ITIMER_REAL, &timer, NULL);

    /* do nothing until a signal is received, then do nothing again */
    while(pause()) {
    }

    /* this should never be reached */
    return 0;
}

I learned about signals (I need an excuse for my hours-long solution vs your minutes-long one). It gives the time about 15-25 microseconds late on my system --I guess that's the cool feature.

Offline

Board footer

Powered by FluxBB