Is it possible to set a class variable using a different variable type [closed]Convert Unix Epoch Timestamp to DateTime and backWhat is the difference between String and string in C#?What is the difference between old style and new style classes in Python?Are static class variables possible?What is the difference between an abstract function and a virtual function?Type Checking: typeof, GetType, or is?Public Fields versus Automatic PropertiesPython class inherits objectIs there a reason for C#'s reuse of the variable in a foreach?What does “Could not find or load main class” mean?Why not inherit from List<T>?
Split into three!
How to write numbers and percentage?
Why A=2 and B=1 in the call signs for Spirit and Opportunity?
Are there any German nonsense poems (Jabberwocky)?
ifconfig shows UP while ip link shows DOWN
Can diplomats be allowed on the flight deck of a commercial European airline?
Are runways booked by airlines to land their planes?
Are PMR446 walkie-talkies legal in Switzerland?
Visual Block Mode edit with sequential number
Did Game of Thrones end the way that George RR Martin intended?
What is the use case for non-breathable waterproof pants?
Goldfish unresponsive, what should I do?
EU rights when flight delayed so much that return is missed
Moons and messages
What happened to the Dothraki in S08E06?
Why is the Eisenstein ideal paper so great?
Complications of displaced core material?
Piping the output of comand columns
Is keeping the forking link on a true fork necessary (Github/GPL)?
Who were the members of the jury in the Game of Thrones finale?
What did Brienne write about Jaime?
Unary Enumeration
Maximum interval between Alto & Tenor, & intervals when writing for SATB
Is it normal to "extract a paper" from a master thesis?
Is it possible to set a class variable using a different variable type [closed]
Convert Unix Epoch Timestamp to DateTime and backWhat is the difference between String and string in C#?What is the difference between old style and new style classes in Python?Are static class variables possible?What is the difference between an abstract function and a virtual function?Type Checking: typeof, GetType, or is?Public Fields versus Automatic PropertiesPython class inherits objectIs there a reason for C#'s reuse of the variable in a foreach?What does “Could not find or load main class” mean?Why not inherit from List<T>?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
is it possible to set a c# variable with a different variable type value than what the variable one is setting?
public class Test
public DateTime TimeSinceEpoch
get return TimeSinceEpoch;
set
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSinceEpoch = Epoch.AddMilliseconds(value); //<-the value that's expected is a DateTime value..
How can i write:
'TimeSinceEpoch = 1549090800000'
and use TimeSinceEpoch as a normal DateTime variable?
Without Having tow sepperate get and set variables inside the class
c# class
closed as unclear what you're asking by Ahmed Abdelhameed, Wai Ha Lee, Shiladitya, EdChum, meze Mar 24 at 14:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 6 more comments
is it possible to set a c# variable with a different variable type value than what the variable one is setting?
public class Test
public DateTime TimeSinceEpoch
get return TimeSinceEpoch;
set
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSinceEpoch = Epoch.AddMilliseconds(value); //<-the value that's expected is a DateTime value..
How can i write:
'TimeSinceEpoch = 1549090800000'
and use TimeSinceEpoch as a normal DateTime variable?
Without Having tow sepperate get and set variables inside the class
c# class
closed as unclear what you're asking by Ahmed Abdelhameed, Wai Ha Lee, Shiladitya, EdChum, meze Mar 24 at 14:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
what do you want to setTimeSinceEpoch
?
– Ashkan Mobayen Khiabani
Mar 23 at 22:09
1
Note that your current code has infinite recursion. That won't by itself prevent compiling, but there is no backing variable, just a circular reference back to the same property.
– Ben Voigt
Mar 23 at 22:11
1
Why not make another class method called for example SetTimeSinceEpoch(long milliseconds) that does the assignment you are looking for. Don’t use = for direct assignment unless you are using the same type.
– Mike Wodarczyk
Mar 23 at 22:13
1
Implicit conversions are described here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… They can be created usingoperator implicit
, but you aren't the author ofDateTime
orlong
so that approach won't work here.
– Ben Voigt
Mar 23 at 22:18
1
Implicit conversion is a conversion for which you don't need to do an explicit cast (conversion). (for example:int a = 5; float b = a;
the integer a is implicitly convertible to a float without you needing to writefloat b = (float) a;
)
– elgonzo
Mar 23 at 22:19
|
show 6 more comments
is it possible to set a c# variable with a different variable type value than what the variable one is setting?
public class Test
public DateTime TimeSinceEpoch
get return TimeSinceEpoch;
set
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSinceEpoch = Epoch.AddMilliseconds(value); //<-the value that's expected is a DateTime value..
How can i write:
'TimeSinceEpoch = 1549090800000'
and use TimeSinceEpoch as a normal DateTime variable?
Without Having tow sepperate get and set variables inside the class
c# class
is it possible to set a c# variable with a different variable type value than what the variable one is setting?
public class Test
public DateTime TimeSinceEpoch
get return TimeSinceEpoch;
set
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
TimeSinceEpoch = Epoch.AddMilliseconds(value); //<-the value that's expected is a DateTime value..
How can i write:
'TimeSinceEpoch = 1549090800000'
and use TimeSinceEpoch as a normal DateTime variable?
Without Having tow sepperate get and set variables inside the class
c# class
c# class
edited Mar 23 at 22:15
Ahmed Abdelhameed
6,95282349
6,95282349
asked Mar 23 at 22:06
SteinarSteinar
548
548
closed as unclear what you're asking by Ahmed Abdelhameed, Wai Ha Lee, Shiladitya, EdChum, meze Mar 24 at 14:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Ahmed Abdelhameed, Wai Ha Lee, Shiladitya, EdChum, meze Mar 24 at 14:39
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
what do you want to setTimeSinceEpoch
?
– Ashkan Mobayen Khiabani
Mar 23 at 22:09
1
Note that your current code has infinite recursion. That won't by itself prevent compiling, but there is no backing variable, just a circular reference back to the same property.
– Ben Voigt
Mar 23 at 22:11
1
Why not make another class method called for example SetTimeSinceEpoch(long milliseconds) that does the assignment you are looking for. Don’t use = for direct assignment unless you are using the same type.
– Mike Wodarczyk
Mar 23 at 22:13
1
Implicit conversions are described here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… They can be created usingoperator implicit
, but you aren't the author ofDateTime
orlong
so that approach won't work here.
– Ben Voigt
Mar 23 at 22:18
1
Implicit conversion is a conversion for which you don't need to do an explicit cast (conversion). (for example:int a = 5; float b = a;
the integer a is implicitly convertible to a float without you needing to writefloat b = (float) a;
)
– elgonzo
Mar 23 at 22:19
|
show 6 more comments
what do you want to setTimeSinceEpoch
?
– Ashkan Mobayen Khiabani
Mar 23 at 22:09
1
Note that your current code has infinite recursion. That won't by itself prevent compiling, but there is no backing variable, just a circular reference back to the same property.
– Ben Voigt
Mar 23 at 22:11
1
Why not make another class method called for example SetTimeSinceEpoch(long milliseconds) that does the assignment you are looking for. Don’t use = for direct assignment unless you are using the same type.
– Mike Wodarczyk
Mar 23 at 22:13
1
Implicit conversions are described here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… They can be created usingoperator implicit
, but you aren't the author ofDateTime
orlong
so that approach won't work here.
– Ben Voigt
Mar 23 at 22:18
1
Implicit conversion is a conversion for which you don't need to do an explicit cast (conversion). (for example:int a = 5; float b = a;
the integer a is implicitly convertible to a float without you needing to writefloat b = (float) a;
)
– elgonzo
Mar 23 at 22:19
what do you want to set
TimeSinceEpoch
?– Ashkan Mobayen Khiabani
Mar 23 at 22:09
what do you want to set
TimeSinceEpoch
?– Ashkan Mobayen Khiabani
Mar 23 at 22:09
1
1
Note that your current code has infinite recursion. That won't by itself prevent compiling, but there is no backing variable, just a circular reference back to the same property.
– Ben Voigt
Mar 23 at 22:11
Note that your current code has infinite recursion. That won't by itself prevent compiling, but there is no backing variable, just a circular reference back to the same property.
– Ben Voigt
Mar 23 at 22:11
1
1
Why not make another class method called for example SetTimeSinceEpoch(long milliseconds) that does the assignment you are looking for. Don’t use = for direct assignment unless you are using the same type.
– Mike Wodarczyk
Mar 23 at 22:13
Why not make another class method called for example SetTimeSinceEpoch(long milliseconds) that does the assignment you are looking for. Don’t use = for direct assignment unless you are using the same type.
– Mike Wodarczyk
Mar 23 at 22:13
1
1
Implicit conversions are described here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… They can be created using
operator implicit
, but you aren't the author of DateTime
or long
so that approach won't work here.– Ben Voigt
Mar 23 at 22:18
Implicit conversions are described here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… They can be created using
operator implicit
, but you aren't the author of DateTime
or long
so that approach won't work here.– Ben Voigt
Mar 23 at 22:18
1
1
Implicit conversion is a conversion for which you don't need to do an explicit cast (conversion). (for example:
int a = 5; float b = a;
the integer a is implicitly convertible to a float without you needing to write float b = (float) a;
)– elgonzo
Mar 23 at 22:19
Implicit conversion is a conversion for which you don't need to do an explicit cast (conversion). (for example:
int a = 5; float b = a;
the integer a is implicitly convertible to a float without you needing to write float b = (float) a;
)– elgonzo
Mar 23 at 22:19
|
show 6 more comments
5 Answers
5
active
oldest
votes
This isn't allowed even in C++/CLI which does have a syntax for it:
public ref struct Test
property System::DateTime TimeSinceEpoch
DateTime get();
void set(DateTime);
void set(long long);
;
You get the following error:
error C3902: 'set': type of last parameter must be 'System::DateTime'
Intellisense actually produces two complaints, depending on which order you write them:
E1930: a 'set' accessor was already declared for this property at
or
E1942: the last parameter of the 'set' accessor does not match the property type
The underlying problem is that a .NET property has to list a type and a maximum of one setter in the metadata (Have a look at PropertyInfo.GetSetMethod
-- it returns one setter or null
, not an array). Even though you could easily overload the setter function itself:
void set_TimeSinceEpoch(DateTime)
void set_TimeSinceEpoch(long)
only one of these two functions could be associated to the property.
You could use a helper type to allow multiple incoming types:
struct SuperDateTime
public DateTime Value;
public static implicit operator SuperDateTime(DateTime d) return new SuperDateTime Value = d ;
public static implicit operator SuperDateTime(long x) return new SuperDateTime Value = Epoch.AddMilliseconds(x) ;
public static implicit operator DateTime(SuperDateTime d) return d.Value;
but this will break readers of the property, who now have to say TimeSinceEpoch.Value
before using a member (if passing as an argument to another function, the implicit conversion will kick in). To overcome that, you'd need to write a forwarding function for every single member DateTime
has.
add a comment |
You need a private variable for DateTime and a long property. See code below :
public class Test
private DateTime Epoch get;set;
public long TimeSinceEpoch
get return Epoch.Ticks;
set
DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
Epoch = temp.AddMilliseconds(value);
1
This fails the "and useTimeSinceEpoch
as a normalDateTime
" goal stated in the question.
– Ben Voigt
Mar 23 at 22:16
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
add a comment |
You may just use it the way you do and to get Milliseconds:
var ms = (TimeSinceEpoch - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalMilliSeconds;
or maybe create an extension method for it like:
public static class Extensions
public static long MilliSeccondsSinceEpoch(this DateTime d)
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return (d - Epoch).TotalMillisecond;
Then you can use it like:
TimeSinceEpoch = DateTime.Now;
var ms = TimeSinceEpoch.MilliSeccondsSinceEpoch();
add a comment |
The following code works as i wanted it to. Hope it helps somone else
public class Test
private DateTime Time get;set;
public object TimeSinceEpoch
get
return (DateTime)Time
set
DateTime x = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
Time = x.AddMilliseconds(Convert.ToDouble(value));
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@jdweng: The essential change was to make the property typeSystem.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.
– Ben Voigt
Mar 25 at 17:43
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
add a comment |
You could achieve this using implicit casts:
public struct EpochBasedDateTime
public EpochBasedDateTime(DateTime value) => Value = value;
public EpochBasedDateTime(int milliseconds)
: this(Epoch.addMilliseconds(milliseconds)
public static readonly DateTime Epoch = new DateTime(1970,1,1);
public DateTime Value get;
public static implicit operator EpochBasedDateTime (int milliseconds)
=> new EpochBasedDateTime(milliseconds);
public static implicit operator DateTime (EpochBasedDateTime date)
=> date.Value;
So you can
public class Test
public EpochBasedDateTime TimeSinceEpoch get; set;
var test = new Test();
test.TimeSinceEpoch = 12345;
DateTime dt = test.TimeSinceEpoch;
However, you're probably better off explicitly converting your number to a date using a utility method:
public static class Epoch
public static readonly DateTime Value get; = new DateTime(1970,1,1);
public static DateTime AddMilliseconds(ms)
=> Value.AddMilliseconds(ms);
So you can
DateTime foo = Epoch.AddMilliseconds(12345);
or
DateTime foo = Epoch.Value.AddMilliseconds(12345);
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
This isn't allowed even in C++/CLI which does have a syntax for it:
public ref struct Test
property System::DateTime TimeSinceEpoch
DateTime get();
void set(DateTime);
void set(long long);
;
You get the following error:
error C3902: 'set': type of last parameter must be 'System::DateTime'
Intellisense actually produces two complaints, depending on which order you write them:
E1930: a 'set' accessor was already declared for this property at
or
E1942: the last parameter of the 'set' accessor does not match the property type
The underlying problem is that a .NET property has to list a type and a maximum of one setter in the metadata (Have a look at PropertyInfo.GetSetMethod
-- it returns one setter or null
, not an array). Even though you could easily overload the setter function itself:
void set_TimeSinceEpoch(DateTime)
void set_TimeSinceEpoch(long)
only one of these two functions could be associated to the property.
You could use a helper type to allow multiple incoming types:
struct SuperDateTime
public DateTime Value;
public static implicit operator SuperDateTime(DateTime d) return new SuperDateTime Value = d ;
public static implicit operator SuperDateTime(long x) return new SuperDateTime Value = Epoch.AddMilliseconds(x) ;
public static implicit operator DateTime(SuperDateTime d) return d.Value;
but this will break readers of the property, who now have to say TimeSinceEpoch.Value
before using a member (if passing as an argument to another function, the implicit conversion will kick in). To overcome that, you'd need to write a forwarding function for every single member DateTime
has.
add a comment |
This isn't allowed even in C++/CLI which does have a syntax for it:
public ref struct Test
property System::DateTime TimeSinceEpoch
DateTime get();
void set(DateTime);
void set(long long);
;
You get the following error:
error C3902: 'set': type of last parameter must be 'System::DateTime'
Intellisense actually produces two complaints, depending on which order you write them:
E1930: a 'set' accessor was already declared for this property at
or
E1942: the last parameter of the 'set' accessor does not match the property type
The underlying problem is that a .NET property has to list a type and a maximum of one setter in the metadata (Have a look at PropertyInfo.GetSetMethod
-- it returns one setter or null
, not an array). Even though you could easily overload the setter function itself:
void set_TimeSinceEpoch(DateTime)
void set_TimeSinceEpoch(long)
only one of these two functions could be associated to the property.
You could use a helper type to allow multiple incoming types:
struct SuperDateTime
public DateTime Value;
public static implicit operator SuperDateTime(DateTime d) return new SuperDateTime Value = d ;
public static implicit operator SuperDateTime(long x) return new SuperDateTime Value = Epoch.AddMilliseconds(x) ;
public static implicit operator DateTime(SuperDateTime d) return d.Value;
but this will break readers of the property, who now have to say TimeSinceEpoch.Value
before using a member (if passing as an argument to another function, the implicit conversion will kick in). To overcome that, you'd need to write a forwarding function for every single member DateTime
has.
add a comment |
This isn't allowed even in C++/CLI which does have a syntax for it:
public ref struct Test
property System::DateTime TimeSinceEpoch
DateTime get();
void set(DateTime);
void set(long long);
;
You get the following error:
error C3902: 'set': type of last parameter must be 'System::DateTime'
Intellisense actually produces two complaints, depending on which order you write them:
E1930: a 'set' accessor was already declared for this property at
or
E1942: the last parameter of the 'set' accessor does not match the property type
The underlying problem is that a .NET property has to list a type and a maximum of one setter in the metadata (Have a look at PropertyInfo.GetSetMethod
-- it returns one setter or null
, not an array). Even though you could easily overload the setter function itself:
void set_TimeSinceEpoch(DateTime)
void set_TimeSinceEpoch(long)
only one of these two functions could be associated to the property.
You could use a helper type to allow multiple incoming types:
struct SuperDateTime
public DateTime Value;
public static implicit operator SuperDateTime(DateTime d) return new SuperDateTime Value = d ;
public static implicit operator SuperDateTime(long x) return new SuperDateTime Value = Epoch.AddMilliseconds(x) ;
public static implicit operator DateTime(SuperDateTime d) return d.Value;
but this will break readers of the property, who now have to say TimeSinceEpoch.Value
before using a member (if passing as an argument to another function, the implicit conversion will kick in). To overcome that, you'd need to write a forwarding function for every single member DateTime
has.
This isn't allowed even in C++/CLI which does have a syntax for it:
public ref struct Test
property System::DateTime TimeSinceEpoch
DateTime get();
void set(DateTime);
void set(long long);
;
You get the following error:
error C3902: 'set': type of last parameter must be 'System::DateTime'
Intellisense actually produces two complaints, depending on which order you write them:
E1930: a 'set' accessor was already declared for this property at
or
E1942: the last parameter of the 'set' accessor does not match the property type
The underlying problem is that a .NET property has to list a type and a maximum of one setter in the metadata (Have a look at PropertyInfo.GetSetMethod
-- it returns one setter or null
, not an array). Even though you could easily overload the setter function itself:
void set_TimeSinceEpoch(DateTime)
void set_TimeSinceEpoch(long)
only one of these two functions could be associated to the property.
You could use a helper type to allow multiple incoming types:
struct SuperDateTime
public DateTime Value;
public static implicit operator SuperDateTime(DateTime d) return new SuperDateTime Value = d ;
public static implicit operator SuperDateTime(long x) return new SuperDateTime Value = Epoch.AddMilliseconds(x) ;
public static implicit operator DateTime(SuperDateTime d) return d.Value;
but this will break readers of the property, who now have to say TimeSinceEpoch.Value
before using a member (if passing as an argument to another function, the implicit conversion will kick in). To overcome that, you'd need to write a forwarding function for every single member DateTime
has.
answered Mar 23 at 22:32
Ben VoigtBen Voigt
239k30320590
239k30320590
add a comment |
add a comment |
You need a private variable for DateTime and a long property. See code below :
public class Test
private DateTime Epoch get;set;
public long TimeSinceEpoch
get return Epoch.Ticks;
set
DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
Epoch = temp.AddMilliseconds(value);
1
This fails the "and useTimeSinceEpoch
as a normalDateTime
" goal stated in the question.
– Ben Voigt
Mar 23 at 22:16
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
add a comment |
You need a private variable for DateTime and a long property. See code below :
public class Test
private DateTime Epoch get;set;
public long TimeSinceEpoch
get return Epoch.Ticks;
set
DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
Epoch = temp.AddMilliseconds(value);
1
This fails the "and useTimeSinceEpoch
as a normalDateTime
" goal stated in the question.
– Ben Voigt
Mar 23 at 22:16
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
add a comment |
You need a private variable for DateTime and a long property. See code below :
public class Test
private DateTime Epoch get;set;
public long TimeSinceEpoch
get return Epoch.Ticks;
set
DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
Epoch = temp.AddMilliseconds(value);
You need a private variable for DateTime and a long property. See code below :
public class Test
private DateTime Epoch get;set;
public long TimeSinceEpoch
get return Epoch.Ticks;
set
DateTime temp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
Epoch = temp.AddMilliseconds(value);
answered Mar 23 at 22:15
jdwengjdweng
18.7k2917
18.7k2917
1
This fails the "and useTimeSinceEpoch
as a normalDateTime
" goal stated in the question.
– Ben Voigt
Mar 23 at 22:16
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
add a comment |
1
This fails the "and useTimeSinceEpoch
as a normalDateTime
" goal stated in the question.
– Ben Voigt
Mar 23 at 22:16
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
1
1
This fails the "and use
TimeSinceEpoch
as a normal DateTime
" goal stated in the question.– Ben Voigt
Mar 23 at 22:16
This fails the "and use
TimeSinceEpoch
as a normal DateTime
" goal stated in the question.– Ben Voigt
Mar 23 at 22:16
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
The code is just a sample and needs some minor tweaks. I just posted the missing private variable. I know the code as posted isn't going to properly convert unix time to windows time.
– jdweng
Mar 23 at 22:18
add a comment |
You may just use it the way you do and to get Milliseconds:
var ms = (TimeSinceEpoch - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalMilliSeconds;
or maybe create an extension method for it like:
public static class Extensions
public static long MilliSeccondsSinceEpoch(this DateTime d)
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return (d - Epoch).TotalMillisecond;
Then you can use it like:
TimeSinceEpoch = DateTime.Now;
var ms = TimeSinceEpoch.MilliSeccondsSinceEpoch();
add a comment |
You may just use it the way you do and to get Milliseconds:
var ms = (TimeSinceEpoch - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalMilliSeconds;
or maybe create an extension method for it like:
public static class Extensions
public static long MilliSeccondsSinceEpoch(this DateTime d)
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return (d - Epoch).TotalMillisecond;
Then you can use it like:
TimeSinceEpoch = DateTime.Now;
var ms = TimeSinceEpoch.MilliSeccondsSinceEpoch();
add a comment |
You may just use it the way you do and to get Milliseconds:
var ms = (TimeSinceEpoch - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalMilliSeconds;
or maybe create an extension method for it like:
public static class Extensions
public static long MilliSeccondsSinceEpoch(this DateTime d)
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return (d - Epoch).TotalMillisecond;
Then you can use it like:
TimeSinceEpoch = DateTime.Now;
var ms = TimeSinceEpoch.MilliSeccondsSinceEpoch();
You may just use it the way you do and to get Milliseconds:
var ms = (TimeSinceEpoch - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalMilliSeconds;
or maybe create an extension method for it like:
public static class Extensions
public static long MilliSeccondsSinceEpoch(this DateTime d)
DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return (d - Epoch).TotalMillisecond;
Then you can use it like:
TimeSinceEpoch = DateTime.Now;
var ms = TimeSinceEpoch.MilliSeccondsSinceEpoch();
answered Mar 23 at 22:21
Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani
23.7k1868125
23.7k1868125
add a comment |
add a comment |
The following code works as i wanted it to. Hope it helps somone else
public class Test
private DateTime Time get;set;
public object TimeSinceEpoch
get
return (DateTime)Time
set
DateTime x = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
Time = x.AddMilliseconds(Convert.ToDouble(value));
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@jdweng: The essential change was to make the property typeSystem.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.
– Ben Voigt
Mar 25 at 17:43
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
add a comment |
The following code works as i wanted it to. Hope it helps somone else
public class Test
private DateTime Time get;set;
public object TimeSinceEpoch
get
return (DateTime)Time
set
DateTime x = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
Time = x.AddMilliseconds(Convert.ToDouble(value));
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@jdweng: The essential change was to make the property typeSystem.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.
– Ben Voigt
Mar 25 at 17:43
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
add a comment |
The following code works as i wanted it to. Hope it helps somone else
public class Test
private DateTime Time get;set;
public object TimeSinceEpoch
get
return (DateTime)Time
set
DateTime x = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
Time = x.AddMilliseconds(Convert.ToDouble(value));
The following code works as i wanted it to. Hope it helps somone else
public class Test
private DateTime Time get;set;
public object TimeSinceEpoch
get
return (DateTime)Time
set
DateTime x = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
Time = x.AddMilliseconds(Convert.ToDouble(value));
edited Mar 23 at 22:36
answered Mar 23 at 22:33
SteinarSteinar
548
548
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@jdweng: The essential change was to make the property typeSystem.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.
– Ben Voigt
Mar 25 at 17:43
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
add a comment |
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@jdweng: The essential change was to make the property typeSystem.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.
– Ben Voigt
Mar 25 at 17:43
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
This fails the "and use TimeSinceEpoch as a normal DateTime" goal stated in the question, because anyone reading from the property will have to apply a cast.
– Ben Voigt
Mar 23 at 22:34
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
Your edit did precisely nothing.
– Ben Voigt
Mar 23 at 22:36
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@BenVoigt : The new code uses a variable (Time) and a property (TimeSinceEpoch) while original code only had a property.
– jdweng
Mar 25 at 10:24
@jdweng: The essential change was to make the property type
System.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.– Ben Voigt
Mar 25 at 17:43
@jdweng: The essential change was to make the property type
System.Object
. Adding the field fixed the infinite recursion but didn't address the main point of the question in any way.– Ben Voigt
Mar 25 at 17:43
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
Look closer at solution and original code.
– jdweng
Mar 25 at 20:51
add a comment |
You could achieve this using implicit casts:
public struct EpochBasedDateTime
public EpochBasedDateTime(DateTime value) => Value = value;
public EpochBasedDateTime(int milliseconds)
: this(Epoch.addMilliseconds(milliseconds)
public static readonly DateTime Epoch = new DateTime(1970,1,1);
public DateTime Value get;
public static implicit operator EpochBasedDateTime (int milliseconds)
=> new EpochBasedDateTime(milliseconds);
public static implicit operator DateTime (EpochBasedDateTime date)
=> date.Value;
So you can
public class Test
public EpochBasedDateTime TimeSinceEpoch get; set;
var test = new Test();
test.TimeSinceEpoch = 12345;
DateTime dt = test.TimeSinceEpoch;
However, you're probably better off explicitly converting your number to a date using a utility method:
public static class Epoch
public static readonly DateTime Value get; = new DateTime(1970,1,1);
public static DateTime AddMilliseconds(ms)
=> Value.AddMilliseconds(ms);
So you can
DateTime foo = Epoch.AddMilliseconds(12345);
or
DateTime foo = Epoch.Value.AddMilliseconds(12345);
add a comment |
You could achieve this using implicit casts:
public struct EpochBasedDateTime
public EpochBasedDateTime(DateTime value) => Value = value;
public EpochBasedDateTime(int milliseconds)
: this(Epoch.addMilliseconds(milliseconds)
public static readonly DateTime Epoch = new DateTime(1970,1,1);
public DateTime Value get;
public static implicit operator EpochBasedDateTime (int milliseconds)
=> new EpochBasedDateTime(milliseconds);
public static implicit operator DateTime (EpochBasedDateTime date)
=> date.Value;
So you can
public class Test
public EpochBasedDateTime TimeSinceEpoch get; set;
var test = new Test();
test.TimeSinceEpoch = 12345;
DateTime dt = test.TimeSinceEpoch;
However, you're probably better off explicitly converting your number to a date using a utility method:
public static class Epoch
public static readonly DateTime Value get; = new DateTime(1970,1,1);
public static DateTime AddMilliseconds(ms)
=> Value.AddMilliseconds(ms);
So you can
DateTime foo = Epoch.AddMilliseconds(12345);
or
DateTime foo = Epoch.Value.AddMilliseconds(12345);
add a comment |
You could achieve this using implicit casts:
public struct EpochBasedDateTime
public EpochBasedDateTime(DateTime value) => Value = value;
public EpochBasedDateTime(int milliseconds)
: this(Epoch.addMilliseconds(milliseconds)
public static readonly DateTime Epoch = new DateTime(1970,1,1);
public DateTime Value get;
public static implicit operator EpochBasedDateTime (int milliseconds)
=> new EpochBasedDateTime(milliseconds);
public static implicit operator DateTime (EpochBasedDateTime date)
=> date.Value;
So you can
public class Test
public EpochBasedDateTime TimeSinceEpoch get; set;
var test = new Test();
test.TimeSinceEpoch = 12345;
DateTime dt = test.TimeSinceEpoch;
However, you're probably better off explicitly converting your number to a date using a utility method:
public static class Epoch
public static readonly DateTime Value get; = new DateTime(1970,1,1);
public static DateTime AddMilliseconds(ms)
=> Value.AddMilliseconds(ms);
So you can
DateTime foo = Epoch.AddMilliseconds(12345);
or
DateTime foo = Epoch.Value.AddMilliseconds(12345);
You could achieve this using implicit casts:
public struct EpochBasedDateTime
public EpochBasedDateTime(DateTime value) => Value = value;
public EpochBasedDateTime(int milliseconds)
: this(Epoch.addMilliseconds(milliseconds)
public static readonly DateTime Epoch = new DateTime(1970,1,1);
public DateTime Value get;
public static implicit operator EpochBasedDateTime (int milliseconds)
=> new EpochBasedDateTime(milliseconds);
public static implicit operator DateTime (EpochBasedDateTime date)
=> date.Value;
So you can
public class Test
public EpochBasedDateTime TimeSinceEpoch get; set;
var test = new Test();
test.TimeSinceEpoch = 12345;
DateTime dt = test.TimeSinceEpoch;
However, you're probably better off explicitly converting your number to a date using a utility method:
public static class Epoch
public static readonly DateTime Value get; = new DateTime(1970,1,1);
public static DateTime AddMilliseconds(ms)
=> Value.AddMilliseconds(ms);
So you can
DateTime foo = Epoch.AddMilliseconds(12345);
or
DateTime foo = Epoch.Value.AddMilliseconds(12345);
edited Mar 23 at 23:04
answered Mar 23 at 22:42
realbartrealbart
2,0991722
2,0991722
add a comment |
add a comment |
what do you want to set
TimeSinceEpoch
?– Ashkan Mobayen Khiabani
Mar 23 at 22:09
1
Note that your current code has infinite recursion. That won't by itself prevent compiling, but there is no backing variable, just a circular reference back to the same property.
– Ben Voigt
Mar 23 at 22:11
1
Why not make another class method called for example SetTimeSinceEpoch(long milliseconds) that does the assignment you are looking for. Don’t use = for direct assignment unless you are using the same type.
– Mike Wodarczyk
Mar 23 at 22:13
1
Implicit conversions are described here: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… They can be created using
operator implicit
, but you aren't the author ofDateTime
orlong
so that approach won't work here.– Ben Voigt
Mar 23 at 22:18
1
Implicit conversion is a conversion for which you don't need to do an explicit cast (conversion). (for example:
int a = 5; float b = a;
the integer a is implicitly convertible to a float without you needing to writefloat b = (float) a;
)– elgonzo
Mar 23 at 22:19