Skip to content

Instantly share code, notes, and snippets.

@gonzopancho
Created November 22, 2015 09:41
Show Gist options
  • Save gonzopancho/be341ae7dcf24a4fe252 to your computer and use it in GitHub Desktop.
Save gonzopancho/be341ae7dcf24a4fe252 to your computer and use it in GitHub Desktop.
jim@x230:~ % sysctl kern.version
kern.version: FreeBSD 11.0-CURRENT #14 r289806: Fri Oct 23 04:55:45 CDT 2015
root@x230:/usr/obj/usr/src/sys/GENERIC-NODEBUG
jim@x230:~ % cat clock.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
clockid_t id = CLOCK_REALTIME;
struct timespec ts_now;
if (clock_gettime(id, &ts_now) < 0) {
fprintf(stderr, "clock_gettime: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
system("date");
system("uptime");
sleep(2);
struct timespec ts_then = ts_now;
ts_then.tv_sec -= 86400 * 16800;
if (clock_settime(id, &ts_then) < 0) {
fprintf(stderr, "clock_settime: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
system("date");
system("uptime");
if (clock_settime(id, &ts_now) < 0) {
fprintf(stderr, "clock_settime: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
system("date");
system("uptime");
}
jim@x230:~ % cc clock.c
jim@x230:~ % sudo ./a.out
Sun Nov 22 03:39:23 CST 2015
3:39AM up 19 days, 13:52, 8 users, load averages: 0.07, 0.09, 0.03
Sun Nov 23 03:39:23 CST 1969
3:39AM up 19 days, 13:52, 8 users, load averages: 0.07, 0.09, 0.03
Sun Nov 22 03:39:23 CST 2015
3:39AM up 19 days, 13:52, 8 users, load averages: 0.07, 0.09, 0.03
jim@x230:~ %
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment