473,657 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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# " EnableSessionSt ate="false"%><s cript 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.Flo or (tt.TotalSecond s)

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

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

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

timebugj.aspx
<%@Language="Js cript" EnableSessionSt ate="false"%><s cript language="Jscri pt" runat="server"
function PrintJDate (
{ var secs = Application ["tbugSecond s"]*1000; // get offset from C# cod
var d = new Date (secs)
Response.Write ("JDate: "+d+"<br>\n ")

</script><% PrintJDate(); %>
Nov 18 '05 #1
2 2106
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********@lyc os-inc.com> a écrit dans le message de
news:43******** *************** ***********@mic rosoft.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# " EnableSessionSt ate="false"%><s cript 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.Flo or (tt.TotalSecond s);

Response.Write ("Now: "+now+"<br>\n") ;
Response.Write ("Seconds (time_t): "+update_time+" <br>\n");
Application ["tbugSecond s"] = (long) update_time; // pass seconds in Application
DateTime ttnow = t_base.AddSecon ds (update_time);
Response.Write ("TTNow: "+ttnow+"<br>\n ");
}
</script><%
PrintCSDate();
Server.Transfer ("timebugj.aspx ");
%>

timebugj.aspx:
<%@Language="Js cript" EnableSessionSt ate="false"%><s cript language="Jscri pt" runat="server"> function PrintJDate ()
{ var secs = Application ["tbugSecond s"]*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%40tkmsftng p13&rnum=1

http://groups.google.com/groups?hl=e...%24hQhZBHA.145
2%40tkmsftngp04 &rnum=18&pre v=/groups%3Fq%3D.n et%2Bdatetime%2 Btime_t%26hl%3D e
n%26lr%3D%26ie% 3DUTF-8%26start%3D10% 26sa%3DN

http://groups.google.com/groups?hl=e...ze6g%24BHA.198
0%40tkmsftngp04 &rnum=24&pre v=/groups%3Fq%3D.n et%2Bdatetime%2 Btime_t%26hl%3D e
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
19704
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, Claire
1
4998
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
20462
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 = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds)); Regards, John
2
18854
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
2128
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 desktop. Essentially, I'm running an executable that will create a folder, and drop a zipped up copy of my data to that folder. There doesn't seem to be a way to get 7zip to automatically create a file where the filename.zip would be a...
6
6609
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 DateTime object in Native C, but I would like to know if there is a work-around to parse out the day/month/year from this DateTime object. Any ideas?
45
9338
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, ((time_t)-1) is returned, and errno is set
2
2642
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 parse a dateTime data from the xml file into a time_t object. Could that be easily done in MSXML6? Seems like an essentially simple task that MS should have included but i just cant find documentation on this thing! The dateTime data in my xml...
4
5864
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) time_t value to System::DateTime ???
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.