|
i'm communicating wiht a remote sever and i get a datetime as a long value, is there any method that converts the long to datetime directly...
|
|
|
|
|
There is no any direct way to convert long value to datetime.
You need to first convert it to string then you can convert to datetime.
Try following code for the same...
DateTime dateTime;
if (DateTime.TryParse(input.ToString(), out dateTime))
{
Console.WriteLine(dateTime);
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
prasadbuddhika wrote: i'm communicating wiht a remote sever and i get a datetime as a long value, is there any method that converts the long to datetime directly...
Depends on what encoded within that long-value. Is it a numerical representation of the date, like the number "12012002"? Or is it encoded as a number of days since a certain date?
I are Troll
|
|
|
|
|
Is the remote server returning you a Unix time by any chance? If so, you can convert it into a DateTime using:
DateTime localTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(unixTime).ToLocalTime(); This takes things such as Daylight Savings Time into account.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
You need to make sure the number you have is a number of ticks, as defined by:
"(in .NET) time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar", so you may need a linear transformation;
You then can use the constructor new DateTime(long ticks)
PS: you do not need any string for this.
|
|
|
|
|
thanx for your replies ..
the date given by the sever is in this format "20100615072733" , i found a method
DateTime.TryParseExact(), and i tried following way , "s" contains the date value string
dateTime = DateTime.TryParseExact(s, "yyyy/MM/dd hh:mm:ss tt", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateTime);
but still i get the default date.
any help on this ...!
|
|
|
|
|
well, as the string doesn't have any / spaces or : characters it's not suprising.
Try:
DateTime dateTime = DateTime.ParseExact(s, "yyyyMMddhhmmss", CultureInfo.InvariantCulture, DateTimeStyles.None);
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
hey thanx Dave and all who helped me , exactly as u mentioned it works when the "/" are removed ... thanx..
|
|
|
|
|
yeah, when they put Exact in a method name, you'd have to be extra careful.
|
|
|
|
|
LOL!
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
I try not to use global values as I belive that dependancys can be designed in a way that it should be dependant on as few objects as possible howeever I am making a small game and require some behavioural structure and came up with this:
public sealed class EnterMineAndDigForGold : IState<Miner>
{
static readonly EnterMineAndDigForGold _instance = new EnterMineAndDigForGold();
public static IState<Miner> Instance
{
get
{
return _instance;
}
}
public void Enter(Miner owner)
{
if (owner.Location != LocationType.Mine)
{
owner.Location = LocationType.Mine;
Console.WriteLine("Walked into the mine");
}
}
public void Execute(Miner owner)
{
owner.AddToGoldCarried(1);
Console.WriteLine("Picked up some gold");
owner.IncreaseFatigue(1);
Console.WriteLine("Getting more fatigued");
if (owner.PocketFull)
{
}
if (owner.IsThirsty)
{
}
}
public void Exit(Miner owner)
{
Console.WriteLine(owner.GetType().Name + " Is leaving the mine");
}
}
Which I have been learning from an AI book have learnt so far.
Is this ok to do?
modified on Tuesday, June 15, 2010 6:10 AM
|
|
|
|
|
"Global variables" were considered "evil", mainly because of the abuse - Singletons are not inherently evil.
Imagine people setting boolean flags to simulate communication between parts of the application
I are Troll
|
|
|
|
|
Eddy Vluggen wrote: Singletons are not inherently evil.
Unless there are too many of them.
|
|
|
|
|
Luc Pattyn wrote: Unless there are too many of them
That would be confusing. It would become evil if you'd try to justify it by calling it obfuscation
Quoad vos spirum, vos sperum?
|
|
|
|
|
I was thinking that would be the one case (obfuscation) of good justification for too many globals.
In Christ,
Aaron Laws
http://ProCure.com
|
|
|
|
|
One thing to note about this example is that it's not a singleton. It's a global property called Instance that is accessed via the EnterMineAndDigForGold class.
The key point about a singleton is that there is only one instance at any point in time and there can never be more than one. There is nothing stopping me from ignoring the Instance property on EnterMineAndDigForGold and creating as many instances of EnterMineAndDigForGold as I want.
In order to make this a singleton you would need to make sure that no-one can create any instances of EnterMineAndDigForGold other than the one created and held as the static variable _instance.
The easiest way to do this is to make the constructor for EnterMineAndDigForGold private. That will stop any other class from creating one of these.
To be honest, looking at your class I don't think it really matters because your EnterMineAndDigForGold class doesn't have any instance state so it doesn't matter whether you create a new one every time, or share one around, or make all the methods static so that you never need an instance at all.
|
|
|
|
|
Singletons are unnecessary (though not necessarily evil) in C#, especially since the advent of static classes.
Only use singletons in lesser languages; like C++.
|
|
|
|
|
What is the best way to determine an Object Variable is null or Empty ?
(or is not Null and Empty)?
|
|
|
|
|
I know the best method for checking null, and that is very simple.
Just compare your object with null keyword.
if(obj==null)
{
}
But for checking emptiness of your object you need to prepare custom method
of your object and check emptiness of all member.
Class Employee
{
String FirstName;
String LastName;
public bool IsEmpty()
{
if(!String.IsNullOrEmpty(FirstName) || !String.IsNullOrEmpty(LasttName))
return False;
else
return True;
}
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
If it's a string then use string.IsNullOrEmpty .
If it's for another class then it depends on how you define empty. If this is for your own classes you could define an IEmpty interface and implement that.
public interface IEmpty
{
bool IsEmpty { get; }
}
then you can use a generic method such as
public static bool IsNullOrEmpty<T>(T obj) where T : IEmpty
{
if (obj == null)
return true;
return obj.IsEmpty;
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Thanks
Will You Please Give me an example on how to use this codes.
|
|
|
|
|
OK, here's an example class. The implementation of the interface is the method at the end.
public class IntString : IEmpty
{
private string text;
private int value;
public IntString()
: this(0, string.Empty)
{ }
public IntString(int value)
: this(value, string.Empty)
{ }
public IntString(string text)
: this(0, text)
{ }
public IntString(int value, string text)
{
this.text = text;
this.value = value;
}
public string Text
{
get { return text; }
set { text = value; }
}
public int Value
{
get { return value; }
set { this.value = value; }
}
public bool IsEmpty
{
get { return value == 0 && string.IsNullOrEmpty(text); }
}
}
To use it, combined with the method and interface declaration i posted earlier you can do something like this...
IntString intString = new IntString();
Console.WriteLine(IsNullOrEmpty<IntString>(intString));
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Thanks for reply,
I think that u didnt get my question:
I wanna something like :
Object objVariable;
if(!String.IsNullOrEmpty(objVariable))
Instead of doing this :
Object objVariable;
if(objVariable !=null && objVariable.ToString() !="")
|
|
|
|
|
As I posted above[^] then
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Thanks
Will You Please Give me an example on how to use this code.
|
|
|
|