How do I convert from an IronPython datetime to a .NET DateTime? - datetime

Literally the inverse of this question, is there an easy way to get a .Net DateTime from an IronPython datetime?
Clearly, one could
Output a string and parse it or
Dump all the date parts into a DateTime constructor
but those are both messy. This doesn't work either:
pydate = datetime.datetime.now()
csharp = DateTime(pydate) # Crashes, because .Net wants a 'long' for Ticks
Is there an easy cast or a short way to get the Ticks that .Net wants?

I was fairly certain a direct conversion was already allowed, but I was wrong. I added it in 31f5c88 but that won't be available until (currently unscheduled) 2.7.6.
In the meantime the best way would be to use the timetuple method to get the parts:
dt = datetime.now()
d = DateTime(*dt.timetuple()[:6])
# For UTC times, you need to pass 'kind' as a kwarg
# because of Python's rules around using * unpacking
udt = datetime.now()
ud = DateTime(*udt.timetuple()[:6], kind=DateTimeKind.Utc)

Now that 2.7.6 has been released, you can use clr.Convert to make an explicit cast, if needed. There could be better ways to do this, but I'm stealing this one from Jeff Hardy's commit.
>>> from System import DateTime
>>> from datetime import datetime
>>> from clr import Convert
>>> now_py = datetime.now()
>>> now_py
datetime.datetime(2020, 1, 1, 18, 28, 34, 598000)
>>> Convert(now_py, DateTime)
<System.DateTime object at 0x00000000006C [1/1/2020 6:28:34 PM]>
>>> now_py == Convert(now_py, DateTime)
True
DateTime(pydate) still crashes.

Jeff's answer gets a DateTime to the second. If you want to stretch the precision to the millisecond, you can use something like this:
def cs_date(date):
return DateTime(*date.timetuple()[:6] + (date.microsecond/1000,))

Related

Round back to quarter-hour in Mulesoft Dataweave

I have a Mule Server 4.3.0 EE application, and in it I want to round a DateTime to its most recent quarter-hour, with the result also being a DateTime. Some examples:
if the input is 9:18:32 AM, then the output is 9:15:00 AM the same day
if the input is 9:33:33 AM, then the output is 9:30:00 AM the same day
if the input is 9:59:58 AM, then the output is 9:45:00 AM the same day
if the input is 10:00:00 AM, then the output is 10:00:00 AM the same day
In this application, the input will always be in New York time, with DST in effect or not depending on the time of year.
Here's what I've come up with so far. Keep the date and hour, truncate the minutes to the nearest 15, set the seconds to zero, and keep the time zone. (There's a special case for '00' minutes because it won't convert if the minutes are a single '0'.)
I know newer Mule versions have the Dates package which would make this much more elegant, but upgrading isn't currently an option for me. Is this about as good as the code can be for those requirements, or is there a better way to do this without upgrading Mule? Thank you.
%dw 2.0
import * from dw::core::Strings
output application/json
fun roundBack(inputTime: DateTime): DateTime =
(inputTime[0 to 13] ++
(if (inputTime.minutes < 15) '00' else (inputTime.minutes - mod(inputTime.minutes, 15) as String)) ++
':00' ++
inputTime[-6 to -1]
) as DateTime
I believe that the logic is good but the implementation is fragile because it depends on auto coercions (DateTime to Strings when using the range selector []). It is better for me to be explicit in the conversions to avoid issues caused by unexpected defaults formats and warnings in the editor:
%dw 2.0
import * from dw::core::Strings
output application/json
fun roundBack(inputTime: DateTime): DateTime =
(
inputTime as String {format: "yyyy-MM-dd HH:"}
++ (if (inputTime.minutes < 15)
'00'
else (inputTime.minutes - mod(inputTime.minutes, 15)) as String
)
++':00'
++ inputTime as String {format: "xxx"}
) as DateTime {format: "yyyy-MM-dd HH:mm:ssxxx"}
---
roundBack(|2020-10-01T23:57:59-04:00|)
Output: "2020-10-01 23:45:00-04:00"

How to format a date in Visual Basic?

In Visual Basic, I have an object with an ItemDate key in it with the value being assigned to a label in ASPX. Here is the code:
lblItemDate.Text = .ItemDate
The result on the front end is '2021/11/15'. I want the result to be: 'November 15, 2021'
What do I need to do in Visual Basic to make the result on the front end be 'November 15, 2021' instead of '2021/11/15'?
In another VB file, the ItemDate object key is created this way:
oItem.ItemDate = Trim(odbcReader("ItemDate").ToString)
Assuming you really have a .Net DateTime struct:
lblItemDate.Text = .ItemDate.ToString("MMMM dd, yyyy")
Otherwise you have a string, in which case you want to parse into a .Net DateTime struct so you can use the same ToString() call above:
Dim MyDate As DateTime = DateTime.ParseExact( .ItemDate, "yyyy/MM/dd")
lblItemDate.Text = MyDate.ToString("MMMM dd, yyyy")
Even better if you can update your code so ItemDate is a DateTime value in the first place, and the Parse() call is moved to the point where the object is first created.
It's been a while, so I don't recall whether the .ItemDate shortcut is available in the context of a function call. You may need to use the full version of the variable name.
Besides the .Net functions mentioned in another answer, which work across different languages, there is also the Format(..) function traditionally included with professional implementations of BASIC since the early 70's (I first used it in DEC's BASIC-Plus in 1976).
To get 'November 15, 2021' you'd do it like this:
lblItemDate.Text = Format(.ItemDate, "MMMM d, yyyy")

C# asp.net core 2.2 - Get current date time - Not Accurate

I tried everything to get the current year/month/date/hour but it doesn't work. I always get data 1 hour behind. So if it's 17:00 in the Netherlands, I get 16:00. I have tried so many things, but I can't get this fixed.. is it a bug?
I Tried this:
string dateTimeNowPlusForecaseHour = DateTimeOffset.Now.ToLocalTime().AddHours(hour).ToString("yyyyMMddhh");
And this:
string dateTimeNowPlusForecaseHour = DateTimeOffset.UtcNow.ToLocalTime().AddHours(hour).ToString("yyyyMMddhh");
And this:
string dateTimeNowPlusForecaseHour = DateTimeOffset.UtcNow.ToLocalTime().AddHours(hour).ToString("yyyyMMddhh");
And this:
string dateTimeNowPlusForecaseHour = DateTimeOffset.Now.AddHours(hour).ToString("yyyyMMddhh");
And this:
public string GetForecastTime(int hour)
{
// Take amount of seconds elapsed since 1970
var amountOfSecondsElapsedSince1970 = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
// Make a Datetime variable (1970 is the start)
System.DateTime dateTimeNowString = new DateTime(1970, 1, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
// Add amountOfSecondsElapsedSince1970 to the date time starting at 1970, + the amount of hours (for each forecast so 0, 1, and 2)
dateTimeNowString = dateTimeNowString.AddSeconds(amountOfSecondsElapsedSince1970).AddHours(hour).ToLocalTime();
// This is the dateTime
string dateRealWeatherForecastTimee = dateTimeNowString.ToString("yyyyMMddhh");
return dateRealWeatherForecastTimee;
}
Nothing seems to work.
Update
I tried the following line on a new asp.net core console project (rather than the project I'm working on):
string dateTimeNowPlusForecaseHour = DateTime.Now.AddHours(hour).ToString("yyyyMMddhh");
That works! But why doesn't it work for the project I'm working on?
Update2.0:
When I view my timezone of the project, I get GMT which is wrong. When I create a new asp.net core console project and view the timezone, I get w. europe, which gives me the correct time... Why is the timezone wrong? Shouldn't that be correct automatically?
DateTime.Now returns the current time and day. The struct it returns
can be stored as a field or property in a class. We look into this
property and its implementation—where it accesses the operating system
for the current time.
string dateTimeNowPlusForecaseHour = DateTime.Now.ToString("yyyyMMddHH");
This screenshot is taken from here. And I've used almost all of these, which means these are tested and should definitely work. If you still get incorrect hour, please check the time-zone of the server where you're hosting your application.

How to ignore milliseconds and format using DateTimeFormatter

I am trying to use DateTimeParser and DateTimeFormatter to format the date time. My requirement is to format the input string to "yyyy-MM-dd'T'HH:mm:ssZ" format irrespective of whether the input has milliseconds or not. I am using below code to parse the input. But so far I can get it working for only with or without milliseconds.
This is my method:
public static DateTime convertDateTime(String dateTime){
DateTimeParser parsers = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").getParser();
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(parsers ).toFormatter().withLocale(Locale.US).withChronology(ISOChronology.getInstanceUTC());
return DateTime.parse(dateTime,formatter);
}
Imports used :
import org.joda.time.DateTime;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;
I want my code to accept 2 different input forms(with or without milliseconds) and returns one DateTime without milliseconds.
Examples:
input can be:
2020-10-01T12:05:22.458-04:00
2020-10-01T12:05:22-04:00
Output:
2020-10-01T12:05:22-04:00
Any help on this will be greatly appreciated. Thanks!
You are reinventing the ISO_ZONED_DATE_TIME.
For this format, milliseconds are already optional. You only have to truncate them when present, to get your desired result. The format is even the standard, so the code is as simple as:
String[] samples = {
"2020-10-01T12:05:22.458-04:00",
"2020-10-01T12:05:22-04:00",
};
for(String s: samples) {
ZonedDateTime parsed = ZonedDateTime.parse(s).with(ChronoField.MILLI_OF_SECOND, 0);
System.out.println(parsed);
}
2020-10-01T12:05:22-04:00
2020-10-01T12:05:22-04:00
Note that I used MILLI_OF_SECOND to make the solution more intuitive regarding your task of removing the milliseconds. Actually, milliseconds are just nanoseconds with a lower precision, so you could use the even simpler ZonedDateTime.parse(s).withNano(0), once it has been understood.
Or, as suggested by Ole V.V., ZonedDateTime.parse(s).truncatedTo(ChronoUnit.SECONDS), which might be even easier to grasp.

EntityFunction.TruncateTime not working in my query

I am using Linq to entityframework to query some infomration. I am trying to use entityfunction.truncatetime and it doesnt seem to work as expected. here is my sample query
From d In Request
Where d.Requestor= "XXXX" And d.ProcessedFlag = "N"
Select d.RequestID, RequestReason = d.RequestReason.ItemValue, RequestType = d.RequestType.ItemValue, RequestedDate = EntityFunctions.TruncateTime(d.RequestedMoveDate)
The requesteddate doesnt seem to truncate the time part and I am still getting the both Date and time.
Am I missing something here?
In .NET, the DateTime class actually represents both a date and a time. Internally, this is stored as a numeric value represented by the number of 100-nanosecond "ticks" since Midnight, January 1, 1001 AD. This number gets "converted" when it's displayed (either in output or in a debugger). This conversion is done via a format string.
Even if you truncate a DateTime's time portion, it still has a time... it's just 00:00:00, and if you don't want to see that time, you need to adjust your format string to not convert that.
Thus, if you do something like this: DateTime.Now.Date it will display `10/15/2012 00:00:00" if you use the default date conversion string (or whatever is the default format for your culture).
If you want to only display the Date portion, then you must do something like myDate.ToShortDateString() or myDate.ToString("d").
EntityFunctions is a set of tools designed to be used in Linq to Entities queries, because doing DateTime formatting is not normally allowed in a query.
For example, this code does not work:
var q = from x in dc where x.BirthDate == DateTime.Now.AddYears(-15).Date select x;
You have to do it like this:
var q = from x in dc
where x.Birthdate == EntityFunctions.TruncateTime(DateTime.Now.AddYears(-15))
select x;
This will then generate the correct SQL to do date comparisons in SQL code. This is what the EntityFunctions are designed for, not truncating dates in the select portion (although it does work). But, even though the date is truncated, it will still have a Time component, it will just be 00:00:00, and you must use a date format string to present it to your users in the manner you intend.
cant you use ToShortDateString() like below?
List<DateTime> time = new List<DateTime>();
time.Add(DateTime.Now);
var WhatDate = from date in time
select new { Date = date.ToShortDateString() };
In your case try this
From d In Request
Where d.Requestor= "XXXX" And d.ProcessedFlag = "N"
Select new{ RequestID = d.RequestID, RequestReason = d.RequestReason.ItemValue, RequestType = d.RequestType.ItemValue, RequestedDate = d.RequestedMoveDate.ToShortDateString()};

Resources