Dart - Flutter - Aqueduct: strange behavior of DateTime.parse() - parsing

aye aye good people,
I'm really confused about the behavior of DateTime.parse();
on dartpad this works
void main() {
const String _iso8601 = '2019-04-01T08:30:00';
final DateTime _date = DateTime.parse(_iso8601);
print(_date.toIso8601String());
}
but in flutter doesn't, but this does
const String _iso8601 = '2019-04-01T08:30:00.000';
final DateTime _date = DateTime.parse(_iso8601);
I'm now in aqueduct and neither of those works including this
String _iso8601 = '2019-04-01T08:30:00Z';
please note that with "didn't work" I don't mean that it returns an error,
but just a null.
[edit: correction
when I mock the string instead of mapping it from the body of a request it returns
Exception has occurred. FormatException (null)
but then again I'm using Iso8601]
If you have some experience with this situation I could use some help.
[edit: note that aqueduct runs on dart 2.0]
Thank you in advance, Francesco

Examples of accepted strings:
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456z"
"2012-02-27 13:27:00,123456z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"

Related

Parse clock time in java 8

I wonder is it possible to parse clock time hour:minute:second in Java 8?
e.g.
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
final String str = "12:22:10";
final LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
I tried but get this exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed
LocalTime.parse
Since you only have a time use the LocalTime class. No need to define a formatting pattern in your case.
String str = "12:22:10";
LocalTime time = LocalTime.parse(str);
See that code run live at IdeOne.com.
See Oracle Tutorial for more info.
LocalTime.parse("04:17");

Getting error while parsing string to datetime?

Getting error while parsing string to datetime.
string datestring = "111815";
DateTime date = Convert.ToDateTime(datestring);
I also tried using, Parse, ExactParse with/without culture specificinfo.
I'm still getting the error:
String was not recognized as a valid DateTime.
Please suggest the correct solution.
You just need to specify the right format string when you call ParseExact. In your case, it looks like this is month-day-year, without any separators, and with a 2-digit year (blech). So you'd parse it like this:
using System;
using System.Globalization;
class Test
{
static void Main()
{
DateTime dt = DateTime.ParseExact("111815", "MMddyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
}
}
If you're in control of the format at all, I'd strongly recommend yyyy-MM-dd instead of this ambiguous (due to the 2-digit years) and US-centric (due to month/day/year) format.

Check if (native) feature/class/function (e.g. MediaSource) is available/supported

how can I check using Google Dart if a feature (for example MediaSource) is available.
new MediaSource() throws an error. How to programmatically check if this class or feature exists? Any ideas? Is there an in-build feature for this?
I tried try/catch but it looks like the type of the exception differs on the Browser I use.
EDIT #2
youtube.com/html5 does it like this:
var mse = window['MediaSource'] || window['WebKitMediaSource'];
setCompatibility('c-mse', !!mse);
So should I just use jsobject (dart:js package)?
Regards and Thanks,
Robert
I found this:
import 'dart:js';
bool available = context.hasProperty('MediaSource');
Does anyone have a better solution? To me this looks like the cleanest solution.
Regards, Robert
I think TypeError is thrown, so what about catching the exception if MediaSource doesn't exist?
try {
new MediaSource();
// do something if MediaSource is available
} on TypeError catch(e) {
// do something else if MediaSource is not available
}
Dart provides special annotation:
/**
* An annotation used to mark a feature as only being supported by a subset
* of the browsers that Dart supports by default.
*
* If an API is not annotated with [SupportedBrowser] then it is assumed to
* work on all browsers Dart supports.
*/
class SupportedBrowser {
static const String CHROME = "Chrome";
static const String FIREFOX = "Firefox";
static const String IE = "Internet Explorer";
static const String OPERA = "Opera";
static const String SAFARI = "Safari";
/// The name of the browser.
final String browserName;
/// The minimum version of the browser that supports the feature, or null
/// if supported on all versions.
final String minimumVersion;
const SupportedBrowser(this.browserName, [this.minimumVersion]);
}
for example:
#DomName('ApplicationCache')
#SupportedBrowser(SupportedBrowser.CHROME)
#SupportedBrowser(SupportedBrowser.FIREFOX)
#SupportedBrowser(SupportedBrowser.IE, '10')
#SupportedBrowser(SupportedBrowser.OPERA)
#SupportedBrowser(SupportedBrowser.SAFARI)
#Unstable()
class ApplicationCache extends EventTarget {
...
You can detect browser version and get(with mirrors) annotation of a class that represents some web feature. If it has #Experimental and, probably #Unstable then you can't rely on it even with supported browsers. If it has #SupportedBrowser annotation and users browser is in list or it has no #SupportedBrowser at all then you should be ok.

F# passing null reference to ref parameter

I am using a third party API that rather clumsily makes use of ref parameters to produce outputs. Personally I really hate this design of an API but it's what I have available to me right now. I've had to hide the datatypes of the API slightly due to proprietary code but this should be irrelevant to the problem at hand.
Anyway in C# I can pass a null reference as a ref parameter successfully as follows:
IDataType tl = null;
bool success = api.myFunction(ref tl);
However in F# the following will not work
let mutable tl : IDataType = null //null reference assignment in F#
let success = api.myFunction(&tl) //& means ref in F#
It returns a null reference exception error. No such error is returned in C#.
Has anyone experiences this before? I am thinking it must be a bug in the API itself which is relatively ancient design.
**Edit: This should be closed, I believe the answer does not lie in the F# code but in the API as it's already a number of known bugs similar to this.
Quick and dirty prototyping of your API in C#
namespace API
{
public interface IDataType { void Hi(); }
public class API: IDataType {
public void Hi() { System.Console.WriteLine("Hi!"); }
public bool MyFunction(ref IDataType iface) {
iface = new API();
return true;
}
}
}
and then using it from F# exactly your way while staying within the same CLR:
let mutable iface : API.IDataType = null
if API.API().MyFunction(&iface) then iface.Hi()
works without any problem.
So, indeed, your problem is specific to your given API and has nothing to do with the form of its use from F#.
Using a ref cell is also an option here. Does this work?
let tl = ref null
let success = api.myFunction(tl)
The problem was with the API being compiled in .NET 2.0 which works fine under C# but not F#.

WeekOfYear in X++ using .net lib

static void Job5(Args _args)
{
int i;
System.DateTime netDttm;
System.Int32 intnet;
;
netDttm = new System.DateTime(2011,03,20 ,13,44,55);
intnet = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(netDttm, Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
i = intnet;
info(int2str(i));
}
I tried the in vb.net it works fine but doing the same in x++(using .net lib) it shows syntax error..All I am trying is to get the week no. from a supplied date. Any insight would be appreciated.
P.S. I found another solution to this which is I created a dll file in VS .net and added this to Reference node(AOT)of AX. It has shorten the code in AX
static void Job5(Args _args)
{
weekofyear.wof asd;
;
asd = new weekofyear.Wof();
print asd.weekofyr(today());
pause;
}
try this
int i;
System.DateTime netDttm;
System.Int32 intnet;
System.Globalization.CultureInfo cultureInfo;
System.Globalization.Calendar calendar;
System.Globalization.CalendarWeekRule calWeekRule
;
netDttm = new System.DateTime(2011,03,20 ,13,44,55);
cultureInfo = System.Globalization.CultureInfo::get_CurrentCulture();
calendar = cultureInfo.get_Calendar();
intnet = calendar.GetWeekOfYear(netDttm, System.Globalization.CalendarWeekRule::FirstFourDayWeek, System.DayOfWeek::Sunday);
i = intnet;
info(int2str(i));
[Note to any future readers: The following described an error in the original code Indranil posted; it does not apply to the code currently in the question, because Indranil fixed this error. The other error was dealt with in another answer from someone else :-).]
You shouldn't be passing a string as the first argument to GetWeekOfYear; it wants a System.DateTime (http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx). (At least, that's true in ordinary .NET; I don't know whether Dynamics AX does some other magical thing. I doubt it does.)
(But if and when you do want a date in the form of a string, those backslashes \ should be forward slashes /.)
Just make sure you load the correct CultureInfo if you expect this code to support word wide locations. Loading the current CultureInfo will load the servers preferred culture. If the user is en-gb and the server is en-us, you first day of week will be incorrect.
To load a specific cultureinfo you can simply do this:
System.Globalization.CultureInfo arCul = new System.Globalization.CultureInfo("en-US");
In the example chosen as answer, the code loads cultureinfo, but the cultureinfo is not used as parameter to the GetWeekOfYear method, which doesnt really make any sense. Instead you could send in the settings from the cultureinfo.

Resources