473,395 Members | 1,677 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,395 software developers and data experts.

DateTime fundamentally broken

Does anyone know how to make the following method work across
timezones?

[WebMethod]
public DateTime GetTime ()
{
return DateTime.UtcNow;
}

Nov 23 '05 #1
9 2889
Is it not returning the UTC time at the client?

--
William Stacey, MVP
http://mvp.support.microsoft.com

<pi****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Does anyone know how to make the following method work across
timezones?

[WebMethod]
public DateTime GetTime ()
{
return DateTime.UtcNow;
}


Nov 23 '05 #2
not if the client & server are in different time zones.

client code:
Console.WriteLine (service.GetTime ().ToString ("s"));

Nov 23 '05 #3
not if the client & server are in different time zones.

client code:
Console.WriteLine (service.GetTime ().ToString ("s"));

Nov 23 '05 #4
No, it is returning the current date/time OF the server, converted to UTC,
TO the client.

What the server is saying is that at the time that I executed the
DateTime.UtcNow method, the date/time was <whatever> in terms of UTC.

The whole crux of your original questions depends entirely upon what your
definition of 'work across timezones' is.
"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:O3**************@TK2MSFTNGP10.phx.gbl...
Is it not returning the UTC time at the client?

--
William Stacey, MVP
http://mvp.support.microsoft.com

<pi****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Does anyone know how to make the following method work across
timezones?

[WebMethod]
public DateTime GetTime ()
{
return DateTime.UtcNow;
}

Nov 23 '05 #5
Does the following give you the correct time?

Console.WriteLine(service.GetTime().ToLocalTime(). ToString("s"));

--
William Stacey, MVP
http://mvp.support.microsoft.com

<pi****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
not if the client & server are in different time zones.

client code:
Console.WriteLine (service.GetTime ().ToString ("s"));


Nov 23 '05 #6
The other thing is the way the serializer handles DateTimes. Even if the
date is UTC, it will put the TZ on the wire like:
<dateTime>2005-04-06T05:12:10.5468750-04:00</dateTime> (note Eastern TZ).

This is not correct as it should be Zulu time with no timezone. Not sure
how that will be handled in all cases at different timezones. Therefore,
when I pass DateTime, I always work in Zulu time and pass the date myself as
a string. That way I always know it is a true UTC and I can convert to
local time if needed at the client side. Use these two methods for the
conversions. ToUTCDateTimeString at the server and FromUTCDateTimeString at
the client.

/// <summary>
/// Returns string formatted in Zulu (UTC) time. value must be UTC
DateTime.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToUTCDateTimeString(DateTime value)
{
// Date expected to be UTC date already.
// e.g 2003-10-26T14:33:41.1234567Z
return value.ToString("yyyy-MM-ddTHH:mm:ssZ",
CultureInfo.InvariantCulture);
}

/// <summary>
/// Parses the UTC formatted string, returning DateTime in UTC.
/// Use ToLocalTime() on the result if you want to convert to local time.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static DateTime FromUTCDateTimeString(string value)
{
return DateTime.Parse(value, CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

<pi****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
not if the client & server are in different time zones.

client code:
Console.WriteLine (service.GetTime ().ToString ("s"));


Nov 23 '05 #7
> Therefore, when I pass DateTime, I always work in Zulu time and pass
the date myself as a string.

This is exactly what I'm doing now (except I'm using date.ToString
("s"); )

This implies that the current Microsoft Serializer behaviour is either:

1) completely useless
2) a bug
3) all of the above

I'd love to hear from an MSFT guy what the rational behind this was.

Nov 23 '05 #8
> The whole crux of your original questions depends entirely upon what
your definition of 'work across timezones' is.

I want the client to receive a DateTime that holds the UTC time on the
server.

However, since Microsoft chose to transfer the dates using a
time-zone-sensitive format (the dates are treated as relative to the
local timezone on each machine) this only works if both the client &
the server are in the same timezone. Otherwise, the DateTime receives
is shifted by the time difference between the two timezones.

Nov 23 '05 #9
You need to do the zulu "Z" thing as I shown to get the transfer right.
Once you have the DateTime after using FromUTC method (and using ToLocal if
you wish), then you can format as needed with "s" or what ever you want for
output. I would keep all dates in UTC as long as possible and only go to
local time when you want to display. All date calcs should be in UTC.
1) completely useless
2) a bug
3) all of the above

I'd love to hear from an MSFT guy what the rational behind this was.


Some poor early design decisions. Brad Abrams from MS talks frankly about
most of the issues here:
http://blogs.msdn.com/brada/search.aspx?q=datetime&p=1

--
William Stacey, MVP
http://mvp.support.microsoft.com
Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
10
by: Philip Wagenaar | last post by:
I have some code to convert a date to the unixdatetime representation (number of seconds since 1970). Only problem is that it only counts from the whole day, it ignores the times part. So convert...
6
by: Bryce K. Nielsen | last post by:
I have a Form that has a method that sets a label's text to DateTime.Now.ToString(). Strange thing is, when I call the form twice from the same app, the label shows the old DateTime value. It's...
4
by: sumip | last post by:
I just made a pretty strange observation with .NET 2.0 - DateTime.AddMilliseconds(double) and AddSeconds(double) seem to be broken: DateTime now = new DateTime(); DateTime t2 =...
6
by: Brandon | last post by:
I'm using PHP with MySQL 4.x and was having trouble converting a datetime from MySQL into a formatted string until I ran across this solution that converts a YYYY-MM-DD HH:MM:SS string into a...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.