How to Convert UTC to localTIme(C# )

£

£¤£¤£¤

I have got the value of user account's lastlogon time. Its type is Int64.
This value is stored as a large integer that represents the number of 100
nanosecond intervals since January 1, 1601 (UTC)(Refer to MSDN).
I don't know how to convert this value to localTime.

The following is my code.
################################################################
DirectoryEntry deUser = new DirectoryEntry(ldappath);
DirectorySearcher src = new DirectorySearcher(deUser);
src.Filter = "(&(objectClass=user)(SAMAccountName=" +
accountName + "))";
SearchResult result = src.FindOne();
ResultPropertyCollection accountProperties = result.Properties;

foreach (object lastlogon in accountProperties["lastlogon"])
{
//Int64 lastlogon =129046401906875000 ;//this is the value of user
account's lastlogon time.
double aa=Convert.ToDouble(lastlogon.ToString()); // I am not sure that
this is a correct and good method
DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
dtZone.AddSeconds(aa/10000000);//10^7 ,AddSeconds method needs a double
type parameter.
lbLastLogon.Text = dtZone.ToLocalTime().ToString(); //it displays 1601-1-1
8:0;0,it is
not correct
localtime
}
##################################################################################




I try to use number dirctly as following. Then It returns "2009-12-7
6:16:30" ,Year ,month,day are right ,but the rest is not right.I am in
China.It may need add 8 hours additionally.
**********************************************************
DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
lbLastLogon .Text =dtZone
..AddSeconds(12904640190687500000/1000000000).ToString ()
**********************************************************
 
G

Göran Andersson

£¤£¤£¤ said:
I have got the value of user account's lastlogon time. Its type is Int64.
This value is stored as a large integer that represents the number of 100
nanosecond intervals since January 1, 1601 (UTC)(Refer to MSDN).
I don't know how to convert this value to localTime.

The following is my code.
################################################################
DirectoryEntry deUser = new DirectoryEntry(ldappath);
DirectorySearcher src = new DirectorySearcher(deUser);
src.Filter = "(&(objectClass=user)(SAMAccountName=" +
accountName + "))";
SearchResult result = src.FindOne();
ResultPropertyCollection accountProperties = result.Properties;

foreach (object lastlogon in accountProperties["lastlogon"])
{
//Int64 lastlogon =129046401906875000 ;//this is the value of user
account's lastlogon time.
double aa=Convert.ToDouble(lastlogon.ToString()); // I am not sure that
this is a correct and good method
DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
dtZone.AddSeconds(aa/10000000);//10^7 ,AddSeconds method needs a double
type parameter.
lbLastLogon.Text = dtZone.ToLocalTime().ToString(); //it displays 1601-1-1
8:0;0,it is
not correct
localtime
}
##################################################################################




I try to use number dirctly as following. Then It returns "2009-12-7
6:16:30" ,Year ,month,day are right ,but the rest is not right.I am in
China.It may need add 8 hours additionally.
**********************************************************
DateTime dtZone = new DateTime(1601, 1, 1, 0, 0, 0);
lbLastLogon .Text =dtZone
.AddSeconds(12904640190687500000/1000000000).ToString ()
**********************************************************

The DateTime type uses a 100 nanosecond value, but with the origin at
0001-01-01 instead of 1601-01-01, so you just have to add the
difference. Use DateTimeKind.Utc to create an UTC DateTime value:

DateTime utc = new DateTime(lastlogon + 504911232000000000,
DateTimeKind.Utc);

Now you can just convert it to local time:

DateTime local = utc.ToLocalTime();
 
T

Tim Roberts

£¤£¤£¤ said:
1.What does it mean by 504911232000000000?

He told you that. It's the number of 100ns ticks between 0001-01-01 and
1601-01-01.
2.You said"with the origin at 0001-01-01 instead of 1601-01-01", but why
MSDN http://msdn.microsoft.com/en-us/library/ms676823(VS.85).aspx
says"intervals since January 1, 1601 (UTC)."?

The Last-Login attribute starts at 1601-01-01. The number in the DateTime
type is origined at 0001-01-01. The number you showed (129046401906875000)
is about 408 years, so it is must be based from 1601. So, if you want to
initialize a DateTime variable with that integer, you need to add the
difference between 0001 and 1601.

The ToLocalTime method of the DateTime class will take your timezone into
account.

class What
{
public static void Main()
{
Int64 i1 = 129046401906875000;
Int64 i2 = 504911232000000000;
DateTime dt1 = new DateTime( i1 + i2 );
DateTime dt2 = new DateTime( 1601, 1, 1, 0, 0, 0 );
Console.WriteLine( dt1 );
Console.WriteLine( dt1.ToLocalTime() );
Console.WriteLine( dt2 );
}
}
 
G

Göran Andersson

Top