Skip to content

Instantly share code, notes, and snippets.

@wakita
Last active August 29, 2015 14:24
Show Gist options
  • Save wakita/374807ef7a01e25d96ca to your computer and use it in GitHub Desktop.
Save wakita/374807ef7a01e25d96ca to your computer and use it in GitHub Desktop.
閏秒にあわせて作成しました。100ミリ秒ごとにミリ秒単位で時刻を表示し続けます。
#!/bin/sh
while sleep 1
do
ntpq -c rv; echo
done
all: ptime
ptime.o: ptime.c
ptime: ptime.o
clang++ -o $@ $^
clean:
rm -f ptime ptime.o
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
void ptime() {
struct timeval tv;
struct tm* ptm;
char time_string[40];
long milliseconds;
/* Obtain the time of day, and convert it to a tm struct. */
gettimeofday (&tv, NULL);
ptm = localtime (&tv.tv_sec);
/* Format the date and time, down to a single second. */
strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm);
/* Compute milliseconds from microseconds. */
milliseconds = tv.tv_usec / 1000;
/* Print the formatted time, in seconds, followed by a decimal point
* and the milliseconds. */
printf ("%s.%03ld\n", time_string, milliseconds);
}
int main() {
while (1) {
ptime();
usleep(100000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment