Skip to content

Instantly share code, notes, and snippets.

@insom
Created June 3, 2015 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/bf40b91fd25ae1d84764 to your computer and use it in GitHub Desktop.
Save insom/bf40b91fd25ae1d84764 to your computer and use it in GitHub Desktop.
Swatch Internet Time patched into strftime

Usage

Compile beat.so as above, then add it to any program with LD_PRELOAD=<absolute-path-to-so-file>, like:

LD_PRELOAD=/home/insom/beat.so irc

(And in irssi just issue /SET TIMESTAMP_FORMAT {BEAT} to switch your status bar and timestamp entries to the One True Internet Time).

#define _GNU_SOURCE
#include <sys/types.h>
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
static size_t (*real_strftime)(char *s, size_t max, const char *format, const struct tm *tm) = NULL;
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm) {
char result[5] = {0};
real_strftime = dlsym(RTLD_NEXT, "strftime");
time_t t = mktime((struct tm *)tm);
t = t + 3600; // Add an hour to make into BMT
struct tm *bt = gmtime(&t);
double sex = (bt->tm_hour * 3600) + (bt->tm_min * 60) + bt->tm_sec;
int beats = (int)(10 * (sex / 864)) % 1000;
sprintf(result, "@%.3d", beats);
char *new_format = strdup(format);
char *dest = strstr(new_format, "BEAT");
if(dest != NULL) {
strncpy(dest, result, 4);
}
size_t rv = real_strftime(s, max, new_format, tm);
free(new_format);
return rv;
}
#!/bin/sh
gcc -shared -fPIC beat.c -ldl -o beat.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment