import Long from "long";
/**
 * @param duration nanoseconds
 */
export function toDuration(duration) {
  return {
    seconds: Long.fromNumber(Math.floor(parseInt(duration) / 1000000000)),
    nanos: parseInt(duration) % 1000000000
  };
}

/**
 * @return nanoseconds
 */
export function fromDuration(duration) {
  return (parseInt(duration.seconds.toString()) * 1000000000 + duration.nanos).toString();
}
export function toTimestamp(date) {
  const seconds = numberToLong(date.getTime() / 1000);
  const nanos = date.getTime() % 1000 * 1000000;
  return {
    seconds,
    nanos
  };
}
export function fromTimestamp(t) {
  let millis = t.seconds.toNumber() * 1000;
  millis += t.nanos / 1000000;
  return new Date(millis);
}
export function numberToLong(number) {
  return Long.fromNumber(number);
}