shrub/v/time.c

187 lines
3.5 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* v/time.c
**
** This file is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <setjmp.h>
#include <gmp.h>
#include <dirent.h>
#include <stdint.h>
#include <uv.h>
#include <curses.h>
#include <termios.h>
#include <term.h>
#include "all.h"
#include "v/vere.h"
2014-09-11 04:01:32 +04:00
/* u3_time_sec_in(): urbit seconds from unix time.
2013-09-29 00:21:18 +04:00
**
** Adjust for future leap secs!
*/
c3_d
2014-09-11 04:01:32 +04:00
u3_time_sec_in(c3_w unx_w)
2013-09-29 00:21:18 +04:00
{
return 0x8000000cce9e0d80ULL + (c3_d)unx_w;
}
2014-09-11 04:01:32 +04:00
/* u3_time_sec_out(): unix time from urbit seconds.
2013-09-29 00:21:18 +04:00
**
** Adjust for future leap secs!
*/
c3_w
2014-09-11 04:01:32 +04:00
u3_time_sec_out(c3_d urs_d)
2013-09-29 00:21:18 +04:00
{
c3_d adj_d = (urs_d - 0x8000000cce9e0d80ULL);
2013-09-29 00:21:18 +04:00
if ( adj_d > 0xffffffffULL ) {
fprintf(stderr, "Agh! It's 2106! And no one's fixed this shite!\n");
exit(1);
}
return (c3_w)adj_d;
}
2014-09-11 04:01:32 +04:00
/* u3_time_fsc_in(): urbit fracto-seconds from unix microseconds.
2013-09-29 00:21:18 +04:00
*/
c3_d
2014-09-11 04:01:32 +04:00
u3_time_fsc_in(c3_w usc_w)
2013-09-29 00:21:18 +04:00
{
c3_d usc_d = usc_w;
2013-09-29 00:21:18 +04:00
return ((usc_d * 65536ULL) / 1000000ULL) << 48ULL;
}
2014-09-11 04:01:32 +04:00
/* u3_time_fsc_out: unix microseconds from urbit fracto-seconds.
2013-09-29 00:21:18 +04:00
*/
c3_w
2014-09-11 04:01:32 +04:00
u3_time_fsc_out(c3_d ufc_d)
2013-09-29 00:21:18 +04:00
{
return (c3_w) (((ufc_d >> 48ULL) * 1000000ULL) / 65536ULL);
}
2014-09-11 04:01:32 +04:00
/* u3_time_msc_out: unix microseconds from urbit fracto-seconds.
2013-09-29 00:21:18 +04:00
*/
c3_w
2014-09-11 04:01:32 +04:00
u3_time_msc_out(c3_d ufc_d)
2013-09-29 00:21:18 +04:00
{
return (c3_w) (((ufc_d >> 48ULL) * 1000ULL) / 65536ULL);
}
2014-09-11 04:01:32 +04:00
/* u3_time_in_tv(): urbit time from struct timeval.
2013-09-29 00:21:18 +04:00
*/
2014-09-11 04:01:32 +04:00
u3_atom
u3_time_in_tv(struct timeval* tim_tv)
2013-09-29 00:21:18 +04:00
{
c3_w unx_w = tim_tv->tv_sec;
c3_w usc_w = tim_tv->tv_usec;
c3_d cub_d[2];
2014-09-11 04:01:32 +04:00
cub_d[0] = u3_time_fsc_in(usc_w);
cub_d[1] = u3_time_sec_in(unx_w);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
return u3i_chubs(2, cub_d);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_time_out_tv(): struct timeval from urbit time.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_time_out_tv(struct timeval* tim_tv, u3_noun now)
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
c3_d ufc_d = u3r_chub(0, now);
c3_d urs_d = u3r_chub(1, now);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
tim_tv->tv_sec = u3_time_sec_out(urs_d);
tim_tv->tv_usec = u3_time_fsc_out(ufc_d);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(now);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_time_in_ts(): urbit time from struct timespec.
2013-09-29 00:21:18 +04:00
*/
2014-09-11 04:01:32 +04:00
u3_atom
u3_time_in_ts(struct timespec* tim_ts)
2013-09-29 00:21:18 +04:00
{
struct timeval tim_tv;
tim_tv.tv_sec = tim_ts->tv_sec;
tim_tv.tv_usec = (tim_ts->tv_nsec / 1000);
2014-09-11 04:01:32 +04:00
return u3_time_in_tv(&tim_tv);
2013-09-29 00:21:18 +04:00
}
#if defined(U3_OS_linux)
2014-09-11 04:01:32 +04:00
/* u3_time_t_in_ts(): urbit time from time_t.
2013-09-29 00:21:18 +04:00
*/
2014-09-11 04:01:32 +04:00
u3_atom
u3_time_t_in_ts(time_t tim)
2013-09-29 00:21:18 +04:00
{
struct timeval tim_tv;
tim_tv.tv_sec = tim;
tim_tv.tv_usec = 0;
2014-09-11 04:01:32 +04:00
return u3_time_in_tv(&tim_tv);
2013-09-29 00:21:18 +04:00
}
#endif // defined(U3_OS_linux)
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
/* u3_time_out_ts(): struct timespec from urbit time.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_time_out_ts(struct timespec* tim_ts, u3_noun now)
2013-09-29 00:21:18 +04:00
{
struct timeval tim_tv;
2014-09-11 04:01:32 +04:00
u3_time_out_tv(&tim_tv, now);
2013-09-29 00:21:18 +04:00
tim_ts->tv_sec = tim_tv.tv_sec;
tim_ts->tv_nsec = (tim_tv.tv_usec * 1000);
}
2014-09-11 04:01:32 +04:00
/* u3_time_gap_ms(): (wen - now) in ms.
2013-09-29 00:21:18 +04:00
*/
c3_d
2014-09-11 04:01:32 +04:00
u3_time_gap_ms(u3_noun now, u3_noun wen)
2013-09-29 00:21:18 +04:00
{
2014-11-06 22:13:57 +03:00
if ( c3n == u3ka_gth(u3k(wen), u3k(now)) ) {
2014-09-11 04:01:32 +04:00
u3z(wen); u3z(now);
2013-09-29 00:21:18 +04:00
return 0ULL;
}
2013-09-29 00:21:18 +04:00
else {
2014-11-06 22:13:57 +03:00
u3_noun dif = u3ka_sub(wen, now);
2014-11-06 03:20:01 +03:00
c3_d fsc_d = u3r_chub(0, dif);
c3_d sec_d = u3r_chub(1, dif);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(dif);
return (sec_d * 1000ULL) + u3_time_msc_out(fsc_d);
2013-09-29 00:21:18 +04:00
}
}
2014-09-11 04:01:32 +04:00
/* u3_time_gap_double(): (wen - now) in libev resolution.
2013-09-29 00:21:18 +04:00
*/
double
2014-09-11 04:01:32 +04:00
u3_time_gap_double(u3_noun now, u3_noun wen)
2013-09-29 00:21:18 +04:00
{
mpz_t now_mp, wen_mp, dif_mp;
double sec_g = (((double)(1ULL << 32ULL)) * ((double)(1ULL << 32ULL)));
double gap_g, dif_g;
2014-11-06 03:20:01 +03:00
u3r_mp(now_mp, now);
u3r_mp(wen_mp, wen);
2013-09-29 00:21:18 +04:00
mpz_init(dif_mp);
mpz_sub(dif_mp, wen_mp, now_mp);
2014-09-11 04:01:32 +04:00
u3z(now);
u3z(wen);
2013-09-29 00:21:18 +04:00
dif_g = mpz_get_d(dif_mp) / sec_g;
gap_g = (dif_g > 0.0) ? dif_g : 0.0;
mpz_clear(dif_mp); mpz_clear(wen_mp); mpz_clear(now_mp);
return gap_g;
}