Skip to content

Commit

Permalink
dhms et al., resolving #35
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Aug 30, 2015
1 parent df48440 commit 320e214
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 22 deletions.
16 changes: 8 additions & 8 deletions c/mapping/lrec_evaluators.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,14 +857,14 @@ static function_lookup_t FUNCTION_LOOKUP_TABLE[] = {
{ FUNC_CLASS_TIME, "systime", 0 , "Floating-point seconds since the epoch, e.g. 1440768801.748936." },
{ FUNC_CLASS_TIME, "sec2gmt", 1 , "Formats seconds since epoch (integer part only) as GMT timestamp, e.g. sec2gmt(1440768801.7) = \"2015-08-28T13:33:21Z\"."},
{ FUNC_CLASS_TIME, "gmt2sec", 1 , "Parses GMT timestamp as integer seconds since epoch."},
{ FUNC_CLASS_TIME, "sec2hms", 1 , "EXPERIMENTAL Formats integer seconds as in sec2hms(5000) = \"01:23:20\""},
{ FUNC_CLASS_TIME, "sec2dhms", 1 , "EXPERIMENTAL Formats integer seconds as in sec2dhms(500000) = \"5d18h53m20s\""},
{ FUNC_CLASS_TIME, "hms2sec", 1 , "EXPERIMENTAL Recovers integer seconds as in hms2sec(\"01:23:20\") = 5000"},
{ FUNC_CLASS_TIME, "dhms2sec", 1 , "EXPERIMENTAL Recovers integer seconds as in dhms2sec(\"5d18h53m20s\") = 500000"},
{ FUNC_CLASS_TIME, "fsec2hms", 1 , "EXPERIMENTAL Formats floating-point seconds as in fsec2hms(5000.25) = \"01:23:20.250000\""},
{ FUNC_CLASS_TIME, "fsec2dhms", 1 , "EXPERIMENTAL Formats floating-point seconds as in fsec2dhms(500000.25) = \"5d18h53m20.250000s\""},
{ FUNC_CLASS_TIME, "hms2fsec", 1 , "EXPERIMENTAL Recovers floating-point seconds as in hms2fsec(\"01:23:20.250000\") = 5000.250000"},
{ FUNC_CLASS_TIME, "dhms2fsec", 1 , "EXPERIMENTAL Recovers floating-point seconds as in dhms2fsec(\"5d18h53m20.250000s\") = 500000.250000"},
{ FUNC_CLASS_TIME, "sec2hms", 1 , "Formats integer seconds as in sec2hms(5000) = \"01:23:20\""},
{ FUNC_CLASS_TIME, "sec2dhms", 1 , "Formats integer seconds as in sec2dhms(500000) = \"5d18h53m20s\""},
{ FUNC_CLASS_TIME, "hms2sec", 1 , "Recovers integer seconds as in hms2sec(\"01:23:20\") = 5000"},
{ FUNC_CLASS_TIME, "dhms2sec", 1 , "Recovers integer seconds as in dhms2sec(\"5d18h53m20s\") = 500000"},
{ FUNC_CLASS_TIME, "fsec2hms", 1 , "Formats floating-point seconds as in fsec2hms(5000.25) = \"01:23:20.250000\""},
{ FUNC_CLASS_TIME, "fsec2dhms", 1 , "Formats floating-point seconds as in fsec2dhms(500000.25) = \"5d18h53m20.250000s\""},
{ FUNC_CLASS_TIME, "hms2fsec", 1 , "Recovers floating-point seconds as in hms2fsec(\"01:23:20.250000\") = 5000.250000"},
{ FUNC_CLASS_TIME, "dhms2fsec", 1 , "Recovers floating-point seconds as in dhms2fsec(\"5d18h53m20.250000s\") = 500000.250000"},

{ 0, NULL, -1 , NULL}, // table terminator
};
Expand Down
32 changes: 22 additions & 10 deletions c/mapping/mlr_val.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,36 +472,48 @@ mv_t f_s_hms2fsec_func(mv_t* pval1) {
mv_t i_s_dhms2sec_func(mv_t* pval1) {
long long d = 0LL, h = 0LL, m = 0LL, s = 0LL;
long long sec = 0LL;
if (sscanf(pval1->u.strv, "%lldd%lldh%lldm%llds", &d, &h, &m, &s) == 4) {
char* p = pval1->u.strv;
long long sign = 1LL;
if (*p == '-') {
p++;
sign = -1LL;
}
if (sscanf(p, "%lldd%lldh%lldm%llds", &d, &h, &m, &s) == 4) {
sec = 86400*d + 3600*h + 60*m + s;
} else if (sscanf(pval1->u.strv, "%lldh%lldm%llds", &h, &m, &s) == 3) {
} else if (sscanf(p, "%lldh%lldm%llds", &h, &m, &s) == 3) {
sec = 3600*h + 60*m + s;
} else if (sscanf(pval1->u.strv, "%lldm%llds", &m, &s) == 2) {
} else if (sscanf(p, "%lldm%llds", &m, &s) == 2) {
sec = 60*m + s;
} else if (sscanf(pval1->u.strv, "%llds", &s) == 1) {
} else if (sscanf(p, "%llds", &s) == 1) {
sec = s;
} else {
return MV_ERROR;
}
return (mv_t) {.type = MT_INT, .u.intv = sec};
return (mv_t) {.type = MT_INT, .u.intv = sec * sign};
}

mv_t f_s_dhms2fsec_func(mv_t* pval1) {
long long d = 0LL, h = 0LL, m = 0LL;
double s = 0.0;
double sec = 0.0;
if (sscanf(pval1->u.strv, "%lldd%lldh%lldm%lfs", &d, &h, &m, &s) == 4) {
char* p = pval1->u.strv;
long long sign = 1LL;
if (*p == '-') {
p++;
sign = -1LL;
}
if (sscanf(p, "%lldd%lldh%lldm%lfs", &d, &h, &m, &s) == 4) {
sec = 86400*d + 3600*h + 60*m + s;
} else if (sscanf(pval1->u.strv, "%lldh%lldm%lfs", &h, &m, &s) == 3) {
} else if (sscanf(p, "%lldh%lldm%lfs", &h, &m, &s) == 3) {
sec = 3600*h + 60*m + s;
} else if (sscanf(pval1->u.strv, "%lldm%lfs", &m, &s) == 2) {
} else if (sscanf(p, "%lldm%lfs", &m, &s) == 2) {
sec = 60*m + s;
} else if (sscanf(pval1->u.strv, "%lfs", &s) == 1) {
} else if (sscanf(p, "%lfs", &s) == 1) {
sec = s;
} else {
return MV_ERROR;
}
return (mv_t) {.type = MT_DOUBLE, .u.dblv = sec};
return (mv_t) {.type = MT_DOUBLE, .u.dblv = sec * sign};
}

// ----------------------------------------------------------------
Expand Down
122 changes: 122 additions & 0 deletions c/test/expected/out
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,128 @@ x=3,y=,z=3.000000
x=,y=,z=
x=,y=2,z=2.000000

./test/../mlr --opprint put $hms=sec2hms($sec); $resec=hms2sec($hms); $diff=$resec-$sec ./test/input/sec2xhms
sec hms resec diff
0 00:00:00 0 0.000000
1 00:00:01 1 0.000000
59 00:00:59 59 0.000000
60 00:01:00 60 0.000000
61 00:01:01 61 0.000000
3599 00:59:59 3599 0.000000
3600 01:00:00 3600 0.000000
3601 01:00:01 3601 0.000000
86399 23:59:59 86399 0.000000
86400 24:00:00 86400 0.000000
86401 24:00:01 86401 0.000000
863999 239:59:59 863999 0.000000
864000 240:00:00 864000 0.000000
864001 240:00:01 864001 0.000000
-1 -00:00:01 -1 0.000000
-59 -00:00:59 -59 0.000000
-60 -00:01:00 -60 0.000000
-61 -00:01:01 -61 0.000000
-3599 -00:59:59 -3599 0.000000
-3600 -01:00:00 -3600 0.000000
-3601 -01:00:01 -3601 0.000000
-86399 -23:59:59 -86399 0.000000
-86400 -24:00:00 -86400 0.000000
-86401 -24:00:01 -86401 0.000000
-863999 -239:59:59 -863999 0.000000
-864000 -240:00:00 -864000 0.000000
-864001 -240:00:01 -864001 0.000000

./test/../mlr --opprint put $hms=fsec2hms($sec); $resec=hms2fsec($hms); $diff=$resec-$sec ./test/input/fsec2xhms
sec hms resec diff
0.25 0:00:00.250000 0.250000 0.000000
1.25 0:00:01.250000 1.250000 0.000000
59.25 0:00:59.250000 59.250000 0.000000
60.25 0:01:00.250000 60.250000 0.000000
61.25 0:01:01.250000 61.250000 0.000000
3599.25 0:59:59.250000 3599.250000 0.000000
3600.25 1:00:00.250000 3600.250000 0.000000
3601.25 1:00:01.250000 3601.250000 0.000000
86399.25 23:59:59.250000 86399.250000 0.000000
86400.25 24:00:00.250000 86400.250000 0.000000
86401.25 24:00:01.250000 86401.250000 0.000000
863999.25 239:59:59.250000 863999.250000 0.000000
864000.25 240:00:00.250000 864000.250000 0.000000
864001.25 240:00:01.250000 864001.250000 0.000000
-0.25 -00:00:00.250000 -0.250000 0.000000
-1.25 -00:00:01.250000 -1.250000 0.000000
-59.25 -00:00:59.250000 -59.250000 0.000000
-60.25 -00:01:00.250000 -60.250000 0.000000
-61.25 -00:01:01.250000 -61.250000 0.000000
-3599.25 -00:59:59.250000 -3599.250000 0.000000
-3600.25 -01:00:00.250000 -3600.250000 0.000000
-3601.25 -01:00:01.250000 -3601.250000 0.000000
-86399.25 -23:59:59.250000 -86399.250000 0.000000
-86400.25 -24:00:00.250000 -86400.250000 0.000000
-86401.25 -24:00:01.250000 -86401.250000 0.000000
-863999.25 -239:59:59.250000 -863999.250000 0.000000
-864000.25 -240:00:00.250000 -864000.250000 0.000000
-864001.25 -240:00:01.250000 -864001.250000 0.000000

./test/../mlr --opprint put $hms=sec2dhms($sec); $resec=dhms2sec($hms); $diff=$resec-$sec ./test/input/sec2xhms
sec hms resec diff
0 0s 0 0.000000
1 1s 1 0.000000
59 59s 59 0.000000
60 1m00s 60 0.000000
61 1m01s 61 0.000000
3599 59m59s 3599 0.000000
3600 1h00m00s 3600 0.000000
3601 1h00m01s 3601 0.000000
86399 23h59m59s 86399 0.000000
86400 1d00h00m00s 86400 0.000000
86401 1d00h00m01s 86401 0.000000
863999 9d23h59m59s 863999 0.000000
864000 10d00h00m00s 864000 0.000000
864001 10d00h00m01s 864001 0.000000
-1 -1s -1 0.000000
-59 -59s -59 0.000000
-60 -1m00s -60 0.000000
-61 -1m01s -61 0.000000
-3599 -59m59s -3599 0.000000
-3600 -1h00m00s -3600 0.000000
-3601 -1h00m01s -3601 0.000000
-86399 -23h59m59s -86399 0.000000
-86400 -1d00h00m00s -86400 0.000000
-86401 -1d00h00m01s -86401 0.000000
-863999 -9d23h59m59s -863999 0.000000
-864000 -10d00h00m00s -864000 0.000000
-864001 -10d00h00m01s -864001 0.000000

./test/../mlr --opprint put $hms=fsec2dhms($sec); $resec=dhms2fsec($hms); $diff=$resec-$sec ./test/input/fsec2xhms
sec hms resec diff
0.25 0.250000s 0.250000 0.000000
1.25 1.250000s 1.250000 0.000000
59.25 59.250000s 59.250000 0.000000
60.25 1m00.250000s 60.250000 0.000000
61.25 1m01.250000s 61.250000 0.000000
3599.25 59m59.250000s 3599.250000 0.000000
3600.25 1h00m00.250000s 3600.250000 0.000000
3601.25 1h00m01.250000s 3601.250000 0.000000
86399.25 23h59m59.250000s 86399.250000 0.000000
86400.25 1d00h00m00.250000s 86400.250000 0.000000
86401.25 1d00h00m01.250000s 86401.250000 0.000000
863999.25 9d23h59m59.250000s 863999.250000 0.000000
864000.25 10d00h00m00.250000s 864000.250000 0.000000
864001.25 10d00h00m01.250000s 864001.250000 0.000000
-0.25 -0.250000s -0.250000 0.000000
-1.25 -1.250000s -1.250000 0.000000
-59.25 -59.250000s -59.250000 0.000000
-60.25 -1m00.250000s -60.250000 0.000000
-61.25 -1m01.250000s -61.250000 0.000000
-3599.25 -59m59.250000s -3599.250000 0.000000
-3600.25 -1h00m00.250000s -3600.250000 0.000000
-3601.25 -1h00m01.250000s -3601.250000 0.000000
-86399.25 -23h59m59.250000s -86399.250000 0.000000
-86400.25 -1d00h00m00.250000s -86400.250000 0.000000
-86401.25 -1d00h00m01.250000s -86401.250000 0.000000
-863999.25 -9d23h59m59.250000s -863999.250000 0.000000
-864000.25 -10d00h00m00.250000s -864000.250000 0.000000
-864001.25 -10d00h00m01.250000s -864001.250000 0.000000


================================================================
CHAINING
Expand Down
122 changes: 122 additions & 0 deletions c/test/output/out
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,128 @@ x=3,y=,z=3.000000
x=,y=,z=
x=,y=2,z=2.000000

./test/../mlr --opprint put $hms=sec2hms($sec); $resec=hms2sec($hms); $diff=$resec-$sec ./test/input/sec2xhms
sec hms resec diff
0 00:00:00 0 0.000000
1 00:00:01 1 0.000000
59 00:00:59 59 0.000000
60 00:01:00 60 0.000000
61 00:01:01 61 0.000000
3599 00:59:59 3599 0.000000
3600 01:00:00 3600 0.000000
3601 01:00:01 3601 0.000000
86399 23:59:59 86399 0.000000
86400 24:00:00 86400 0.000000
86401 24:00:01 86401 0.000000
863999 239:59:59 863999 0.000000
864000 240:00:00 864000 0.000000
864001 240:00:01 864001 0.000000
-1 -00:00:01 -1 0.000000
-59 -00:00:59 -59 0.000000
-60 -00:01:00 -60 0.000000
-61 -00:01:01 -61 0.000000
-3599 -00:59:59 -3599 0.000000
-3600 -01:00:00 -3600 0.000000
-3601 -01:00:01 -3601 0.000000
-86399 -23:59:59 -86399 0.000000
-86400 -24:00:00 -86400 0.000000
-86401 -24:00:01 -86401 0.000000
-863999 -239:59:59 -863999 0.000000
-864000 -240:00:00 -864000 0.000000
-864001 -240:00:01 -864001 0.000000

./test/../mlr --opprint put $hms=fsec2hms($sec); $resec=hms2fsec($hms); $diff=$resec-$sec ./test/input/fsec2xhms
sec hms resec diff
0.25 0:00:00.250000 0.250000 0.000000
1.25 0:00:01.250000 1.250000 0.000000
59.25 0:00:59.250000 59.250000 0.000000
60.25 0:01:00.250000 60.250000 0.000000
61.25 0:01:01.250000 61.250000 0.000000
3599.25 0:59:59.250000 3599.250000 0.000000
3600.25 1:00:00.250000 3600.250000 0.000000
3601.25 1:00:01.250000 3601.250000 0.000000
86399.25 23:59:59.250000 86399.250000 0.000000
86400.25 24:00:00.250000 86400.250000 0.000000
86401.25 24:00:01.250000 86401.250000 0.000000
863999.25 239:59:59.250000 863999.250000 0.000000
864000.25 240:00:00.250000 864000.250000 0.000000
864001.25 240:00:01.250000 864001.250000 0.000000
-0.25 -00:00:00.250000 -0.250000 0.000000
-1.25 -00:00:01.250000 -1.250000 0.000000
-59.25 -00:00:59.250000 -59.250000 0.000000
-60.25 -00:01:00.250000 -60.250000 0.000000
-61.25 -00:01:01.250000 -61.250000 0.000000
-3599.25 -00:59:59.250000 -3599.250000 0.000000
-3600.25 -01:00:00.250000 -3600.250000 0.000000
-3601.25 -01:00:01.250000 -3601.250000 0.000000
-86399.25 -23:59:59.250000 -86399.250000 0.000000
-86400.25 -24:00:00.250000 -86400.250000 0.000000
-86401.25 -24:00:01.250000 -86401.250000 0.000000
-863999.25 -239:59:59.250000 -863999.250000 0.000000
-864000.25 -240:00:00.250000 -864000.250000 0.000000
-864001.25 -240:00:01.250000 -864001.250000 0.000000

./test/../mlr --opprint put $hms=sec2dhms($sec); $resec=dhms2sec($hms); $diff=$resec-$sec ./test/input/sec2xhms
sec hms resec diff
0 0s 0 0.000000
1 1s 1 0.000000
59 59s 59 0.000000
60 1m00s 60 0.000000
61 1m01s 61 0.000000
3599 59m59s 3599 0.000000
3600 1h00m00s 3600 0.000000
3601 1h00m01s 3601 0.000000
86399 23h59m59s 86399 0.000000
86400 1d00h00m00s 86400 0.000000
86401 1d00h00m01s 86401 0.000000
863999 9d23h59m59s 863999 0.000000
864000 10d00h00m00s 864000 0.000000
864001 10d00h00m01s 864001 0.000000
-1 -1s -1 0.000000
-59 -59s -59 0.000000
-60 -1m00s -60 0.000000
-61 -1m01s -61 0.000000
-3599 -59m59s -3599 0.000000
-3600 -1h00m00s -3600 0.000000
-3601 -1h00m01s -3601 0.000000
-86399 -23h59m59s -86399 0.000000
-86400 -1d00h00m00s -86400 0.000000
-86401 -1d00h00m01s -86401 0.000000
-863999 -9d23h59m59s -863999 0.000000
-864000 -10d00h00m00s -864000 0.000000
-864001 -10d00h00m01s -864001 0.000000

./test/../mlr --opprint put $hms=fsec2dhms($sec); $resec=dhms2fsec($hms); $diff=$resec-$sec ./test/input/fsec2xhms
sec hms resec diff
0.25 0.250000s 0.250000 0.000000
1.25 1.250000s 1.250000 0.000000
59.25 59.250000s 59.250000 0.000000
60.25 1m00.250000s 60.250000 0.000000
61.25 1m01.250000s 61.250000 0.000000
3599.25 59m59.250000s 3599.250000 0.000000
3600.25 1h00m00.250000s 3600.250000 0.000000
3601.25 1h00m01.250000s 3601.250000 0.000000
86399.25 23h59m59.250000s 86399.250000 0.000000
86400.25 1d00h00m00.250000s 86400.250000 0.000000
86401.25 1d00h00m01.250000s 86401.250000 0.000000
863999.25 9d23h59m59.250000s 863999.250000 0.000000
864000.25 10d00h00m00.250000s 864000.250000 0.000000
864001.25 10d00h00m01.250000s 864001.250000 0.000000
-0.25 -0.250000s -0.250000 0.000000
-1.25 -1.250000s -1.250000 0.000000
-59.25 -59.250000s -59.250000 0.000000
-60.25 -1m00.250000s -60.250000 0.000000
-61.25 -1m01.250000s -61.250000 0.000000
-3599.25 -59m59.250000s -3599.250000 0.000000
-3600.25 -1h00m00.250000s -3600.250000 0.000000
-3601.25 -1h00m01.250000s -3601.250000 0.000000
-86399.25 -23h59m59.250000s -86399.250000 0.000000
-86400.25 -1d00h00m00.250000s -86400.250000 0.000000
-86401.25 -1d00h00m01.250000s -86401.250000 0.000000
-863999.25 -9d23h59m59.250000s -863999.250000 0.000000
-864000.25 -10d00h00m00.250000s -864000.250000 0.000000
-864001.25 -10d00h00m01.250000s -864001.250000 0.000000


================================================================
CHAINING
Expand Down
8 changes: 4 additions & 4 deletions c/test/run
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ run_command $mlr --csvlite put '$sec=gmt2sec($gmt)' $indir/gmt2sec
run_command $mlr put '$z=min($x, $y)' $indir/minmax.dkvp
run_command $mlr put '$z=max($x, $y)' $indir/minmax.dkvp

#run_command $mlr --opprint put '$hms=sec2hms($sec); $resec=hms2sec($hms); $diff=$resec-$sec' $indir/sec2xhms
#run_command $mlr --opprint put '$hms=fsec2hms($sec); $resec=hms2fsec($hms); $diff=$resec-$sec' $indir/fsec2xhms
#run_command $mlr --opprint put '$hms=sec2dhms($sec); $resec=dhms2sec($hms); $diff=$resec-$sec' $indir/sec2xhms
#run_command $mlr --opprint put '$hms=fsec2dhms($sec); $resec=dhms2fsec($hms); $diff=$resec-$sec' $indir/fsec2xhms
run_command $mlr --opprint put '$hms=sec2hms($sec); $resec=hms2sec($hms); $diff=$resec-$sec' $indir/sec2xhms
run_command $mlr --opprint put '$hms=fsec2hms($sec); $resec=hms2fsec($hms); $diff=$resec-$sec' $indir/fsec2xhms
run_command $mlr --opprint put '$hms=sec2dhms($sec); $resec=dhms2sec($hms); $diff=$resec-$sec' $indir/sec2xhms
run_command $mlr --opprint put '$hms=fsec2dhms($sec); $resec=dhms2fsec($hms); $diff=$resec-$sec' $indir/fsec2xhms

# ----------------------------------------------------------------
announce CHAINING
Expand Down

0 comments on commit 320e214

Please sign in to comment.