Add support for the swatch dot-beat internet time ##print

* Available via `date -b`
* As well as another date format via `ptb`
This commit is contained in:
pancake 2022-11-14 12:01:29 +01:00 committed by GitHub
parent 29b5cbbf61
commit 1e23b9e04b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 3 deletions

View File

@ -5176,9 +5176,19 @@ static int cmd_debug(void *data, const char *input) {
return 0;
}
if (!strncmp (input, "ate", 3)) { // "date" -- same as pt.
char *nostr = r_time_stamp_to_str (time (0));
r_cons_println (nostr);
free (nostr);
if (strstr (input, "-h") || strstr (input, "?")) {
eprintf ("Usage: date [-b] # use -b for beat time\n");
return 0;
}
bool use_beat = strstr (input, "-b");
if (use_beat) {
int beats = r_time_beats (r_time_now (), NULL);
r_cons_printf ("@%03d\n", beats);
} else {
char *nostr = r_time_stamp_to_str (time (0));
r_cons_println (nostr);
free (nostr);
}
return 0;
}

View File

@ -517,6 +517,7 @@ static const char *help_msg_pt[] = {
"Usage: pt", "[dn]", "print timestamps",
"pt.", "", "print current time",
"pt", "", "print UNIX time (32 bit `cfg.bigendian`) Since January 1, 1970",
"ptb", "", "print BEAT time (Swatch Internet Time)",
"ptd", "", "print DOS time (32 bit `cfg.bigendian`) Since January 1, 1980",
"pth", "", "print HFS time (32 bit `cfg.bigendian`) Since January 1, 1904",
"ptn", "", "print NTFS time (64 bit `cfg.bigendian`) Since January 1, 1601",
@ -8016,6 +8017,19 @@ static int cmd_print(void *data, const char *input) {
r_print_date_hfs (core->print, block + l, sizeof (ut32));
}
break;
case 'b': // "ptb"
if (len < sizeof (ut32)) {
R_LOG_WARN ("Change the block size: b %d", (int) sizeof (ut32));
}
if (len % sizeof (ut32)) {
len = len - (len % sizeof (ut32));
}
for (l = 0; l < len; l += sizeof (ut64)) {
ut64 ts = r_read_le64 (block + l);
int beats = r_time_beats (ts, NULL);
r_cons_printf ("@%03d\n", beats);
}
break;
case 'd': // "ptd"
// len must be multiple of 4 since r_print_date_dos read buf+3
// if block size is 1 or 5 for example it reads beyond the buffer

View File

@ -25,6 +25,7 @@ R_API ut64 r_time_now_mono(void);
R_API R_MUSTUSE char *r_time_stamp_to_str(time_t timeStamp);
R_API ut32 r_time_dos_time_stamp_to_posix(ut32 timeStamp);
R_API bool r_time_stamp_is_dos_format(const ut32 certainPosixTimeStamp, const ut32 possiblePosixOrDosTimeStamp);
R_API int r_time_beats(ut64 ts, int *sub);
R_API char *r_time_tostring(ut64 ts);
// Cross platform thread-safe time functions

View File

@ -207,3 +207,34 @@ R_API char *r_ctime_r(const time_t *timer, char *buf) {
return ctime_r (timer, buf);
#endif
}
static int get_time_correction(void) {
#if __UNIX__
struct my_timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
} tz;
struct timeval tv;
gettimeofday (&tv, (void*) &tz);
return (int) (tz.tz_minuteswest * 60); // in seconds
#else
#prama message("warning BEAT time may not correct for this platform")
return (60*60); // hardcoded gmt+1
#endif
}
R_API int r_time_beats(ut64 ts, int *sub) {
if (sub) {
// R_WARN_LOG ("sub-beats not implemented yet");
}
ut64 seconds = ts / (1000 * 1000);
int time_correction = get_time_correction ();
seconds -= time_correction;
seconds %= 86400;
ut64 beats = ((seconds / 86.4) * 100) / 100;
if (beats >= 1000) {
return R_ABS (beats - 1000);
}
return beats;
}