471,337 Members | 907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Setting UTC time to 1970 (Unix Epoch)

Hi,

Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
Doing the following

DateTime dt1 = new DateTime(1970, 1,1);
Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());

Doing the following returns 1/1/1970 5:00:00 AM

However, I want to set the DateTime so that it would return 1/1/1970
12:00:00 AM instead.

Basically force DateTime(1970,1,1) to be UTC.

Thanks
Maz.
Dec 2 '05 #1
4 6088
Maziar,

Common mistake is that programmers assume DataTime objects has some relation
to some time zone. There is no such thing. DateTime wraps one double number
and that is what it knows all about. Thre are couple of methods and
properties that take into account the time zone, but information inside has
nothig to do with any time zones.

So the answer of your question is pretty simple:
DateTime dt1 = new DateTime(1970, 1,1);
Debug.WriteLine("dt1(utc):"+ dt1.ToString());

Jan 1st, 1970 00:00 is the same in any time zone.

If you want to het the current time in unix like format as milliseconds
since Jan 1st, 1970 you can do:

(DateTime.UtcNow - new DateTime(1970, 1,1)).TotalMilliseconds.ToString();

UtcNow property is one of few that take into accound the current time zone.
--
HTH
Stoitcho Goutsev (100) [C# MVP]

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:s5********************@rogers.com...
Hi,

Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
Doing the following

DateTime dt1 = new DateTime(1970, 1,1);
Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());

Doing the following returns 1/1/1970 5:00:00 AM

However, I want to set the DateTime so that it would return 1/1/1970
12:00:00 AM instead.

Basically force DateTime(1970,1,1) to be UTC.

Thanks
Maz.

Dec 2 '05 #2
Stoitcho & Maziar,
| There is no such thing. DateTime wraps one double number
| and that is what it knows all about.
Actually DateTime wraps one Int64 number.
http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx

I suspect you are thinking of COM DateTime values.

| but information inside has
| nothig to do with any time zones.
Yes & No, a DateTime can represent local time, UTC time or unspecified,
Starting with .NET 2.0 there the DateTime.Kind property tells you which the
DateTime represents. Although .NET 1.0 & 1.1 could be either, it was not
obvious which a specific DateTime was (effectively
DateTimeKind.Unspecified).

http://msdn2.microsoft.com/e3t6hfy5(en-US,VS.80).aspx

With .NET 2.0 you can indicate that you want a UTC DateTime (or Local
DateTime, or Unspecified), something like:

DateTime dt1 = new DateTime(1970, 1,1, 0, 0, 0,
DateTimeKind.Utc);
Debug.WriteLine(dt1.ToUniversalTime(), "dt1(utc)");
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:O2**************@TK2MSFTNGP11.phx.gbl...
| Maziar,
|
| Common mistake is that programmers assume DataTime objects has some
relation
| to some time zone. There is no such thing. DateTime wraps one double
number
| and that is what it knows all about. Thre are couple of methods and
| properties that take into account the time zone, but information inside
has
| nothig to do with any time zones.
|
| So the answer of your question is pretty simple:
| DateTime dt1 = new DateTime(1970, 1,1);
| Debug.WriteLine("dt1(utc):"+ dt1.ToString());
|
| Jan 1st, 1970 00:00 is the same in any time zone.
|
| If you want to het the current time in unix like format as milliseconds
| since Jan 1st, 1970 you can do:
|
| (DateTime.UtcNow - new DateTime(1970, 1,1)).TotalMilliseconds.ToString();
|
| UtcNow property is one of few that take into accound the current time
zone.
|
|
| --
| HTH
| Stoitcho Goutsev (100) [C# MVP]
|
| "Maziar Aflatoun" <ma***@rogers.com> wrote in message
| news:s5********************@rogers.com...
| > Hi,
| >
| > Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
| > Doing the following
| >
| > DateTime dt1 = new DateTime(1970, 1,1);
| > Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());
| >
| > Doing the following returns 1/1/1970 5:00:00 AM
| >
| > However, I want to set the DateTime so that it would return 1/1/1970
| > 12:00:00 AM instead.
| >
| > Basically force DateTime(1970,1,1) to be UTC.
| >
| > Thanks
| > Maz.
| >
|
|
Dec 2 '05 #3
Jay,

Thanks for the correction.
Actually DateTime wraps one Int64 number.
http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx

I suspect you are thinking of COM DateTime values.
I should've check first. I was talking .NET 1.x. I don't know where I got
that probably some memories from some other frameworks I've used. It could
be COM or VCL that I used many years ago.

It looks you have .NET 2.0 in mind. If this is the case I'd correct you in
my turn. It looks like one of the changes that Microsoft's guys did, in .NET
2.0, is to change the value that DateTime wraps to UInt64 :).

Any ways whether is double, Int64 or UInt64 my point was that prior .NET 2.0
DateTime didn't actually care for the time zone.
| but information inside has
| nothig to do with any time zones.
Yes & No, a DateTime can represent local time, UTC time or unspecified,
Starting with .NET 2.0 there the DateTime.Kind property tells you which
the
DateTime represents. Although .NET 1.0 & 1.1 could be either, it was not
obvious which a specific DateTime was (effectively
DateTimeKind.Unspecified).
Yes, I wasn't aware of these new additions of the DateTime probably because
I've never have any problems with it, but giving the fact how many
probgrammers got confused of all this time-zone stuff I believe it is a good
thing to do.

So thanks again for the correction.
--

Stoitcho Goutsev (100) [C# MVP]


http://msdn2.microsoft.com/e3t6hfy5(en-US,VS.80).aspx

With .NET 2.0 you can indicate that you want a UTC DateTime (or Local
DateTime, or Unspecified), something like:

DateTime dt1 = new DateTime(1970, 1,1, 0, 0, 0,
DateTimeKind.Utc);
Debug.WriteLine(dt1.ToUniversalTime(), "dt1(utc)");
--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:O2**************@TK2MSFTNGP11.phx.gbl...
| Maziar,
|
| Common mistake is that programmers assume DataTime objects has some
relation
| to some time zone. There is no such thing. DateTime wraps one double
number
| and that is what it knows all about. Thre are couple of methods and
| properties that take into account the time zone, but information inside
has
| nothig to do with any time zones.
|
| So the answer of your question is pretty simple:
| DateTime dt1 = new DateTime(1970, 1,1);
| Debug.WriteLine("dt1(utc):"+ dt1.ToString());
|
| Jan 1st, 1970 00:00 is the same in any time zone.
|
| If you want to het the current time in unix like format as milliseconds
| since Jan 1st, 1970 you can do:
|
| (DateTime.UtcNow - new DateTime(1970,
1,1)).TotalMilliseconds.ToString();
|
| UtcNow property is one of few that take into accound the current time
zone.
|
|
| --
| HTH
| Stoitcho Goutsev (100) [C# MVP]
|
| "Maziar Aflatoun" <ma***@rogers.com> wrote in message
| news:s5********************@rogers.com...
| > Hi,
| >
| > Can someone please tell me how I can set DateTime to 01/01/1970 (UTC).
| > Doing the following
| >
| > DateTime dt1 = new DateTime(1970, 1,1);
| > Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());
| >
| > Doing the following returns 1/1/1970 5:00:00 AM
| >
| > However, I want to set the DateTime so that it would return 1/1/1970
| > 12:00:00 AM instead.
| >
| > Basically force DateTime(1970,1,1) to be UTC.
| >
| > Thanks
| > Maz.
| >
|
|

Dec 5 '05 #4
Stoitcho,
| It looks you have .NET 2.0 in mind. If this is the case I'd correct you in
| my turn.
..NET 2.0 *is* the current version. ;-)
| It looks like one of the changes that Microsoft's guys did, in .NET
| 2.0, is to change the value that DateTime wraps to UInt64 :).
Interesting using ILDASM.EXE I see internally the DateTime now wraps an
UInt64 instead of an Int64, which if you think about it actually makes
sense.

However! That is an implementation detail. I consider both UInt64 & Int64 to
be 64-bit "integers", as oppose to a 64-bit floating point.

My initial statement was based on the Remarks - Version Considerations under
the link I gave initially, which tells you how the value is stored under 1.0
& 1.1 verses 2.0. Which is also why I gave the link, it tells you how its
stored in both versions.

http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:uN*****************@TK2MSFTNGP11.phx.gbl...
| Jay,
|
| Thanks for the correction.
|
| > Actually DateTime wraps one Int64 number.
| > http://msdn2.microsoft.com/03ybds8y(en-US,VS.80).aspx
| >
| > I suspect you are thinking of COM DateTime values.
|
| I should've check first. I was talking .NET 1.x. I don't know where I got
| that probably some memories from some other frameworks I've used. It could
| be COM or VCL that I used many years ago.
|
| It looks you have .NET 2.0 in mind. If this is the case I'd correct you in
| my turn. It looks like one of the changes that Microsoft's guys did, in
..NET
| 2.0, is to change the value that DateTime wraps to UInt64 :).
|
| Any ways whether is double, Int64 or UInt64 my point was that prior .NET
2.0
| DateTime didn't actually care for the time zone.
| >
| > | but information inside has
| > | nothig to do with any time zones.
| > Yes & No, a DateTime can represent local time, UTC time or unspecified,
| > Starting with .NET 2.0 there the DateTime.Kind property tells you which
| > the
| > DateTime represents. Although .NET 1.0 & 1.1 could be either, it was not
| > obvious which a specific DateTime was (effectively
| > DateTimeKind.Unspecified).
|
| Yes, I wasn't aware of these new additions of the DateTime probably
because
| I've never have any problems with it, but giving the fact how many
| probgrammers got confused of all this time-zone stuff I believe it is a
good
| thing to do.
|
| So thanks again for the correction.
|
|
| --
|
| Stoitcho Goutsev (100) [C# MVP]
|
|
| >
| > http://msdn2.microsoft.com/e3t6hfy5(en-US,VS.80).aspx
| >
| > With .NET 2.0 you can indicate that you want a UTC DateTime (or Local
| > DateTime, or Unspecified), something like:
| >
| > DateTime dt1 = new DateTime(1970, 1,1, 0, 0, 0,
| > DateTimeKind.Utc);
| > Debug.WriteLine(dt1.ToUniversalTime(), "dt1(utc)");
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
| > news:O2**************@TK2MSFTNGP11.phx.gbl...
| > | Maziar,
| > |
| > | Common mistake is that programmers assume DataTime objects has some
| > relation
| > | to some time zone. There is no such thing. DateTime wraps one double
| > number
| > | and that is what it knows all about. Thre are couple of methods and
| > | properties that take into account the time zone, but information
inside
| > has
| > | nothig to do with any time zones.
| > |
| > | So the answer of your question is pretty simple:
| > | DateTime dt1 = new DateTime(1970, 1,1);
| > | Debug.WriteLine("dt1(utc):"+ dt1.ToString());
| > |
| > | Jan 1st, 1970 00:00 is the same in any time zone.
| > |
| > | If you want to het the current time in unix like format as
milliseconds
| > | since Jan 1st, 1970 you can do:
| > |
| > | (DateTime.UtcNow - new DateTime(1970,
| > 1,1)).TotalMilliseconds.ToString();
| > |
| > | UtcNow property is one of few that take into accound the current time
| > zone.
| > |
| > |
| > | --
| > | HTH
| > | Stoitcho Goutsev (100) [C# MVP]
| > |
| > | "Maziar Aflatoun" <ma***@rogers.com> wrote in message
| > | news:s5********************@rogers.com...
| > | > Hi,
| > | >
| > | > Can someone please tell me how I can set DateTime to 01/01/1970
(UTC).
| > | > Doing the following
| > | >
| > | > DateTime dt1 = new DateTime(1970, 1,1);
| > | > Debug.WriteLine("dt1(utc):"+ dt1.ToUniversalTime().ToString());
| > | >
| > | > Doing the following returns 1/1/1970 5:00:00 AM
| > | >
| > | > However, I want to set the DateTime so that it would return 1/1/1970
| > | > 12:00:00 AM instead.
| > | >
| > | > Basically force DateTime(1970,1,1) to be UTC.
| > | >
| > | > Thanks
| > | > Maz.
| > | >
| > |
| > |
| >
| >
|
|
Dec 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sorisio, Chris | last post: by
6 posts views Thread by David Graham | last post: by
3 posts views Thread by Tomas | last post: by
4 posts views Thread by Seth7TS | last post: by
5 posts views Thread by Grey Alien | last post: by
11 posts views Thread by usenet | last post: by
17 posts views Thread by Andrew Chalk | last post: by
6 posts views Thread by Jeremy Sanders | last post: by

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.