www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to fix date format?

reply Suliman <evermind live.ru> writes:
I am using mysql native. Date in DB have next format: 2016-11-01 
06:19:37

But every tile when I am trying to get it I am getting such 
format:
2016-Oct-31 15:37:24

I use next code:
writeln(point[1].coerce!string);

Why coerce is forcing format changing? How I can extract result 
as without month name between digits or easily convert it in 
proper data format?
Apr 25 2017
next sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Tuesday, April 25, 2017 17:41:25 Suliman via Digitalmars-d-learn wrote:
 I am using mysql native. Date in DB have next format: 2016-11-01
 06:19:37

 But every tile when I am trying to get it I am getting such
 format:
 2016-Oct-31 15:37:24

 I use next code:
 writeln(point[1].coerce!string);

 Why coerce is forcing format changing? How I can extract result
 as without month name between digits or easily convert it in
 proper data format?
What types are dealing with here? What is point[1]? - Jonathan M Davis
Apr 25 2017
parent reply Suliman <evermind live.ru> writes:
On Tuesday, 25 April 2017 at 20:10:02 UTC, Jonathan M Davis wrote:
 On Tuesday, April 25, 2017 17:41:25 Suliman via 
 Digitalmars-d-learn wrote:
 I am using mysql native. Date in DB have next format: 
 2016-11-01 06:19:37

 But every tile when I am trying to get it I am getting such
 format:
 2016-Oct-31 15:37:24

 I use next code:
 writeln(point[1].coerce!string);

 Why coerce is forcing format changing? How I can extract 
 result as without month name between digits or easily convert 
 it in proper data format?
What types are dealing with here? What is point[1]? - Jonathan M Davis
writeln(point[1].coerce!string); writeln(point[1].type);
std.datetime.DateTime
std.variant.VariantN!20LU.VariantN
Apr 25 2017
parent reply Suliman <evermind live.ru> writes:
I tried to do:

writeln(DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!string)));

But got error:

Error: function std.datetime.DateTime.toISOExtString () const is 
not callable using argument types (DateTime)
Error: function database.Database.getSingleTrackInfo no return 
exp; or assert(0); at end of function
Apr 25 2017
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, April 26, 2017 04:02:12 Suliman via Digitalmars-d-learn wrote:
 I tried to do:

 writeln(DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!
 string)));

 But got error:

 Error: function std.datetime.DateTime.toISOExtString () const is
 not callable using argument types (DateTime)
 Error: function database.Database.getSingleTrackInfo no return
 exp; or assert(0); at end of function
toISOExtString is a normal member function on DateTime, not a static member function. If point[1].coerce!string is giving you a string in Boost's "simple time" format (e.g. "2016-Jan-04 12:19:17"), then DateTime.fromSimpleString(point[1].coerce!string) will give you a DateTime. Then if you called toISOExtString() on that, e.g. DateTime dt = DateTime.fromSimpleString(point[1].coerce!string); string str = dt.toISOExtString(); then the string would be in the ISO extended format (e.g. "2016-01-04T12:19:17"). If you then wanted that in the format "2016-01-04 12:19:17", then you could just replace the 'T' with ' ', e.g. DateTime dt = DateTime.fromSimpleString(point[1].coerce!string); string str = dt.toISOExtString().replace("T", " "); And if you have "2016-01-04 12:19:17", and you want to convert that to the Boost simple time format, you could do DateTime dt = DateTime.fromISOExtString(str.replace(" ", "T")) auto simpleStr = dt.toSimpleString(); Hopefully, that helps. - Jonathan M Davis
Apr 25 2017
parent reply Suliman <evermind live.ru> writes:
On Wednesday, 26 April 2017 at 05:21:32 UTC, Jonathan M Davis 
wrote:
 On Wednesday, April 26, 2017 04:02:12 Suliman via 
 Digitalmars-d-learn wrote:
 I tried to do:

 writeln(DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!
 string)));

 But got error:

 Error: function std.datetime.DateTime.toISOExtString () const 
 is
 not callable using argument types (DateTime)
 Error: function database.Database.getSingleTrackInfo no return
 exp; or assert(0); at end of function
toISOExtString is a normal member function on DateTime, not a static member function. If point[1].coerce!string is giving you a string in Boost's "simple time" format (e.g. "2016-Jan-04 12:19:17"), then DateTime.fromSimpleString(point[1].coerce!string) will give you a DateTime. Then if you called toISOExtString() on that, e.g. DateTime dt = DateTime.fromSimpleString(point[1].coerce!string); string str = dt.toISOExtString(); then the string would be in the ISO extended format (e.g. "2016-01-04T12:19:17"). If you then wanted that in the format "2016-01-04 12:19:17", then you could just replace the 'T' with ' ', e.g. DateTime dt = DateTime.fromSimpleString(point[1].coerce!string); string str = dt.toISOExtString().replace("T", " "); And if you have "2016-01-04 12:19:17", and you want to convert that to the Boost simple time format, you could do DateTime dt = DateTime.fromISOExtString(str.replace(" ", "T")) auto simpleStr = dt.toSimpleString(); Hopefully, that helps. - Jonathan M Davis
Thanks! That's work! But why I can't do it in single line like: string dt = DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!string)); "Error: function std.datetime.DateTime.toISOExtString () const is not callable using argument types (DateTime)" And should do: DateTime dt = DateTime.fromSimpleString(point[1].coerce!string); string str = dt.toISOExtString();
Apr 25 2017
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Wednesday, April 26, 2017 06:55:01 Suliman via Digitalmars-d-learn wrote:
 Thanks! That's work!

 But why I can't do it in single line like:
 string dt =
 DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!string))
 ; "Error: function std.datetime.DateTime.toISOExtString () const is not
 callable using argument types (DateTime)"

 And should do:

 DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
 string str = dt.toISOExtString();
You can do it in a single line. I just split it up so that it fit better in the limited line length of an e-mail, and because it was clearer with the types. The problem is that you're calling toISOExtString incorrectly. The from*String functions are static functions, but the to*String functions are normal member functions. You don't pass a DateTime to toISOExtString. You just call it on the DateTime object. So, if you want one line, you end up with something like auto str = DateTime.fromSimpleString(point[1].coerce!string). toISOExtString().replace("T", " "); - Jonathan M Davis
Apr 26 2017
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/25/2017 10:41 AM, Suliman wrote:
 I am using mysql native. Date in DB have next format: 2016-11-01 06:19:37

 But every tile when I am trying to get it I am getting such format:
 2016-Oct-31 15:37:24

 I use next code:
 writeln(point[1].coerce!string);

 Why coerce is forcing format changing? How I can extract result as
 without month name between digits or easily convert it in proper data
 format?
Here's a silly little code that gets the job done. :) import std.stdio; string[string] numeralMonth; static this() { numeralMonth = [ "Jan" : "01", "Feb" : "02", "Mar" : "03", "Apr" : "04", "May" : "05", "Jun" : "06", "Jul" : "07", "Aug" : "08", "Sep" : "09", "Oct" : "10", "Nov" : "11", "Dec" : "12" ]; } auto goodDateFormat(string str) { import std.range : chain, choose; if (str.length >= 8) { auto key = str[5..8]; auto numeral = key in numeralMonth; if (numeral) { import std.string : format; return format("%s%s%s", str[0..5], *numeral, str[8..$]); } } return str; } void main() { writeln(goodDateFormat("2016-Oct-31 15:37:24")); } Ali
Apr 25 2017