I am using chrono. I have now() and some other NaiveDateTime. How can I find a difference between them?
let now = Utc::now().naive_utc();
let dt1 = get_my_naive_datetime();
In more recent versions of chrono (at least as of 0.4.22, and likely earlier), you can simply subtract NaiveDateTimes:
println!("{:?}", dt1 - now);
The result is a Duration, which has methods to convert to whatever units you like, e.g. (dt1 - now).num_days().
In older versions of chrono, you must use NaiveDateTime::signed_duration_since:
println!("{:?}", dt1.signed_duration_since(now));
Related
I am using swift5 and I am trying to sum by property like this:
let total = realm.objects(Purchase.self).sum(ofProperty: "amount")
but i get this error:
Type of expression is ambiguous without more context
what can I do?
Probably the easist solution is to let the compiler know what kind of result the sum will be.
Like this
let total: Double = realm.objects(Purchase.self).sum(ofProperty: "orderProperty")
print(total)
This tells the compiler than total will always be a Double
As a side note, Double's are (generally) not a good type to work with for financial situations. Look into Decimal (akin to NSDecimal and Decimal128)
#Persisted var decimal: Decimal128
in newer versions
I am using chrono. I have now() and some other NaiveDateTime. How can I find a difference between them?
let now = Utc::now().naive_utc();
let dt1 = get_my_naive_datetime();
In more recent versions of chrono (at least as of 0.4.22, and likely earlier), you can simply subtract NaiveDateTimes:
println!("{:?}", dt1 - now);
The result is a Duration, which has methods to convert to whatever units you like, e.g. (dt1 - now).num_days().
In older versions of chrono, you must use NaiveDateTime::signed_duration_since:
println!("{:?}", dt1.signed_duration_since(now));
I tried to do some simple UIView layout math in swift and tried the following line of code...
var offset: CGFloat = (bounds.width / 2.0) - ((sortedSymptoms.count * bounds.height) / 2.0)
and got the following error from the compiler:
cannot invoke '-' with an argument list of type '(($T6), ($T17))'
var offset: CGFloat = (bounds.width / 2.0) - ((sortedSymptoms.count * bounds.height) / 2.0)
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The compiler error isn't all that helpful, but it looks like there's a type conflict between Double, Int, and CGFloat of some kind. I was able to get the line to compile by sprinkling in some explicit creations of CGFloats, but I can't believe that this is the right way to do this.
var offset: CGFloat = (bounds.width / CGFloat(2.0)) - ((CGFloat(sortedSymptoms.count) * bounds.height) / CGFloat(2.0))
What's the right way?
This is a known issue in Swift and the dev team has been working on improving the issue around CGFloat in particular. But at this time, yes, that's how you write it.
Some followup from devforums (which may make you happy or sad, but at least roughly explains the current status): https://devforums.apple.com/message/1026028#1026028
Note that the main issue here is that the literal 2.0 doesn't coerce to CGFloat, which it arguably should. But count will likely always require a cast, by intent. You cannot always safely convert between numeric types, and Swift intentionally forces you to consider each time you do these kinds of casts. But it should be possible to determine if a literal conversion is safe at compile-time, so that should be fixable.
I am unable to get any in-built method for retrieving milli-second part of the given date-time.
Method current-dateTime() returns the full date & time along with milli-seconds, but I am looking for in-built one just to return millisecond.
In-built methods are available for hours, minutes and for seconds but not for milliseconds.
Please note, I do not wish to get the unix-timestamp
Indeed, such a function for getting just the milliseconds appears to be missing from XQuery, however this is most likely because if you read the spec, then seconds are modeled as a decimal rather than as two separate seconds and milliseconds components.
Another alternative to your answer is to use a regular expression:
replace(string($currentTime), ".*\.([0-9]*)[+\-].*", "$1")
As I found no default method, I used following approach to get milliseconds
let $sec := fn:seconds-from-dateTime($currentTime)
let $splitSeconds := fn:tokenize(fn:string($sec), '\.')
let $seconds := $splitSeconds[1]
let $milliSeconds := $splitSeconds[2]
I have a beginner question about dates and String in Haskell.
I need to get part of date (year, month or day) as String in Haskell. I found out, that if I write the following two lines in GHCi
Prelude> now <- getCurrentTime
Prelude> let mon = formatTime defaultTimeLocale "%B" now
then mon is of type String. However, I am unable to put this in a function. I tried for instance the following:
getCurrMonth = do
now <- getCurrentTime
putStrLn (formatTime defaultTimeLocale "%B" now)
But this returns type IO () and I need String (also not IO String, only String).
I understand that do statement creates a monad, which I don't want, but I have been unable to find any other solution for getting date in Haskell.
So, is there any way to write a function like this?
Thanks in advance for any help!
If you want to return a String representing the current time, it will have to be in the IO monad, as the value of the current time is always changing!
What you can do is to return a String in the IO monad:
> getCurrMonth :: IO String
> getCurrMonth = do
> now <- getCurrentTime
> return (formatTime defaultTimeLocale "%B" now)
then, from your top level (e.g. in main), you can pass the String around:
> main = do
> s <- getCurrMonth
> ... do something with s ...
If you really want a pure function of that sort, then you need to pass in the time explicitly as a parameter.
import System.Locale (defaultTimeLocale)
import System.Time (formatCalendarTime, toUTCTime, getClockTime, ClockTime)
main = do now <- getClockTime
putStrLn $ getMonthString now
getMonthString :: ClockTime -> String
getMonthString = formatCalendarTime defaultTimeLocale "%B" . toUTCTime
Notice how getMonthString can be pure since the IO action getClockTime is performed elsewhere.
I used the old-time functions, because I was testing it out on codepad, which apparently doesn't have the newer time package. :( I'm new to the old time functions so this might be off a couple hours since it uses toUTCTime.
As Don said, there's no way to avoid using monads in this situation. Remember that Haskell is a pure functional language, and therefore a function must always return the same output given a particular input. Haskell.org provides a great explanation and introduction here that is certainly worth looking at. You'd also probably benefit from monad introduction like this one or a Haskell I/O tutorial like this one. Of course there are tons more resources online you can find. Monads can initially be daunting, but they're really not as difficult as they seem at first.
Oh, and I strongly advise against using unsafePerformIO. There's a very good reason it has the word "unsafe" in the name, and it was definitely not created for situations like this. Using it will only lead to bad habits and problems down the line.
Good luck learning Haskell!
You can't get just a String, it has to be IO String. This is because getCurrMonth is not a pure function, it returns different values at different times, so it has to be in IO.