473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DateTime to time_t bug

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
2 2096
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Claire | last post by:
How can I convert date-time data in time_t to C# DateTime class format? e.g. I read in a file a time_t value of 1070390676. How can I convert it to a DateTime? Any help appreciated. Thx, ...
1
by: trellow | last post by:
Does anyone know how to convert data that was written to a file in the "time_t" format to the C# DateTime type? Thanks in advance!
7
by: John J. Hughes II | last post by:
Is there a better way of doing this? DateTime startTime = new DateTime(1970,1,1,0,0,0,0); TimeSpan currTime = DateTime.Now - startTime; UInt32 time_t =...
2
by: Chris | last post by:
Hello, How can i convert a C++ CTime (4 bytes) into a C# DateTime ? (my CTime is read in a file). Thanks, -- Chris
22
by: Merlin | last post by:
Hello everyone, I realize that I'm new to the group, but I'm hoping that someone might be able to help me out. What I'm doing is using the GNU 7zip command line utility to make a backup on my...
6
by: minboymike | last post by:
Hello everyone, I am working on a project when I have to convert a C# program into Native C for speed purposes. I am using a .dll that creates a DateTime object. Obviously, there is not...
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
2
by: winston | last post by:
Hi, I've been trying to search for this for a while now and can never find anything. Here's my situation. I'm writing a c++ MFC application and I'm using MSXML6 to parse an xml file. I need to...
4
by: PeteOlcott | last post by:
I need to convert time_t to System::DateTime. All of the examples that I found are either in C# or in the obsolete managed C++ syntax. What is the current syntax for converting a (probably 64 bit)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.