How do I parse a String to Time in Crystal Lang? - crystal-lang

I have a String in MM-DD-YYYY format such as: 04-08-2022.
I want to parse that to a Time type.
How do I do that in Crystal?

Crystal has a Time.parse (documentation link) method which can be used in this scenario.
The method receives a String argument to parse along with a String pattern argument and the requested Location of the time. Here is the method signature below.
def self.parse(time : String, pattern : String, location : Location) : Time
For your example, we could use the formatter to provide our custom format as %m-%d-%Y, where %m means month MM format, %d means day DD format, and %Y means YYYY format. Finally we use the local Location of the script being run.
For example:
time = Time.parse("04-08-2022", "%m-%d-%Y", Time::Location.local)

Related

Unreliable DateTime parsing by COleDateTime

I am trying to parse the date using ParseDateTime method provided by COleDateTime class. But parsing of two different dates in the same program is returning inconsisent values for the month.
Code Snippet:
COleDateTime dtCreated;
dtCreated.ParseDateTime(safe_cast<CString>(strCreatedDate));
Inconsistent RESULTS:
if strCreatedDate = "10/7/2020" (in mm.dd.yyyy format)
then dtCreated.GetMonth() = 7 (but it should be 10)
if strCreatedDate = "7/29/2020" (in mm.dd.yyyy format)
then dtCreated.GetMonth() = 7 (in this case, it is correct)
UPDATE:
The value of date present in the strCreatedDate vairable could be "dd.mm.yyyy" OR "mm.dd.yyyy" format. But I do have the information about the data format available in a separate variable. Based on the format, I want COleDateTime to correctly parse the DateTime string. How can I do that?
Since String^ is your input you could use DateTime::ParseExact, and then convert DateTime to COleDateTime using DateTime.ToOADate:
COleDateTime dtCreated(DateTime::ParseExact(
strCreatedDate, isDMY ? "d.M.yyyy" : "M.d.yyyy", nullptr).ToOADate());

String to YYYY-MM-DD date format in Athena

So I've looked through documentation and previous answers on here, but can't seem to figure this out.
I have a STRING that represents a date. A normal output looks as such:
2018-09-19 17:47:12
If I do this, I get it to return in this format 2018-09-19 17:47:12.000:
SELECT
date_parse(click_time,'%Y-%m-%d %H:%i:%s') click_time
FROM
table.abc
But that's not the output I need. I was just trying to show that I'm close, but clearly missing something. When I change click_time to date_parse(click_time,'%Y-%m-%d'), it sends back INVALID_FUNCTION_ARGUMENT: Invalid format: "2018-09-19 17:47:12" is malformed at " 17:47:12"
So there's clearly something I'm not doing correctly to get it to simply return 2018-09-19.
date_parse converts a string to a timestamp. As per the documentation, date_parse does this:
date_parse(string, format) → timestamp
It parses a string to a timestamp using the supplied format.
So for your use case, you need to do the following:
cast(date_parse(click_time,'%Y-%m-%d %H:%i:%s')) as date )
For your further reference, you can go to the below link for prestodb online documentation https://prestodb.github.io/docs/current/functions/datetime.html

Presto SQL: TO_UNIXTIME

I want to convert a readable timestamp to UNIX time.
For example: I want to convert 2018-08-24 18:42:16 to 1535136136000.
Here is my syntax:
TO_UNIXTIME('2018-08-24 06:42:16') new_year_ut
My error is:
SYNTAX_ERROR: line 1:77: Unexpected parameters (varchar(19)) for function to_unixtime. Expected: to_unixtime(timestamp) , to_unixtime(timestamp with time zone)
You need to wrap the varchar in a CAST to timestamp:
to_unixtime(CAST('2018-08-24 06:42:16' AS timestamp)) -- note: returns a double
If your timestamp value doesn't have fraction of second (or you are not interested in it), you can cast to bigint to have integral result:
CAST(to_unixtime(CAST('2018-08-24 06:42:16' AS timestamp)) AS BIGINT)
If your readable timestamp value is a string in different format than the above, you would need to use date_parse or parse_datetime for the conversion. See https://trino.io/docs/current/functions/datetime.html for more information.
Note: when dealing with timestamp values, please keep in mind that: https://github.com/trinodb/trino/issues/37

Ensure filename is in datetime correct format

I want all my files to be off format: 2013-03-31_142436.jpg
i.e. %Y-%m-%d_%H%M%S
I have a script to rename that way but would like to check if the filename is of the format first. I do:
for filename in files:
# check filename not already in file format
filename_without_ext = os.path.splitext(filename)[0];
How do I check filename_without_ext is of format %Y-%m-%d_%H%M%S
Use re:
import re
if re.match(r'\d{4}-\d{2}-\d{2}_\d{6}$', filename_without_ext):
pass # of the right format
This will just check it looks like it has a chance of being a valid date. Use Martijn's answer if you require it to be a valid date.
Just try to parse it as a timestamp:
from time import strptime
try:
strptime(filename_without_ext, '%Y-%m-%d_%H%M%S')
except ValueError:
# Not a valid timestamp
The strptime() test has the advantage that it guarantees that you have a valid datetime value, not just a pattern of digits that still could represent an invalid datetime ('1234-56-78_987654' is not a valid timestamp, for example).

C++ boost date format

I have a vector string of dates in the from "dd-mmm-yyyy" so for example
todays date would be:
std::string today("07-Sep-2010");
I'd like to use the date class in boost but to create a date object the
constructor for date needs to be called as follows:
date test(2010,Sep,07);
Is there any easy/elegant way of passing dates in the format "dd-mmm-yyyy"?
My first thought was to use substr and then cast it? But I've read that there's also
the possibility of using 'date facets'?
Thanks!
include "boost/date_time/gregorian/parsers.hpp"
date test = boost::gregorian::from_us_string("07-Sep-2010")
There is a builtin parser for this form of date in Boost itself, check out the docs here:
http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/date_time_io.html#date_time.io_objects
date_type parse_date(...)
Parameters:
string_type input
string_type format
special_values_parser
Parse a date from the given input using the given format.
string inp("2005-Apr-15");
string format("%Y-%b-%d");
date d;
d = parser.parse_date(inp,
format,
svp);
// d == 2005-Apr-15
with inp adjusted for your needs.