High Performance Compiled Date & Time Display
TextClock C++ is a compiled version of the popular TextClock script. Written in C++, it provides the same functionality as the Perl version but executes significantly faster with minimal server overhead. Perfect for high-traffic websites that need efficient date/time display.
The C++ version of TextClock offers significant performance improvements over the Perl version. As a compiled binary, it runs directly on your server without the overhead of starting a Perl interpreter for each request.
| Metric | Perl Version | C++ Version |
|---|---|---|
| Execution Time | ~50-100ms | ~1-5ms |
| Memory Usage | 2-5 MB | ~100 KB |
| CPU Overhead | Moderate | Minimal |
| Modification Ease | Easy (edit script) | Moderate (recompile) |
| File | Description |
|---|---|
textclock.cpp |
C++ source code |
Makefile |
Compilation instructions |
README |
Compilation and installation guide |
Executes 10-20x faster than Perl version. No interpreter startup overhead.
Minimal CPU consumption per request. Compiled binary runs natively.
Uses less than 200KB of memory compared to 2-5MB for Perl.
Supports all the same date/time format codes as the Perl version.
Drop-in replacement for Perl version. Same SSI syntax.
C++ type safety reduces runtime errors and crashes.
textclock.cpp and Makefile to your server.
g++ -o textclock textclock.cpp or simply make
chmod 755 textclock
http://yoursite.com/cgi-bin/textclock
<!--#exec cgi="/cgi-bin/textclock"-->
On some systems you may need to link with the math library: g++ -o textclock textclock.cpp -lm
#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
// Output HTTP header
cout << "Content-type: text/html\n\n";
// Get query string for format
char* query = getenv("QUERY_STRING");
char format[256] = "%A, %B %d, %Y - %I:%M:%S %p %Z";
if (query != NULL) {
// Parse format parameter from query string
char* format_param = strstr(query, "format=");
if (format_param != NULL) {
format_param += 7; // Skip "format="
// URL decode and copy format string
int i = 0;
while (*format_param && *format_param != '&' && i < 255) {
if (*format_param == '+') {
format[i++] = ' ';
} else if (*format_param == '%' &&
isxdigit(format_param[1]) &&
isxdigit(format_param[2])) {
char hex[3] = {format_param[1], format_param[2], 0};
format[i++] = (char)strtol(hex, NULL, 16);
format_param += 2;
} else {
format[i++] = *format_param;
}
format_param++;
}
format[i] = '\0';
}
}
// Get current time
time_t now = time(0);
struct tm* timeinfo = localtime(&now);
// Format and output time string
char buffer[512];
strftime(buffer, sizeof(buffer), format, timeinfo);
cout << buffer;
return 0;
}
CC = g++
CFLAGS = -O2 -Wall
TARGET = textclock
all: $(TARGET)
$(TARGET): textclock.cpp
$(CC) $(CFLAGS) -o $(TARGET) textclock.cpp
clean:
rm -f $(TARGET)
install: $(TARGET)
cp $(TARGET) /usr/local/apache/cgi-bin/
chmod 755 /usr/local/apache/cgi-bin/$(TARGET)
.PHONY: all clean install
C++ source code for compilation
Compilation automation
Compilation and installation guide