DateTime to time_t bug

G

Guest

Hi..

I'm writing a web service in C# and needs to produce some time_t values in the xml output, and near as I can figure there appears to be a bug in the C# datetime arithmetic. Below is an example in asp.net using the JScript Date object to demonstrate the bug. The net problem is that the number of seconds calculation ends up an hour off (an extra hour). If anyone can point out a better way to run the calculation I'd apprecitate it

Thanks
-Mar

timebug.aspx
<%@Language="C#" EnableSessionState="false"%><script language="C#" runat="server"
void PrintCSDate (

DateTime now = DateTime.Now, t_base = DateTime.Parse("Thu, 1 January 1970 00:00:00 GMT")
TimeSpan tt = now - t_base
double update_time = System.Math.Floor (tt.TotalSeconds)

Response.Write ("Now: "+now+"<br>\n")
Response.Write ("Seconds (time_t): "+update_time+"<br>\n")
Application ["tbugSeconds"] = (long) update_time; // pass seconds in Applicatio

DateTime ttnow = t_base.AddSeconds (update_time)
Response.Write ("TTNow: "+ttnow+"<br>\n")

</script><
PrintCSDate()
Server.Transfer ("timebugj.aspx")
%

timebugj.aspx
<%@Language="Jscript" EnableSessionState="false"%><script language="Jscript" runat="server"
function PrintJDate (
{ var secs = Application ["tbugSeconds"]*1000; // get offset from C# cod
var d = new Date (secs)
Response.Write ("JDate: "+d+"<br>\n")

</script><% PrintJDate(); %>
 
P

Patrice

Parse converts the GTM time to local time. You don't convert back to GMT at
the end.

Try the same code with just "Thu, 1 January 1970" instead if you want to
work only in local time...

Patrice

Mark said:
Hi...

I'm writing a web service in C# and needs to produce some time_t values in
the xml output, and near as I can figure there appears to be a bug in the C#
datetime arithmetic. Below is an example in asp.net using the JScript Date
object to demonstrate the bug. The net problem is that the number of
seconds calculation ends up an hour off (an extra hour). If anyone can
point out a better way to run the calculation I'd apprecitate it.
Thanks.
-Mark

timebug.aspx:
<%@Language="C#" EnableSessionState="false"%><script language="C#" runat="server">
void PrintCSDate ()
{
DateTime now = DateTime.Now, t_base = DateTime.Parse("Thu, 1 January 1970 00:00:00 GMT");
TimeSpan tt = now - t_base;
double update_time = System.Math.Floor (tt.TotalSeconds);

Response.Write ("Now: "+now+"<br>\n");
Response.Write ("Seconds (time_t): "+update_time+"<br>\n");
Application ["tbugSeconds"] = (long) update_time; // pass seconds in Application

DateTime ttnow = t_base.AddSeconds (update_time);
Response.Write ("TTNow: "+ttnow+"<br>\n");
}
</script><%
PrintCSDate();
Server.Transfer ("timebugj.aspx");
%>

timebugj.aspx:
<%@Language="Jscript" EnableSessionState="false"%><script
language="Jscript" runat="server">
function PrintJDate ()
{ var secs = Application ["tbugSeconds"]*1000; // get offset from C# code
var d = new Date (secs);
Response.Write ("JDate: "+d+"<br>\n");
}
</script><% PrintJDate(); %>
 
S

Steven Cheng[MSFT]

Hi Mark,

I've searched in the newsgroup and found some former threads discussing on
the similiar problem:

http://groups.google.com/groups?q=.net+datetime+time_t&hl=en&lr=&ie=UTF-8&se
lm=ueEgZ0eHCHA.1744%40tkmsftngp13&rnum=1

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=eEW$hQhZBHA.145
2%40tkmsftngp04&rnum=18&prev=/groups%3Fq%3D.net%2Bdatetime%2Btime_t%26hl%3De
n%26lr%3D%26ie%3DUTF-8%26start%3D10%26sa%3DN

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=eZtze6g$BHA.198
0%40tkmsftngp04&rnum=24&prev=/groups%3Fq%3D.net%2Bdatetime%2Btime_t%26hl%3De
n%26lr%3D%26ie%3DUTF-8%26start%3D20%26sa%3DN

Hope they'll also help. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top