mirror of
https://github.com/nushell/nushell.git
synced 2025-07-08 18:37:07 +02:00
# Description As title # User-Facing Changes # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
102 lines
2.8 KiB
Rust
102 lines
2.8 KiB
Rust
use super::super::super::values::NuDataFrame;
|
|
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type,
|
|
};
|
|
use polars::prelude::{IntoSeries, Utf8Methods};
|
|
|
|
#[derive(Clone)]
|
|
pub struct AsDate;
|
|
|
|
impl Command for AsDate {
|
|
fn name(&self) -> &str {
|
|
"as-date"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
r#"Converts string to date."#
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
r#"Format example:
|
|
"%Y-%m-%d" => 2021-12-31
|
|
"%d-%m-%Y" => 31-12-2021
|
|
"%Y%m%d" => 2021319 (2021-03-19)"#
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.required("format", SyntaxShape::String, "formatting date string")
|
|
.switch("not-exact", "the format string may be contained in the date (e.g. foo-2021-01-01-bar could match 2021-01-01)", Some('n'))
|
|
.input_type(Type::Custom("dataframe".into()))
|
|
.output_type(Type::Custom("dataframe".into()))
|
|
.category(Category::Custom("dataframe".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Converts string to date",
|
|
example: r#"["2021-12-30" "2021-12-31"] | into df | as-datetime "%Y-%m-%d""#,
|
|
result: None,
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
command(engine_state, stack, call, input)
|
|
}
|
|
}
|
|
|
|
fn command(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let format: String = call.req(engine_state, stack, 0)?;
|
|
let not_exact = call.has_flag("not-exact");
|
|
|
|
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
|
let series = df.as_series(call.head)?;
|
|
let casted = series.utf8().map_err(|e| {
|
|
ShellError::GenericError(
|
|
"Error casting to string".into(),
|
|
e.to_string(),
|
|
Some(call.head),
|
|
None,
|
|
Vec::new(),
|
|
)
|
|
})?;
|
|
|
|
let res = if not_exact {
|
|
casted.as_date_not_exact(Some(format.as_str()))
|
|
} else {
|
|
casted.as_date(Some(format.as_str()), false)
|
|
};
|
|
|
|
let mut res = res
|
|
.map_err(|e| {
|
|
ShellError::GenericError(
|
|
"Error creating datetime".into(),
|
|
e.to_string(),
|
|
Some(call.head),
|
|
None,
|
|
Vec::new(),
|
|
)
|
|
})?
|
|
.into_series();
|
|
|
|
res.rename("date");
|
|
|
|
NuDataFrame::try_from_series(vec![res], call.head)
|
|
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
|
}
|