1747 add ns to duration (#2128)

* Added nanos to Duration

* Removed unwraps

* Added nanos to Duration

* Removed unwraps

* Fixed errors

* Removed unwraps

* Changed serialization to String

* Fixed Date and Duration comparison
This commit is contained in:
Pierre-André Gagnon
2020-07-10 13:48:11 -04:00
committed by GitHub
parent 6a89b1b010
commit e07a9e4ee7
18 changed files with 263 additions and 98 deletions

View File

@ -12,7 +12,6 @@ doctest = false
[dependencies]
nu-errors = {path = "../nu-errors", version = "0.16.1"}
nu-source = {path = "../nu-source", version = "0.16.1"}
ansi_term = "0.12.1"
bigdecimal = {version = "0.1.2", features = ["serde"]}
byte-unit = "3.1.3"
@ -26,6 +25,7 @@ log = "0.4.8"
natural = "0.5.0"
num-bigint = {version = "0.2.6", features = ["serde"]}
num-traits = "0.2.12"
num-integer = "0.1.42"
query_interface = "0.3.5"
serde = {version = "1.0.114", features = ["derive"]}
serde_bytes = "0.11.5"

View File

@ -8,7 +8,6 @@ use serde::{Deserialize, Serialize};
use crate::{hir, Primitive, UntaggedValue};
use crate::{PathMember, ShellTypeName};
use derive_new::new;
use num_traits::ToPrimitive;
use nu_errors::ParseError;
use nu_source::{
@ -19,9 +18,9 @@ use nu_source::{IntoSpanned, Span, Spanned, SpannedItem, Tag};
use bigdecimal::BigDecimal;
use indexmap::IndexMap;
use log::trace;
use num_bigint::BigInt;
use num_bigint::{BigInt, ToBigInt};
use num_traits::identities::Zero;
use num_traits::FromPrimitive;
use num_traits::{FromPrimitive, ToPrimitive};
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub struct InternalCommand {
@ -296,6 +295,9 @@ pub enum Unit {
Petabyte,
// Duration units
Nanosecond,
Microsecond,
Millisecond,
Second,
Minute,
Hour,
@ -433,6 +435,17 @@ impl std::ops::Mul<u32> for Number {
}
}
impl ToBigInt for Number {
fn to_bigint(&self) -> Option<BigInt> {
match self {
Number::Int(int) => Some(int.clone()),
// The BigDecimal to BigInt conversion always return Some().
// FIXME: This conversion might not be want we want, it just remove the scale.
Number::Decimal(decimal) => decimal.to_bigint(),
}
}
}
impl PrettyDebug for Unit {
fn pretty(&self) -> DebugDocBuilder {
b::keyword(self.as_str())
@ -458,25 +471,6 @@ pub fn convert_number_to_u64(number: &Number) -> u64 {
}
}
fn convert_number_to_i64(number: &Number) -> i64 {
match number {
Number::Int(big_int) => {
if let Some(x) = big_int.to_i64() {
x
} else {
unreachable!("Internal error: convert_number_to_u64 given incompatible number")
}
}
Number::Decimal(big_decimal) => {
if let Some(x) = big_decimal.to_i64() {
x
} else {
unreachable!("Internal error: convert_number_to_u64 given incompatible number")
}
}
}
}
impl Unit {
pub fn as_str(self) -> &'static str {
match self {
@ -486,6 +480,9 @@ impl Unit {
Unit::Gigabyte => "GB",
Unit::Terabyte => "TB",
Unit::Petabyte => "PB",
Unit::Nanosecond => "ns",
Unit::Microsecond => "us",
Unit::Millisecond => "ms",
Unit::Second => "s",
Unit::Minute => "m",
Unit::Hour => "h",
@ -508,13 +505,68 @@ impl Unit {
Unit::Petabyte => {
bytes(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024 * 1024)
}
Unit::Second => duration(convert_number_to_i64(&size)),
Unit::Minute => duration(60 * convert_number_to_i64(&size)),
Unit::Hour => duration(60 * 60 * convert_number_to_i64(&size)),
Unit::Day => duration(24 * 60 * 60 * convert_number_to_i64(&size)),
Unit::Week => duration(7 * 24 * 60 * 60 * convert_number_to_i64(&size)),
Unit::Month => duration(30 * 24 * 60 * 60 * convert_number_to_i64(&size)),
Unit::Year => duration(365 * 24 * 60 * 60 * convert_number_to_i64(&size)),
Unit::Nanosecond => duration(size.to_bigint().expect("Conversion should never fail.")),
Unit::Microsecond => {
duration(size.to_bigint().expect("Conversion should never fail.") * 1000)
}
Unit::Millisecond => {
duration(size.to_bigint().expect("Conversion should never fail.") * 1000 * 1000)
}
Unit::Second => duration(
size.to_bigint().expect("Conversion should never fail.") * 1000 * 1000 * 1000,
),
Unit::Minute => duration(
size.to_bigint().expect("Conversion should never fail.") * 60 * 1000 * 1000 * 1000,
),
Unit::Hour => duration(
size.to_bigint().expect("Conversion should never fail.")
* 60
* 60
* 1000
* 1000
* 1000,
),
Unit::Day => duration(
size.to_bigint().expect("Conversion should never fail.")
* 24
* 60
* 60
* 1000
* 1000
* 1000,
),
Unit::Week => duration(
size.to_bigint().expect("Conversion should never fail.")
* 7
* 24
* 60
* 60
* 1000
* 1000
* 1000,
),
// FIXME: Number of days per month should not always be 30.
Unit::Month => duration(
size.to_bigint().expect("Conversion should never fail.")
* 30
* 24
* 60
* 60
* 1000
* 1000
* 1000,
),
// FIXME: Number of days per year should not be 365.
Unit::Year => duration(
size.to_bigint().expect("Conversion should never fail.")
* 365
* 24
* 60
* 60
* 1000
* 1000
* 1000,
),
}
}
}
@ -523,8 +575,8 @@ pub fn bytes(size: u64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Bytes(size))
}
pub fn duration(secs: i64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(secs))
pub fn duration(nanos: BigInt) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(nanos))
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]

View File

@ -212,8 +212,8 @@ impl UntaggedValue {
}
/// Helper for creating date duration values
pub fn duration(secs: i64) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(secs))
pub fn duration(nanos: BigInt) -> UntaggedValue {
UntaggedValue::Primitive(Primitive::Duration(nanos))
}
/// Helper for creating datatime values

View File

@ -81,7 +81,7 @@ impl PrettyDebug for Primitive {
false => b::primitive("$no"),
},
Primitive::Date(date) => primitive_doc(date, "date"),
Primitive::Duration(duration) => primitive_doc(duration, "seconds"),
Primitive::Duration(duration) => primitive_doc(duration, "nanoseconds"),
Primitive::Path(path) => primitive_doc(path, "path"),
Primitive::Binary(_) => b::opaque("binary"),
Primitive::BeginningOfStream => b::keyword("beginning-of-stream"),

View File

@ -7,11 +7,15 @@ use chrono::{DateTime, Utc};
use nu_errors::{ExpectedRange, ShellError};
use nu_source::{PrettyDebug, Span, SpannedItem};
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::cast::{FromPrimitive, ToPrimitive};
use num_traits::identities::Zero;
use num_traits::sign::Signed;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
const NANOS_PER_SEC: u32 = 1000000000;
/// The most fundamental of structured values in Nu are the Primitive values. These values represent types like integers, strings, booleans, dates, etc that are then used
/// as the buildig blocks to build up more complex structures.
///
@ -40,8 +44,9 @@ pub enum Primitive {
Boolean(bool),
/// A date value, in UTC
Date(DateTime<Utc>),
/// A count in the number of seconds
Duration(i64),
/// A count in the number of nanoseconds
#[serde(with = "serde_bigint")]
Duration(BigInt),
/// A range of values
Range(Box<Range>),
/// A file path
@ -75,6 +80,40 @@ impl Primitive {
}
}
// FIXME: This is a bad name, but no other way to differentiate with our own Duration.
pub fn into_chrono_duration(self, span: Span) -> Result<chrono::Duration, ShellError> {
match self {
Primitive::Duration(duration) => {
let (secs, nanos) = duration.div_rem(&BigInt::from(NANOS_PER_SEC));
let secs = match secs.to_i64() {
Some(secs) => secs,
None => {
return Err(ShellError::labeled_error(
"Internal duration conversion overflow.",
"duration overflow",
span,
))
}
};
// This should never fail since nanos < 10^9.
let nanos = match nanos.to_i64() {
Some(nanos) => nanos,
None => return Err(ShellError::unexpected("Unexpected i64 overflow")),
};
let nanos = chrono::Duration::nanoseconds(nanos);
// This should also never fail since we are adding less than NANOS_PER_SEC.
match chrono::Duration::seconds(secs).checked_add(&nanos) {
Some(duration) => Ok(duration),
None => Err(ShellError::unexpected("Unexpected duration overflow")),
}
}
other => Err(ShellError::type_error(
"duration",
other.type_name().spanned(span),
)),
}
}
pub fn into_string(self, span: Span) -> Result<String, ShellError> {
match self {
Primitive::String(s) => Ok(s),
@ -209,6 +248,20 @@ impl From<f64> for Primitive {
}
}
impl From<chrono::Duration> for Primitive {
fn from(duration: chrono::Duration) -> Primitive {
// FIXME: This is a hack since chrono::Duration does not give access to its 'nanos' field.
let secs: i64 = duration.num_seconds();
// This will never fail.
let nanos: u32 = duration
.checked_sub(&chrono::Duration::seconds(secs))
.expect("Unexpected overflow")
.num_nanoseconds()
.expect("Unexpected overflow") as u32;
Primitive::Duration(BigInt::from(secs) * NANOS_PER_SEC + nanos)
}
}
impl ShellTypeName for Primitive {
/// Get the name of the type of a Primitive value
fn type_name(&self) -> &'static str {
@ -254,7 +307,7 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
_ => byte.format(1),
}
}
Primitive::Duration(sec) => format_duration(*sec),
Primitive::Duration(duration) => format_duration(duration),
Primitive::Int(i) => i.to_string(),
Primitive::Decimal(decimal) => format!("{:.4}", decimal),
Primitive::Range(range) => format!(
@ -297,18 +350,49 @@ pub fn format_primitive(primitive: &Primitive, field_name: Option<&String>) -> S
}
}
/// Format a duration in seconds into a string
pub fn format_duration(sec: i64) -> String {
let (minutes, seconds) = (sec / 60, sec % 60);
let (hours, minutes) = (minutes / 60, minutes % 60);
let (days, hours) = (hours / 24, hours % 24);
match (days, hours, minutes, seconds) {
(0, 0, 0, 1) => "1 sec".to_owned(),
(0, 0, 0, s) => format!("{} secs", s),
(0, 0, m, s) => format!("{}:{:02}", m, s),
(0, h, m, s) => format!("{}:{:02}:{:02}", h, m, s),
(d, h, m, s) => format!("{}:{:02}:{:02}:{:02}", d, h, m, s),
/// Format a duration in nanoseconds into a string
pub fn format_duration(duration: &BigInt) -> String {
// FIXME: This involves a lot of allocation, but it seems inevitable with BigInt.
let big_int_1000 = BigInt::from(1000);
let big_int_60 = BigInt::from(60);
let big_int_24 = BigInt::from(24);
// We only want the biggest subvidision to have the negative sign.
let (sign, duration) = if duration.is_zero() || duration.is_positive() {
(1, duration.clone())
} else {
(-1, -duration)
};
let (micros, nanos): (BigInt, BigInt) = duration.div_rem(&big_int_1000);
let (millis, micros): (BigInt, BigInt) = micros.div_rem(&big_int_1000);
let (secs, millis): (BigInt, BigInt) = millis.div_rem(&big_int_1000);
let (mins, secs): (BigInt, BigInt) = secs.div_rem(&big_int_60);
let (hours, mins): (BigInt, BigInt) = mins.div_rem(&big_int_60);
let (days, hours): (BigInt, BigInt) = hours.div_rem(&big_int_24);
let decimals = if millis.is_zero() && micros.is_zero() && nanos.is_zero() {
String::from("0")
} else {
format!("{:03}{:03}{:03}", millis, micros, nanos)
.trim_end_matches('0')
.to_string()
};
match (
days.is_zero(),
hours.is_zero(),
mins.is_zero(),
secs.is_zero(),
) {
(true, true, true, true) => format!("{}.{}", if sign == 1 { "0" } else { "-0" }, decimals),
(true, true, true, _) => format!("{}.{}", sign * secs, decimals),
(true, true, _, _) => format!("{}:{:02}.{}", sign * mins, secs, decimals),
(true, _, _, _) => format!("{}:{:02}:{:02}.{}", sign * hours, mins, secs, decimals),
_ => format!(
"{}:{:02}:{:02}:{:02}.{}",
sign * days,
hours,
mins,
secs,
decimals
),
}
}