By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

DateTime to time_t bug

P: n/a
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(); %>
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <ms********@lycos-inc.com> a écrit dans le message de
news:43**********************************@microsof t.com...
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(); %>

Nov 18 '05 #2

P: n/a
Hi Mark,

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

http://groups.google.com/groups?q=.n...r=&ie=UTF-8&se
lm=ueEgZ0eHCHA.1744%40tkmsftngp13&rnum=1

http://groups.google.com/groups?hl=e...%24hQhZBHA.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=e...ze6g%24BHA.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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.