User Tools

Site Tools


user:mtaft4:projects:hexclock

Hexadecimal Clock

  1. Start by using the date command with the %H, %M, %S options to get the current time in seconds.
  2. Next, take the current time and divide it by the total number of seconds in a day; 86,400 and representing it as a hexadecimal fraction.
  3. Taking the fraction by the first 4 digits shows the hours minutes and seconds in hexadecimal, the first digit being hours, the next two being minutes and the last being seconds

Stage 1 - Bash Code

#!/bin/bash
# hexclock - convert time to a hexadecimal format
hour=`date +%H`                                         # Get the current Hour (24 hour format)
minute=`date +%M`                                       # Get the current Minute
second=`date +%S`                                       # Get the current Second
totalsec=`echo "($hour*60+$minute)*60+$second" | bc`    # Convert Hour, Minute, Second to total seconds
htime="`echo "obase=16; $totalsec/86400" | bc -l`"      # Output the hextime as a hex fraction
htime=`echo $htime | cut -d'.' -f2 | cut --bytes=1-4`   # Format the fraction to not have . and only have first 4 bytes
hexhour=`echo $htime | cut --bytes=1`                   # Hex Hours = First byte of fraction
hexmin=`echo $htime | cut --bytes=2-3`                  # Hex Minutes = Second and Third bytes of fraction
hexsec=`echo $htime | cut --bytes=4`                    # Hex Seconds = Fourth byte of fraction
echo -n "The current time in hexadecimal is: "          # Nice little formatting message
echo $hexhour"_"$hexmin"_"$hexsec                       # Print results
exit

Stage 2 - C++ Code

#include <ctime>
#include <cstdio>
#include <iostream>
 
int main() {
 
	using namespace std;                                                //
	int hours, minutes, seconds, hhours, hmin1, hmin2, hseconds, htime; // Variable and Namespace setup
	int hminutes;                                                       //
	double totalsec, hexsec;
	time_t epoch_time;                                                  // Time is a time_t format
	struct tm *tm_p;
 
	epoch_time = time( NULL );                              //
	tm_p = localtime( &epoch_time );                        //   Time is extracted from Epoch time 
	printf("The time is %.2d:%.2d:%.2d\n",                  //   then converted and separated
	tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec );            //
 
	hours = tm_p->tm_hour;                                  //
	minutes = tm_p->tm_min;                                 // Quick variable declaration to keep from using the pointers
	seconds = tm_p->tm_sec;                                 // 
	totalsec = ( ( ( hours * 60 ) + minutes ) * 60 ) + seconds; // Conversion of time to total seconds
 
 
	hexsec = totalsec / 86400.00 * 65536;  //
	htime = static_cast<int>(hexsec);      //
	hhours = htime / 4096;                 //  Block of mystical mathematics
	htime = htime - (hhours * 4096);       //  to properly format the string
	hminutes = htime / 16;                 //  so it can be displayed
	hmin1 = hminutes / 16;                 //
	hmin2 = hminutes - (hmin1 * 16);       //
	hseconds = htime - (hminutes * 16);    //
 
	printf("The time in hexadecimal is: %x_%x%x_%x\n", hhours, hmin1, hmin2, hseconds); // Quick print to screen showing the current hexadecimal time
 
	return 0;
}

As you can see the C++ code is a massive jump in size from the previous bash script. And yet it seems to do precisely the same thing. This is because it took much more doing in order to get and format the time the way I wanted in order to even come remotely close to being able to convert it. Then I had to convert each piece so that I could actually format the text the right way in order to just make a command line clock.

Now that I have at least a code base to start from I can work on throwing it into SDL and getting it to either actually display the time in a window or a widget or something or get it to determine the position of the hands of a true clock face and do it that way. The math at least for it isn't that hard, divide the 360 degree circle into 16 and 256 parts for hours and minutes respectively and have it move that percentage of the degrees each time a minute or hour changes. With how close hex seconds are to regular seconds (about 3 hex seconds to 4 regular seconds is the ratio) and the fact that there are only 16 hex seconds in a minute it would be impractical to include seconds in a clock face example but digital clock would easily be able to include seconds.

I ended up choosing C++ because that from all my fiddling with SDL was what I needed to use in order to make it work. I don't know if that is truly the case or if it's merely the fact that I never saw any strictly C SDL tutorials. Either way I did make the decision to use the cstdio library along with the standard iostream library just in case I ended up needing to use either even though I'm more comfortable with C as far is output goes which is the only thing I really foresee needing any sort of I/O for as almost all of the interaction comes between the program and the system in trying to determine the time. The only I/O is at the end when the time is reported to the user, but planning for eventualities never hurt anyone.

user/mtaft4/projects/hexclock.txt · Last modified: 2010/12/16 19:21 by mtaft4