You are not logged in.
Pages: 1
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
I might be misunderstanding your dilemma, but what about this?
$ date +%H:%M
thayer williams ~ cinderwick.ca
Offline
and wrap it around while true; do date....; sleep 5; done
Offline
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
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
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
Improvement of script:
while true; do date && sleep 1 && clear; done
Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.
Offline
#!/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 Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
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
Pages: 1