Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: hope to support like mysql date_format and str_to_date fuction #11125

Closed
zhoukai-1992 opened this issue Apr 19, 2023 · 1 comment · Fixed by #11442
Closed

Feature: hope to support like mysql date_format and str_to_date fuction #11125

zhoukai-1992 opened this issue Apr 19, 2023 · 1 comment · Fixed by #11442
Assignees
Labels
C-feature Category: feature good first issue Category: good first issue

Comments

@zhoukai-1992
Copy link

In applications, we often need to display the time format as yyyy-MM-dd HH: mm: ss or a custom time format,but databend only support to change yyyyMM yyyyMMDD yyyymmddhhmmss format.

@zhoukai-1992 zhoukai-1992 added the C-feature Category: feature label Apr 19, 2023
@b41sh b41sh added the good first issue Category: good first issue label Apr 19, 2023
@databendlabs databendlabs locked and limited conversation to collaborators Apr 21, 2023
@databendlabs databendlabs unlocked this conversation Apr 21, 2023
@TCeason TCeason self-assigned this Apr 21, 2023
@TCeason
Copy link
Collaborator

TCeason commented May 12, 2023

MySQL date_format('datetime', format)

Formats the date value according to the format string. If either argument is NULL, the function returns NULL.

Now databend use chrono lib parse date and datetime.

So I think we can use this function to impl str_to_date.

DateTime::parse_from_str("5.8.1994 8:00 am +0000", "%d.%m.%Y %H:%M %P %z")?;

And Date format can use this :

cat src/main.rs 

use chrono::prelude::*;

fn main() {
    let local: DateTime<Local> = Local::now();
    println!("{:?}", local.format("%Y-%m-%d %H:%M:%S").to_string());
    println!("{:?}", local.format("%a %b %e %T %Y").to_string());
    println!("{:?}", local.format("%c").to_string());
    println!("{:?}", local.to_string());
    println!("{:?}", local.to_rfc2822());
    println!("{:?}", local.to_rfc3339());

    let dt = Local.ymd(2020, 12, 05).and_hms(12, 0, 9);
    println!("{:?}", dt.format("%Y-%m-%d %H:%M:%S").to_string());
    println!("{:?}", dt.format("%a %b %e %T %Y").to_string());
    println!("{:?}", dt.format("%c").to_string());
    println!("{:?}", dt.to_string());
    println!("{:?}", dt.to_rfc2822());
    println!("{:?}", dt.to_rfc3339());
}
➜  ~/hi👆 git:(master) ⚡ ./target/debug/hi 
"2023-05-12 17:46:13"
"Fri May 12 17:46:13 2023"
"Fri May 12 17:46:13 2023"
"2023-05-12 17:46:13.058807471 +08:00"
"Fri, 12 May 2023 17:46:13 +0800"
"2023-05-12T17:46:13.058807471+08:00"
"2020-12-05 12:00:09"
"Sat Dec  5 12:00:09 2020"
"Sat Dec  5 12:00:09 2020"
"2020-12-05 12:00:09 +08:00"
"Sat, 05 Dec 2020 12:00:09 +0800"
"2020-12-05T12:00:09+08:00"

<p style="box-sizing: inherit; margin: 0px 0px 15px; padding: 0px; border: 0px; outline: 0px; font-size: 14.256px; vertical-align: baseline; background: rgb(255, 255, 255); line-height: 1.75; display: block; color: rgb(85, 85, 85); font-family: &quot;Open Sans&quot;, Arial, Helvetica, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">The specifiers shown in the following table may be used in the<span> </span><em class="replaceable" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14.256px; vertical-align: baseline; background: transparent; font-style: italic;"><code style="box-sizing: inherit; margin: 0px; padding: 1px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: inherit; color: inherit; font-weight: 700; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">format</code></em><span> </span>string. The<span> </span><code class="literal" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background: transparent; color: rgb(0, 0, 0); font-weight: 400; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">%</code><span> </span>character is required before format specifier characters. The specifiers apply to other functions as well:<span> </span><a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 14.256px; vertical-align: baseline; background: transparent; text-decoration: none; color: rgb(0, 116, 163);"><code class="literal" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background: transparent; text-decoration: underline; text-underline-position: under; color: rgb(0, 0, 0); font-weight: 400; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">STR_TO_DATE()</code></a>,<span> </span><a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-format" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 14.256px; vertical-align: baseline; background: transparent; text-decoration: none; color: rgb(0, 116, 163);"><code class="literal" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background: transparent; text-decoration: underline; text-underline-position: under; color: rgb(0, 0, 0); font-weight: 400; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">TIME_FORMAT()</code></a>,<span> </span><a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 14.256px; vertical-align: baseline; background: transparent; text-decoration: none; color: rgb(0, 116, 163);"><code class="literal" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background: transparent; text-decoration: underline; text-underline-position: under; color: rgb(0, 0, 0); font-weight: 400; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">UNIX_TIMESTAMP()</code></a>.</p><div class="informaltable" style="box-sizing: inherit; margin: 0px; padding: 5px 0px 15px; border: 0px; outline: 0px; font-size: 14.256px; vertical-align: baseline; background: rgb(255, 255, 255); color: rgb(85, 85, 85); font-family: &quot;Open Sans&quot;, Arial, Helvetica, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">

Specifier | Description
-- | --
%a | Abbreviated weekday name (Sun..Sat)
%b | Abbreviated month name (Jan..Dec)
%c | Month, numeric (0..12)
%D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d | Day of the month, numeric (00..31)
%e | Day of the month, numeric (0..31)
%f | Microseconds (000000..999999)
%H | Hour (00..23)
%h | Hour (01..12)
%I | Hour (01..12)
%i | Minutes, numeric (00..59)
%j | Day of year (001..366)
%k | Hour (0..23)
%l | Hour (1..12)
%M | Month name (January..December)
%m | Month, numeric (00..12)
%p | AM or PM
%r | Time, 12-hour (hh:mm:ss followed by AM or PM)
%S | Seconds (00..59)
%s | Seconds (00..59)
%T | Time, 24-hour (hh:mm:ss)
%U | Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
%u | Week (00..53), where Monday is the first day of the week; WEEK() mode 1
%V | Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v | Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%W | Weekday name (Sunday..Saturday)
%w | Day of the week (0=Sunday..6=Saturday)
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y | Year, numeric, four digits
%y | Year, numeric (two digits)
%% | A literal % character
%x | x, for any “x” not listed above

</div><div class="informaltable" style="box-sizing: inherit; margin: 0px; padding: 5px 0px 15px; border: 0px; outline: 0px; font-size: 14.256px; vertical-align: baseline; background: rgb(255, 255, 255); color: rgb(85, 85, 85); font-family: &quot;Open Sans&quot;, Arial, Helvetica, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"></div><p style="box-sizing: inherit; margin: 0px 0px 15px; padding: 0px; border: 0px; outline: 0px; font-size: 14.256px; vertical-align: baseline; background: rgb(255, 255, 255); line-height: 1.75; display: block; color: rgb(85, 85, 85); font-family: &quot;Open Sans&quot;, Arial, Helvetica, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Ranges for the month and day specifiers begin with zero due to the fact that MySQL permits the storing of incomplete dates such as<span> </span><code class="literal" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background: transparent; color: rgb(0, 0, 0); font-weight: 400; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">'2014-00-00'</code>.</p><p style="box-sizing: inherit; margin: 0px 0px 15px; padding: 0px; border: 0px; outline: 0px; font-size: 14.256px; vertical-align: baseline; background: rgb(255, 255, 255); line-height: 1.75; display: block; color: rgb(85, 85, 85); font-family: &quot;Open Sans&quot;, Arial, Helvetica, sans-serif; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">The language used for day and month names and abbreviations is controlled by the value of the<span> </span><a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_lc_time_names" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 14.256px; vertical-align: baseline; background: transparent; text-decoration: none; color: rgb(0, 116, 163);"><code class="literal" style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13.5432px; vertical-align: baseline; background: transparent; text-decoration: underline; text-underline-position: under; color: rgb(0, 0, 0); font-weight: 400; font-family: &quot;Courier New&quot;, Courier, fixed, monospace;">lc_time_names</code></a><span> </span>system variable (<a class="xref" href="https://dev.mysql.com/doc/refman/8.0/en/locale-support.html" title="10.16&nbsp;MySQL Server Locale Support" style="box-sizing: inherit; margin: 0px; padding: 0px; font-size: 14.256px; vertical-align: baseline; background: transparent; text-decoration: none; color: rgb(0, 116, 163);">Section 10.16, “MySQL Server Locale Support”</a>).</p><br class="Apple-interchange-newline">The specifiers shown in the following table may be used in the format string. The % character is required before format specifier characters. The specifiers apply to other functions as well: [STR_TO_DATE()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date), [TIME_FORMAT()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_time-format), [UNIX_TIMESTAMP()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp).

Specifier	Description
%a	Abbreviated weekday name (Sun..Sat)
%b	Abbreviated month name (Jan..Dec)
%c	Month, numeric (0..12)
%D	Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d	Day of the month, numeric (00..31)
%e	Day of the month, numeric (0..31)
%f	Microseconds (000000..999999)
%H	Hour (00..23)
%h	Hour (01..12)
%I	Hour (01..12)
%i	Minutes, numeric (00..59)
%j	Day of year (001..366)
%k	Hour (0..23)
%l	Hour (1..12)
%M	Month name (January..December)
%m	Month, numeric (00..12)
%p	AM or PM
%r	Time, 12-hour (hh:mm:ss followed by AM or PM)
%S	Seconds (00..59)
%s	Seconds (00..59)
%T	Time, 24-hour (hh:mm:ss)
%U	Week (00..53), where Sunday is the first day of the week; [WEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week) mode 0
%u	Week (00..53), where Monday is the first day of the week; [WEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week) mode 1
%V	Week (01..53), where Sunday is the first day of the week; [WEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week) mode 2; used with %X
%v	Week (01..53), where Monday is the first day of the week; [WEEK()](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_week) mode 3; used with %x
%W	Weekday name (Sunday..Saturday)
%w	Day of the week (0=Sunday..6=Saturday)
%X	Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x	Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y	Year, numeric, four digits
%y	Year, numeric (two digits)
%%	A literal % character
%x	x, for any “x” not listed above
Ranges for the month and day specifiers begin with zero due to the fact that MySQL permits the storing of incomplete dates such as '2014-00-00'.

The language used for day and month names and abbreviations is controlled by the value of the [lc_time_names](https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_lc_time_names) system variable ([Section 10.16, “MySQL Server Locale Support”](https://dev.mysql.com/doc/refman/8.0/en/locale-support.html)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature Category: feature good first issue Category: good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants