Parsing a datetime string to local time in rust chrono - datetime

Having trouble with a simple problem. I have a string that does not include timezone information that I need to parse into a DateTime struct. I can get it as UTC, but not local:
let from = NaiveDateTime::parse_from_str(&start_date, "%Y-%m-%dT%H:%M:%S")?;
let from_utc = DateTime::<Utc>::from_utc(from, Utc);

You need Local.from_local_datetime() to convert a NaiveDateTime into a DateTime<Local>:
let from: NaiveDateTime = start_date.parse().unwrap();
let date_time = Local.from_local_datetime(&from).unwrap();
Admittedly, this isn't really easy to find in the documentation.
The first line you have works fine as well. For this particular format (RFC3339), it's easier to use str::parse(), though.

Related

Issue in converting Unix timestamp get from moment to normal date

I am using moment().valueOf() to get unix timestamp and to get the normal time from unix time i'm using moment.unix(unix_time).format('LLL') when the unix value is 1606985226404 getting the anser as May 2, 52893 6:36 AM which is incorrect. What is the issue with this?
According to the documentation of Moment.JS, moment() works with milliseconds.
So:
const unix_time = moment().valueOf(); // return epoc in milliseconds
const now = moment.unix(unix_time / 1000).format('LLL').
That's the trick.

Poco::LocalDateTime::timestamp() not converting timestamps to UTC

According to the header file of Poco::Timestamp, timestamps are in UTC, see Timestamp documentation. If timestamps are in UTC, shouldn't a method converting a Poco::LocalDateTime to Poco::Timestamp make sure that the returned timestamp is in UTC? Currently, Poco::LocalDateTime::timestamp() does not do this, and the returned timestamp is in local time.
It's especially strange since the assignment operator Poco::LocalDateTime::operator = (const Timestamp& timestamp) does a UTC to local time conversion. The following code asserts because of this:
Poco::LocalDateTime local1 = Poco::LocalDateTime( 2020, 1, 30 );
Poco::Timestamp timestamp = local1.timestamp();
Poco::LocalDateTime local2 = timestamp;
assert( local1 == local2 );
local1 will not have the same value as local2 in this example. Am I the only one who think this is strange behavior?
If you look at LocalDateTime::timestamp() you will see that it converts the timestamp before returning via Timestamp::fromUtcTime so that function returns a Timestamp in Local time, not UTC time.
You can use the Timestamp::utcTime() function or the Timestamp::raw() function but those return different types to prevent you from accidentally doing the wrong thing.
What are you actually trying to achieve here?

Querying dates in Realm React Native

[EDITED]
I'd like to query and filter by dates in realm. Here's my code:
let factories = realm.objects('Factory')
for(factory of factories) {
let toys = realm.objects('Toy').filtered('factory_id == factory.id')
let lowerBound = new Date(year + '-1-1T00:00:00Z')
let uppperBound = new Date(year + '-2-1T00:00:00Z')
let janToys = toys.filtered('created_at > lowerBound AND created_at < uppperBound')
}
year is a variable declared before the code snippet above.
This doesn't work and I'm pretty sure it's because the date format is not correct.
Here is the date format when I log toys:
Fri Mar 24 2017 16:01:59 GMT+0800 (Malay Peninsula Standard Time)
I'd like to know how to query realm dates. I can't find it in the documentation and other posts here. I'd appreciate any help. If this is not a date format issue, please tell me.
Thank you!
EDIT: I added the outside loop. This may not be a date format issue. Here is the error message:
Error: Predicate expressions must compare a keypath and another keypath or a constant value`
It seems that Realm does not support dot operators inside its queries. Forcing Realm to be relational is not allowed. As a solution, I transformed my usage to follow NoSQL convention. Passing in objects instead of declaring relationships.

What does moment(testDate, "x") do?

I have the following piece of code before me:
var testDate = 1481103000000;
var enterTime = moment(testDate, "x");
console.log(enterTime);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
The variable enterTime results in a momentjs object with an additional _f property set to "x" and a _pf property of type object (see the console log) compared to a normal moment(testDate) object.
I couldn't find information on the _f or _pf properties anywhere.
Can anyone tell me what the "x" stands for and for what reason it is being used?
Thanks in advance.
With moment(testDate, "x"); you are creating a moment object using moment(String, String); function specifying x as format (Unix ms timestamp).
When you do moment(testDate), you are creating a moment object using moment(Number);.
All moment properties starting with _ are for internal use, _f stands for format, while _pf stand for Parsing Flags.
You can have a look to moment code to get more details about _f and _pf.
x denotes Unix ms timestamp
Please note that this parameter is case-sensitive:
X Output: 1410715640.579 Unix timestamp
x Output: 1410715640579 Unix ms timestamp
Refer here for all the options.

Get local time in seconds using Qt

I am stuck with this problem.
I already got the currentUTCtime in seconds from the QDateTime.
Problem is, I can't find a possible way to convert this into the local time in seconds. There are some QDate functions like toLocalTime() which just don't seem to work. I hope somebody here can help me.
QDateTime::currentMSecsSinceEpoch();
QDateTime currentateTime = QDateTime::currentDateTime();
QDateTime UTC(QDateTime::currentDateTimeUtc());
currentDateTime.toString().toStdString();
TimeNow = currentDateTime.toMSecsSinceEpoch()/1000;
Above is my code for the currentUTC Time in seconds.
If you just need the time in seconds since the epoch you can use QDateTime::toTime_t(); this method exists in Qt 4.7 and seems to be a part of Qt 5 from the start, too.
QDateTime::currentDateTime().toTime_t()
for local time, or for UTC
QDateTime::currentDateTimeUtc().toTime_t()
Use QDateTime::fromTime_t, to which the documentation states:
Returns a datetime whose date and time are the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC) and converted to the given spec.
qint64 utcTime = QDateTime::currentMSecsSinceEpoch();
QDateTime localTime = QDateTime::fromTime_t(utcTime, Qt::LocalTime);

Resources