Converting a double to UTCTime - datetime

The title is all it is.
Assume I have a double:
let d = 11241241.10124012 :: Double
How do I get an instance of UTCTime corresponding to d, d as in unix seconds?

You need to convert the double to a fractional in order for it to be understood as a NominalDiffTime value (which is an instance of Fractional). Once it is understood as a NominalDiffTime value, then posixSecondsToUTCTime will handle the conversion (POSIXTime being a type synonym of NominalDiffTime).
import Data.Time.Clock
import Data.Time.Clock.POSIX(posixSecondsToUTCTime)
doubleToUTCTime d = posixSecondsToUTCTime $ realToFrac d

Related

Parsed F# DateTime value not equal to the original DateTime value

Consider the following code that converts a DateTime to a string, tries to parse it using the TryParse method on DateTime, and returns an DateTime option indicating whether parsing the string was successful or not:
let dt = DateTime.Now
printfn "%A" dt
let parseDateTime (str: string) =
match DateTime.TryParse str with
| (true, v) -> Some v
| _ -> None
let parsedDateTime =
dt.ToString () |> parseDateTime
match parsedDateTime with
| Some v ->
printfn "%A" v
assert (v = dt)
| _ -> ()
The assert statement fails, which I would not expect it to. printfn prints out the same string to the Console which further puzzles me.
If I change the assert statement above to assert (v.ToString() = dt.ToString()) the assert passes as I would have expected when comparing the two DateTime values.
Is there something that I'm missing here when it comes to DateTime equality? I've also tested this using int and Guid (swapping out DateTime.TryParse for Int32.TryParse and Guid.TryParse respectively) and the assert passes as I would expect it to when comparing values for equality.
The resolution of a DateTime value is a single "tick", which represents one ten-millionth of a second. You also need to consider time zone differences. The default output format doesn't include this level of detail. If you want to "round trip" a DateTime, write it out with ToString("o")and then use DateTimeStyles.RoundtripKind when parsing:
open System
open System.Globalization
let parseDateTime (str: string) =
match DateTime.TryParse(str, null, DateTimeStyles.RoundtripKind) with
| (true, v) -> Some v
| _ -> None
let parsedDateTime =
dt.ToString ("o") |> parseDateTime // e.g. "2021-11-15T15:36:07.6506924-05:00"

Kotlin DateTimeParseException

Getting date from https://api.spacexdata.com/v3/launches
This date have format: 2006-03-25T10:30:00+12:00.
I want convert it to "dd, mm, yyyy", but always getting error:
"java.time.format.DateTimeParseException: Text '2006-03-25T10:30:00+12:00' could not be parsed, unparsed text found at index 10"
my code:
val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)
var launchDateConverted: String= myDate.toString()
i getting data at String, then i convert it to date for formatting, and after i converting date back to string thats to display at UI. i used different methods, but cannot find the correct way.
My current locale is "RU".
You do not need any formatter to parse your input string
You input string, 2006-03-25T10:30:00+12:00 is already in the default format used by OffsetDateTime#parse and therefore, you do not need to use a formatter explicitly in order to parse your input date-time string.
m specifies the minute while M specifies the month
You have wrongly used m for the month for which the symbol is M. Check the documentation page to learn more about these symbols.
Prefer u to y
y specifies the year-of-era (era is specified as AD or BC) and is always a positive number whereas u specifies the year which is a signed (+/-) number. Normally, we do not use + sign to write a positive number but we always specify a negative number with a - sign. The same rule applies for a year. As long as you are going to use a year of the era, AD (which is mostly the case), both, y and u will give you the same number. However, the difference occurs when you use a year of the era, BC e.g. the year-of-era, 1 BC is specified as year, 0; the year-of-era, 2 BC is specified as year, -1 and so on. You can understand it better with the following demo:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Testing {
public static void main(String[] args) {
System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
System.out.println();
System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
System.out.println();
System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
}
}
Output:
-1 1 1
2 1 1
2BC 1 1
0 1 1
1 1 1
1BC 1 1
1 1 1
1 1 1
1AD 1 1
Note: I've used Java to demonstrate the solution but it will also work in Kotlin.
The final solution:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2006-03-25T10:30:00+12:00");
System.out.println(odt);
// Format it into the desired pattern
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd, MM, uuuu", Locale.US);
String formatted = dtf.format(odt);
System.out.println(formatted);
}
}
Output:
2006-03-25T10:30+12:00
25, 03, 2006
Learn more about the modern date-time API from Trail: Date Time.
Your formatter does not match the input format. Basically you need two formatters: one for input and one for output.
The format "dd, mm, yyyy" is wrong: mm stands for minute of hour, not for month. You should use "dd, MM, yyyy".
val launchDate = "2006-03-25T10:30:00+12:00"
val inputFormatter = DateTimeFormatter.ISO_DATE_TIME
val myDate = LocalDate.parse(launchDate, inputFormatter)
val outputFormatter = DateTimeFormatter.ofPattern("dd, MM, yyyy", Locale.US)
println(outputFormatter.format(myDate))

From Julia DateTime to Int64 and back

In order to store a DateTime array to a HDF5 database, I first converted each DateTime to Int64 and did the reverse upon retrieval. The code looks as follows:
using Dates
a = Array{DateTime, 1}(undef, 2)
a[1] = DateTime("2020-01-01")
a[2] = DateTime("2020-01-02")
# Convert to Int64 array
msecs = [d.instant.periods.value for d in a]
# Code to store to hdf5 here (skipped for now)
# Code to retrieve from hdf5 here (skipped for now)
# Convert back to array of DateTime
b = [DateTime(0) + Millisecond(msec) for msec in msecs]
Question1: Is it the best way to convert back and forth between DateTime and Int64?
Question2: Can we directly store DateTime in hdf5 without conversion or is this the way to go?

Why can't parse transform float string into int?

When I try to use parse this way: parse(Int64, "3.1459"), I get an error, because '.' is an invalid base 10 digit. I know the error gets raised because of the period, but is there any particular reason why Julia couldn't convert a float string to a integer like this? Any other way to do it?
Well, it isn't an integer so it isn't really clear what it should return. You could just parse it as a float and then round it as you want, e.g:
julia> v = parse(Float64, "3.1459")
3.1459
julia> trunc(Int, v)
3
julia> ceil(Int, v)
4
julia> round(Int, v)
3
I'm not sure the error gets raised because of the period - rather because you can parse a decimal into an integer without specifying how you want to round:
julia> parse(Float64, "3.14159")
3.14159
julia> Int(round(parse(Float64, "3.14159")))
3

How to store the ASCII value in a character (Ada 83 Only)

How do I store the ascii value of an integer (say 33) in a character. I want something like this in Ada83, not 95
C: Code
char c = 10;
char *k = &c;
strncat (des, k, 1);
printf("%s",des);
Thank you!!
C : Character := Character'Val(10);
or
C : Character := ASCII.LF;
The first works in all versions of Ada. The second one was the standard way in Ada 83; it is now obsolescent. The newer way is
C : Character := Ada.Characters.Latin_1.LF;
More information: In Ada, Character is an enumeration type, not an integer type. Therefore, you can't assign an integer to it directly. The 'Val attribute is the Ada way to convert an integer to an enumeration; Enum_Type'Val(N) means "the Nth enumeration literal defined for the enumeration type, 0-relative". To go the other way, Enum_Type'Pos(E) returns the integer corresponding to the position of E in the enumeration list.

Resources