Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

How to get or convert the corrent value of datetime? #19

Open
Songjiadong opened this issue Apr 28, 2021 · 1 comment
Open

How to get or convert the corrent value of datetime? #19

Songjiadong opened this issue Apr 28, 2021 · 1 comment

Comments

@Songjiadong
Copy link

I use the phoenix to create a table (eg:user table) including the property using date type,then get this value using PrepareAndExecuteRequestAsync method.But I find the value's Rep is integer type not JavaSqlDate or JavaSqlTime or JavaUtilDate and its columnclassname is "java.sql.Date",so How to get or convert the corrent value of datetime? Could you give me an an example? Thanks.

@egeaydin
Copy link

egeaydin commented Jun 24, 2021

Hey mate,

I know it's been a little while you posted this but in case anybody stumbles on this,

Basically, I discovered that if the data type from DB is Date, then the value returned would be Rep.Integer which would indicate a number of days after 1/1/1970. If the date type from DB is DateTime, then the value returned is Rep.Long which indicates the number of milliseconds after 1/1/1970.

public DateTime ConvertToCSharpType(TypedValue value)
{
    var startDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    if (value.Type == Rep.Integer)
        return startDate.AddDays(value.NumberValue);
    else if (value.Type == Rep.Long)
        return startDate.AddMilliseconds(value.NumberValue);
    else
        throw new NotImplementedException($"Cannot convert value type {value.Type} to DateTime because it is not implemented");
}`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants