How to parse an ISO string value to a NodaTime Instant? - nodatime

I am getting to know NodaTime and like it a lot. But I don't know it that well (yet)!
Given a value such as '2014-04-08T09:30:18Z', what are the steps required to parse such a string to a NodaTime Instant?
Thank you!

I figured this out. For others who want to do the same thing, here is what I used:
var isoString = "2014-04-08T09:30:18Z";
var result = InstantPattern.GeneralPattern.Parse(isoString).Value;
The Value property in this case returns the actual Instant object. If you omit that, the result is of type ParseResult<Instant> in this case, and has other information such as whether the parsing succeeded, etc.
http://nodatime.org/1.2.x/api/html/T_NodaTime_Text_ParseResult_1.htm
There aren't a lot of examples on Noda Time yet, but I am really liking it and turning to it more and more. Fantastic work by the team who created it. Thank you!

Related

Python 3.10 datetime strptime not picking up time zone?

I have a timestamp embedded in some JSON data as a string, for ease of inspection and modification. An example looks like this:
"debug_time": 1670238819.9747384,
"last_saved": "2022-12-05 11:13:39.974725 UTC",
When loaded back in, I need to convert it back to a float for comparison against time.time() and similar things, however, I can't seem to find the magic incantations to have it restore the correct value.
In restoring the JSON data, I attempt to convert the string to a float via strptime() like this:
loaded_time = datetime.datetime.strptime(obj.last_saved, '%Y-%m-%d %H:%M:%S.%f %Z')
This does restore the timestamp to a valid datetime object, however calling .tzname() results in None, and my attempts to use loaded_time.replace(tzinfo=zoneinfo.ZoneInfo('UTC')) have not yielded any useful results.
In short, emitting loaded_time.timestamp() yields 1670267619.974725, which is 8 hours ahead of what it should be. I've tried using .astimezone(), in various permutations, but can't find a way to correctly have it convert to the client's local time.
I even tried to hard-code in my own time zone US/Pacific but it stubbornly refuses to give me that original debug_time value back.
This doesn't seem like it should be a difficult problem, but clearly I'm misunderstanding something about how python 3's time handling works. Any ideas are welcome!
Thank you for your time!
you have to use built in function replace like
.strftime("%Y-%m-%dT%H:%M:%S.%f%Z").replace("UTC", "")

Get BuiltInParameterId from BuiltIn Parameter ElementId in Revit

Is there a Way to get the BuiltInParameterId (Ex:BuiltInParameter.SHEET_SIZE)
from a Parameter ElementId.
I have a number extracted from an Schedule Field (-1010106)
and I want to get the BuildInParameter-id.
Currently I am doing it like this:
BIPdic = {i.value__ : i for i in BuiltInParameter.GetValues(BuiltInParameter)}
bipid= BIPdic[-1010106]
I could not find an easier way. (Its easy, but I have to built a dictionary
from all (over 3000 BuiltInParameters)).
THX
tillbaum
I am not absolutely sure I know what you mean. Check out the description of the ElementId constructor taking a BuiltInParameter input argument.
You can also take a look at the built-in parameter checker BipChecker and its BipChecker GitHub repo. It iterates over all built-in parameter values and tries to retrieve a parameter value for each one.
That sounds pretty similar to what you are after with your dictionary.

Formatting user input with .title() method

Currently, I started to learn programming with Python. Currently, I am tackling my first 'real' project.
The most problems that I seem to find, are based around formatting. I am curious if my code is written understandable. For example:
new_customer = input('Name? \n')
customer = new_customer.title()
I want to make sure all the inputs received are saved with the same format.
Is this the correct way to transform a user's input with the .title() method?
Thank you in advance, I hope that these kind of questions are accepted here!
You can use the same variable:
newcustomer = input('Name? \n').title()
Is this what you mean?

Dart DateTime.parse() string date return by ServiceStack

I have date string return by ServiceStack : 2013-08-25T12:06:32.8770000 but error when convert to date of Dart
DateTime.parse(mapAccount[Account.RECCREATED]);
it ok when call
DateTime.parse((mapAccount[Account.RECCREATED] as String).substring(0, 26));
Is there anyway to fix it. Thanks you.
Looks like the string don't match the internally used regular expression:
r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'
But the regular expression doesn't support more that 6 digits for the milliseconds (and microseconds) part, but you supply 7 digits.
The documentation does not state which formats are supported, but gives some examples. They only state that they support a subset of ISO 8601.
Looks like your solution is the only one a the moment.
Not sure if this should be treaded as a bug, but if you think it is a bug, create a bug report here.
See the docs about DateTime.parse for more details. Looks like the problems with the parse function is already in the bug tracker.

LevenshteinSim() Approximate string matching

I am using levenshteinSim() to do the approximate string matching. I am facing a problem
here is what my data look like
string = "Mitchell"
stringvector = c("Ray Mitchell", "Mitchell Dough","Juila Mitch")
.
I want the algorithm to match only second part of the Stringvector, not the first half..How do i do it. I really appreciate your help. And how do I use weighing schema?
Thanks
Kothavari
I believe you will need to preprocess the data to just pull out the second part of the string and use the algo on that.
Other people seem to do some preproessing first. See here

Resources