Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Last active March 21, 2019 11:06
Show Gist options
  • Save sebastianknopf/1e36735687c73754a089ff453161208d to your computer and use it in GitHub Desktop.
Save sebastianknopf/1e36735687c73754a089ff453161208d to your computer and use it in GitHub Desktop.
simple and effective way to store date / time in only 4 byte
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
short date_compress(int day, int month, int year) {
short d = ((((year % 100) << 4) + month) << 5) + day;
return d;
}
int* date_decompress(int cdate) {
static int d[3];
d[0] = cdate & 0x1f;
d[1] = (cdate >> 5) & 0xf;
d[2] = 2000 + (cdate >> (5 + 4));
return d;
}
short time_compress(int hour, int minute, int second) {
short t = (((second << 6) + minute) << 4) + hour;
return t;
}
int* time_decompress(int ct) {
static int t[3];
t[0] = ct & 0xf;
t[1] = (ct >> 4) & 0x3f;
t[2] = (ct >> 10) & 0x3f;
return t;
}
int date_time_compress(int day, int month, int year, int hour, int minute, int second) {
int dt = (date_compress(day, month, year) << 16) + time_compress(hour, minute, second);
return dt;
}
int* date_time_decompress(int dtc) {
int* dt = malloc(8 * sizeof(int));
memcpy(dt, date_decompress(dtc >> 16), 4 * sizeof(int));
memcpy(dt + 3, time_decompress(dtc), 4 * sizeof(int));
return dt;
}
int main()
{
int day = 21;
int month = 3;
int year = 2019;
int hour = 10;
int minute = 27;
int second = 24;
printf("Eingabedaten\n");
printf("Datum: %i/%i/%i\n", day, month, year);
printf("Uhrzeit: %i/%i/%i\n\n", hour, minute, second);
short date_compressed = date_compress(day, month, year);
printf("Datum Komprimiert:");
printf("%#06x\n", date_compressed);
int *date_decompressed = date_decompress(date_compressed);
printf("Datum Dekomprimiert:");
printf("%i/%i/%i\n\n",
*date_decompressed, // day
*(date_decompressed + 1), // month
*(date_decompressed + 2)); // year
short time_compressed = time_compress(hour, minute, second);
printf("Uhrzeit Komprimiert:");
printf("%#06x\n", time_compressed);
int *time_decompressed = time_decompress(time_compressed);
printf("Uhrzeit Dekomprimiert:");
printf("%i/%i/%i\n\n",
*time_decompressed, // hour
*(time_decompressed + 1), // minute
*(time_decompressed + 2)); // second
int date_time_compressed = date_time_compress(day, month, year, hour, minute, second);
printf("Datum/Uhrzeit Komprimiert:");
printf("%#06x\n", date_time_compressed);
int *date_time_decompressed = date_time_decompress(date_time_compressed);
printf("Datum/Uhrzeit Dekomprimiert:");
printf("%i/%i/%i %i/%i/%i\n\n",
*date_time_decompressed, // day
*(date_time_decompressed + 1), // month
*(date_time_decompressed + 2), // year
*(date_time_decompressed + 3), // hour
*(date_time_decompressed + 4), // minute
*(date_time_decompressed + 5)); // second
return 0;
}
<?php
function date_compress($day, $month, $year) {
$d = (((($year % 100) << 4) + $month) << 5) + $day;
return $d;
}
function date_decompress($cdate) {
$d = array();
array_push($d, $cdate & 0x1f);
array_push($d, ($cdate >> 5) & 0xf);
array_push($d, 2000 + ($cdate >> (5 + 4)));
return $d;
}
function time_compress($hour, $minute, $second) {
$t = ((($second << 6) + $minute) << 4) + $hour;
return $t;
}
function time_decompress($ct) {
$t = array();
array_push($t, $ct & 0xf);
array_push($t, ($ct >> 4) & 0x3f);
array_push($t, ($ct >> 10) & 0x3f);
return $t;
}
function date_time_compress($day, $month, $year, $hour, $minute, $second) {
$dt = (date_compress($day, $month, $year) << 16) + time_compress($hour, $minute, $second);
return $dt;
}
function date_time_decompress($dtc) {
$dt = array_merge(date_decompress($dtc >> 16), time_decompress($dtc));
return $dt;
}
$day = 21;
$month = 3;
$year = 2019;
$hour = 10;
$minute = 27;
$second = 24;
printf("Eingabedaten\n");
printf("Datum: %d/%d/%d\n", $day, $month, $year);
printf("Uhrzeit: %d/%d/%d\n\n", $hour, $minute, $second);
$date_compressed = date_compress($day, $month, $year);
printf("Datum Komprimiert:");
printf("0x" . dechex($date_compressed) . "\n");
$date_decompressed = date_decompress($date_compressed);
printf("Datum Dekomprimiert:");
printf("%d/%d/%d\n\n",
$date_decompressed[0], // day
$date_decompressed[1], // month
$date_decompressed[2]); // year
$time_compressed = time_compress($hour, $minute, $second);
printf("Uhrzeit Komprimiert:");
printf("0x" . dechex($time_compressed) . "\n");
$time_decompressed = time_decompress($time_compressed);
printf("Uhrzeit Dekomprimiert:");
printf("%d/%d/%d\n\n",
$time_decompressed[0], // hour
$time_decompressed[1], // minute
$time_decompressed[2]); // second
$date_time_compressed = date_time_compress($day, $month, $year, $hour, $minute, $second);
printf("Datum/Uhrzeit Komprimiert:");
printf("0x" . dechex($date_time_compressed) . "\n");
$date_time_decompressed = date_time_decompress($date_time_compressed);
printf("Datum/Uhrzeit Dekomprimiert:");
printf("%d/%d/%d %d/%d/%d\n\n",
$date_time_decompressed[0], // day
$date_time_decompressed[1], // month
$date_time_decompressed[2], // year
$date_time_decompressed[3], // hour
$date_time_decompressed[4], // minute
$date_time_decompressed[5]); // second
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment