How to best compare datetime?

  • Thread starter Thread starter Saul
  • Start date Start date
S

Saul

Hi all!

Well, I admit it, I don't really know the best way to solve this problem, so
I thought I'd ask the braintrust here.

I have a pocketPC device which is syncing to a SQL server. I send back the
servers datetime as part of the sync. The problem is that I want to make
sure that the handhelds date and time is within a few hours of the server,
if not, set it to the same times as the server. As this app will be deployed
all over Australia, local time might be several hours different to the
server (in Sydney). I am ok with getting it "close" if it's way off, even if
it's not to the hour accurate. What I really want to stop is people who have
not set the date correctly and are in 2000 for example. The data I am
collecting is timestamped with the local (handheld) time, so it's kinda
important.

I have discovered in the FAQ how to set the PocketPC time, which is great.
This relies apon a systemtime structure as follows..

Public Structure SYSTEMTIME
' See
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#6.16
' used for changing datetime on unit
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Ok, so armed with the server date/time as a string (eg "10/2/2005 1:15:25
PM") how do I best compare this to now() to see if it's wrong by more than 4
hours, and then put what it should be (from the server string again) into
the structure above to update the time/date?

The things that are freaking me out are - if it's done close to midnight (so
the days will be different), or midnight end of month (month different) or
midnight new years eve (unlikely, everyone will be too drunk for that, but
you know). I am also really not confident either in converting the string
into a comparable date. For example, the day and month don't have leading
zeros so how best to pull them out? As usual, I'm probably missing a whole
bunch of really obvious things.

Any and all help greatly appreciated. Thanks for reading this and post here
if you want me to clarify anything!

Regards,
Saul
 
Saul,

If your mobile devices have any sort of wireless connectivity to the
Internet,
my first choice would be to use a network time service to retrieve the
actual
time and then set the system local time from that. If you do not have
Internet
access and your mobile devices are synchronizing with SQL Server in their
cradles or an intranet, you can set the device's time with code like this:

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

}

[DllImport("coredll.dll")]

public extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]

public extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

private void _updateLocalDateTime()

{

SYSTEMTIME st = new SYSTEMTIME();

GetLocalTime(ref st);

st.wMonth = (ushort)this.nudMonth.Value;

st.wDay = (ushort)this.nudDay.Value;

st.wYear = (ushort)this.nudYear.Value;

st.wMinute = (ushort)this.nudMinute.Value;

st.wHour = (ushort)this.dudHour.SelectedIndex;

SetLocalTime(ref st);


}

Note that you can use DateTime.Parse() to convert the timestamp you
are getting from your server to a .Net DateTime.

There is also a short but noticeable delay between updating the local time
on device using the code above and the time it shows up in the menu bar.

Darren Shaffer
Principal Architect
Connected Innovation
 
Darren,

Thanks very much for your reply. The parse method really helps with one of
the steps (convertying the string into a system.datetime). The code you gave
was good too, thanks, although I think in VB!

I am still wondering about the bit where I have to compare a system.datetime
agains a SYSTEMTIME. These seem two different ways of storing a datetime.
What I want to do is find out if they are more than, say 240 minutes apart.
In pseudocode...

dim st as new SYSTEMTIME
dim servertime as datetime
' Assume they have values assigned
' Check two datetimes are within 240 minutes
if (st>servertime+120mins) or (st<servertime+120mins) then
'reset handheld time to server time
end if

It's the conditional statement I'm struggling with!

Thanks a lot
- Saul

Darren Shaffer said:
Saul,

If your mobile devices have any sort of wireless connectivity to the
Internet,
my first choice would be to use a network time service to retrieve the
actual
time and then set the system local time from that. If you do not have
Internet
access and your mobile devices are synchronizing with SQL Server in their
cradles or an intranet, you can set the device's time with code like this:

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

}

[DllImport("coredll.dll")]

public extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]

public extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

private void _updateLocalDateTime()

{

SYSTEMTIME st = new SYSTEMTIME();

GetLocalTime(ref st);

st.wMonth = (ushort)this.nudMonth.Value;

st.wDay = (ushort)this.nudDay.Value;

st.wYear = (ushort)this.nudYear.Value;

st.wMinute = (ushort)this.nudMinute.Value;

st.wHour = (ushort)this.dudHour.SelectedIndex;

SetLocalTime(ref st);


}

Note that you can use DateTime.Parse() to convert the timestamp you
are getting from your server to a .Net DateTime.

There is also a short but noticeable delay between updating the local time
on device using the code above and the time it shows up in the menu bar.

Darren Shaffer
Principal Architect
Connected Innovation

Saul said:
Hi all!

Well, I admit it, I don't really know the best way to solve this problem,
so I thought I'd ask the braintrust here.

I have a pocketPC device which is syncing to a SQL server. I send back
the servers datetime as part of the sync. The problem is that I want to
make sure that the handhelds date and time is within a few hours of the
server, if not, set it to the same times as the server. As this app will
be deployed all over Australia, local time might be several hours
different to the server (in Sydney). I am ok with getting it "close" if
it's way off, even if it's not to the hour accurate. What I really want
to stop is people who have not set the date correctly and are in 2000 for
example. The data I am collecting is timestamped with the local
(handheld) time, so it's kinda important.

I have discovered in the FAQ how to set the PocketPC time, which is
great. This relies apon a systemtime structure as follows..

Public Structure SYSTEMTIME
' See
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#6.16
' used for changing datetime on unit
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Ok, so armed with the server date/time as a string (eg "10/2/2005 1:15:25
PM") how do I best compare this to now() to see if it's wrong by more
than 4 hours, and then put what it should be (from the server string
again) into the structure above to update the time/date?

The things that are freaking me out are - if it's done close to midnight
(so the days will be different), or midnight end of month (month
different) or midnight new years eve (unlikely, everyone will be too
drunk for that, but you know). I am also really not confident either in
converting the string into a comparable date. For example, the day and
month don't have leading zeros so how best to pull them out? As usual,
I'm probably missing a whole bunch of really obvious things.

Any and all help greatly appreciated. Thanks for reading this and post
here if you want me to clarify anything!

Regards,
Saul
 
Darren (and whoever is interested),

Two things.

1. The parse method is not in the Compact Framework I don't think. Could be
wrong..?

2. My conditional is wrong below, should have been (of course) --

if (st>servertime+120mins) or (st<servertime-120mins) then

Regards,
Saul

Saul said:
Darren,

Thanks very much for your reply. The parse method really helps with one of
the steps (convertying the string into a system.datetime). The code you
gave was good too, thanks, although I think in VB!

I am still wondering about the bit where I have to compare a
system.datetime agains a SYSTEMTIME. These seem two different ways of
storing a datetime. What I want to do is find out if they are more than,
say 240 minutes apart. In pseudocode...

dim st as new SYSTEMTIME
dim servertime as datetime
' Assume they have values assigned
' Check two datetimes are within 240 minutes
if (st>servertime+120mins) or (st<servertime+120mins) then
'reset handheld time to server time
end if

It's the conditional statement I'm struggling with!

Thanks a lot
- Saul

Darren Shaffer said:
Saul,

If your mobile devices have any sort of wireless connectivity to the
Internet,
my first choice would be to use a network time service to retrieve the
actual
time and then set the system local time from that. If you do not have
Internet
access and your mobile devices are synchronizing with SQL Server in their
cradles or an intranet, you can set the device's time with code like
this:

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

}

[DllImport("coredll.dll")]

public extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]

public extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

private void _updateLocalDateTime()

{

SYSTEMTIME st = new SYSTEMTIME();

GetLocalTime(ref st);

st.wMonth = (ushort)this.nudMonth.Value;

st.wDay = (ushort)this.nudDay.Value;

st.wYear = (ushort)this.nudYear.Value;

st.wMinute = (ushort)this.nudMinute.Value;

st.wHour = (ushort)this.dudHour.SelectedIndex;

SetLocalTime(ref st);


}

Note that you can use DateTime.Parse() to convert the timestamp you
are getting from your server to a .Net DateTime.

There is also a short but noticeable delay between updating the local
time
on device using the code above and the time it shows up in the menu bar.

Darren Shaffer
Principal Architect
Connected Innovation

Saul said:
Hi all!

Well, I admit it, I don't really know the best way to solve this
problem, so I thought I'd ask the braintrust here.

I have a pocketPC device which is syncing to a SQL server. I send back
the servers datetime as part of the sync. The problem is that I want to
make sure that the handhelds date and time is within a few hours of the
server, if not, set it to the same times as the server. As this app will
be deployed all over Australia, local time might be several hours
different to the server (in Sydney). I am ok with getting it "close" if
it's way off, even if it's not to the hour accurate. What I really want
to stop is people who have not set the date correctly and are in 2000
for example. The data I am collecting is timestamped with the local
(handheld) time, so it's kinda important.

I have discovered in the FAQ how to set the PocketPC time, which is
great. This relies apon a systemtime structure as follows..

Public Structure SYSTEMTIME
' See
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#6.16
' used for changing datetime on unit
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Ok, so armed with the server date/time as a string (eg "10/2/2005
1:15:25 PM") how do I best compare this to now() to see if it's wrong by
more than 4 hours, and then put what it should be (from the server
string again) into the structure above to update the time/date?

The things that are freaking me out are - if it's done close to midnight
(so the days will be different), or midnight end of month (month
different) or midnight new years eve (unlikely, everyone will be too
drunk for that, but you know). I am also really not confident either in
converting the string into a comparable date. For example, the day and
month don't have leading zeros so how best to pull them out? As usual,
I'm probably missing a whole bunch of really obvious things.

Any and all help greatly appreciated. Thanks for reading this and post
here if you want me to clarify anything!

Regards,
Saul
 
Saul,

System.DateTime.Parse() is in the Compact Framework. It allows you to take
a variety of
string representations of dates and/or times and convert them to a DateTime
type using appropriate
formatters.

When you get the SystemTime back from your native code call, the various
components of the
time will be in separate parameters. In order to figure out how "far off"
your time is from some
other time, I would use DateTime.Parse() on a string you put together by
concatenating the
various members of the SystemTime struct and then simply compare the two
datetimes to
determine their relative chronology or to calculate the timespan between
them. The key is
that you get very familiar with the capabilities of a DateTime in .Net.
http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdatetimeclasstopic.asp

Darren Shaffer
Principal Architect
Connected Innovation

Saul said:
Darren (and whoever is interested),

Two things.

1. The parse method is not in the Compact Framework I don't think. Could
be wrong..?

2. My conditional is wrong below, should have been (of course) --

if (st>servertime+120mins) or (st<servertime-120mins) then

Regards,
Saul

Saul said:
Darren,

Thanks very much for your reply. The parse method really helps with one
of the steps (convertying the string into a system.datetime). The code
you gave was good too, thanks, although I think in VB!

I am still wondering about the bit where I have to compare a
system.datetime agains a SYSTEMTIME. These seem two different ways of
storing a datetime. What I want to do is find out if they are more than,
say 240 minutes apart. In pseudocode...

dim st as new SYSTEMTIME
dim servertime as datetime
' Assume they have values assigned
' Check two datetimes are within 240 minutes
if (st>servertime+120mins) or (st<servertime+120mins) then
'reset handheld time to server time
end if

It's the conditional statement I'm struggling with!

Thanks a lot
- Saul

Darren Shaffer said:
Saul,

If your mobile devices have any sort of wireless connectivity to the
Internet,
my first choice would be to use a network time service to retrieve the
actual
time and then set the system local time from that. If you do not have
Internet
access and your mobile devices are synchronizing with SQL Server in
their
cradles or an intranet, you can set the device's time with code like
this:

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

}

[DllImport("coredll.dll")]

public extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]

public extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

private void _updateLocalDateTime()

{

SYSTEMTIME st = new SYSTEMTIME();

GetLocalTime(ref st);

st.wMonth = (ushort)this.nudMonth.Value;

st.wDay = (ushort)this.nudDay.Value;

st.wYear = (ushort)this.nudYear.Value;

st.wMinute = (ushort)this.nudMinute.Value;

st.wHour = (ushort)this.dudHour.SelectedIndex;

SetLocalTime(ref st);


}

Note that you can use DateTime.Parse() to convert the timestamp you
are getting from your server to a .Net DateTime.

There is also a short but noticeable delay between updating the local
time
on device using the code above and the time it shows up in the menu bar.

Darren Shaffer
Principal Architect
Connected Innovation

Hi all!

Well, I admit it, I don't really know the best way to solve this
problem, so I thought I'd ask the braintrust here.

I have a pocketPC device which is syncing to a SQL server. I send back
the servers datetime as part of the sync. The problem is that I want to
make sure that the handhelds date and time is within a few hours of the
server, if not, set it to the same times as the server. As this app
will be deployed all over Australia, local time might be several hours
different to the server (in Sydney). I am ok with getting it "close" if
it's way off, even if it's not to the hour accurate. What I really want
to stop is people who have not set the date correctly and are in 2000
for example. The data I am collecting is timestamped with the local
(handheld) time, so it's kinda important.

I have discovered in the FAQ how to set the PocketPC time, which is
great. This relies apon a systemtime structure as follows..

Public Structure SYSTEMTIME
' See
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#6.16
' used for changing datetime on unit
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Ok, so armed with the server date/time as a string (eg "10/2/2005
1:15:25 PM") how do I best compare this to now() to see if it's wrong
by more than 4 hours, and then put what it should be (from the server
string again) into the structure above to update the time/date?

The things that are freaking me out are - if it's done close to
midnight (so the days will be different), or midnight end of month
(month different) or midnight new years eve (unlikely, everyone will be
too drunk for that, but you know). I am also really not confident
either in converting the string into a comparable date. For example,
the day and month don't have leading zeros so how best to pull them
out? As usual, I'm probably missing a whole bunch of really obvious
things.

Any and all help greatly appreciated. Thanks for reading this and post
here if you want me to clarify anything!

Regards,
Saul
 
Darren, Thanks for all your help!! I've got it (almost) working.

Darren Shaffer said:
Saul,

System.DateTime.Parse() is in the Compact Framework. It allows you to
take a variety of
string representations of dates and/or times and convert them to a
DateTime type using appropriate
formatters.

When you get the SystemTime back from your native code call, the various
components of the
time will be in separate parameters. In order to figure out how "far off"
your time is from some
other time, I would use DateTime.Parse() on a string you put together by
concatenating the
various members of the SystemTime struct and then simply compare the two
datetimes to
determine their relative chronology or to calculate the timespan between
them. The key is
that you get very familiar with the capabilities of a DateTime in .Net.
http://msdn.microsoft.com/library/d.../cpref/html/frlrfsystemdatetimeclasstopic.asp

Darren Shaffer
Principal Architect
Connected Innovation

Saul said:
Darren (and whoever is interested),

Two things.

1. The parse method is not in the Compact Framework I don't think. Could
be wrong..?

2. My conditional is wrong below, should have been (of course) --

if (st>servertime+120mins) or (st<servertime-120mins) then

Regards,
Saul

Saul said:
Darren,

Thanks very much for your reply. The parse method really helps with one
of the steps (convertying the string into a system.datetime). The code
you gave was good too, thanks, although I think in VB!

I am still wondering about the bit where I have to compare a
system.datetime agains a SYSTEMTIME. These seem two different ways of
storing a datetime. What I want to do is find out if they are more than,
say 240 minutes apart. In pseudocode...

dim st as new SYSTEMTIME
dim servertime as datetime
' Assume they have values assigned
' Check two datetimes are within 240 minutes
if (st>servertime+120mins) or (st<servertime+120mins) then
'reset handheld time to server time
end if

It's the conditional statement I'm struggling with!

Thanks a lot
- Saul

message Saul,

If your mobile devices have any sort of wireless connectivity to the
Internet,
my first choice would be to use a network time service to retrieve the
actual
time and then set the system local time from that. If you do not have
Internet
access and your mobile devices are synchronizing with SQL Server in
their
cradles or an intranet, you can set the device's time with code like
this:

public struct SYSTEMTIME

{

public ushort wYear;

public ushort wMonth;

public ushort wDayOfWeek;

public ushort wDay;

public ushort wHour;

public ushort wMinute;

public ushort wSecond;

public ushort wMilliseconds;

}

[DllImport("coredll.dll")]

public extern static void GetLocalTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]

public extern static uint SetLocalTime(ref SYSTEMTIME lpSystemTime);

private void _updateLocalDateTime()

{

SYSTEMTIME st = new SYSTEMTIME();

GetLocalTime(ref st);

st.wMonth = (ushort)this.nudMonth.Value;

st.wDay = (ushort)this.nudDay.Value;

st.wYear = (ushort)this.nudYear.Value;

st.wMinute = (ushort)this.nudMinute.Value;

st.wHour = (ushort)this.dudHour.SelectedIndex;

SetLocalTime(ref st);


}

Note that you can use DateTime.Parse() to convert the timestamp you
are getting from your server to a .Net DateTime.

There is also a short but noticeable delay between updating the local
time
on device using the code above and the time it shows up in the menu
bar.

Darren Shaffer
Principal Architect
Connected Innovation

Hi all!

Well, I admit it, I don't really know the best way to solve this
problem, so I thought I'd ask the braintrust here.

I have a pocketPC device which is syncing to a SQL server. I send back
the servers datetime as part of the sync. The problem is that I want
to make sure that the handhelds date and time is within a few hours of
the server, if not, set it to the same times as the server. As this
app will be deployed all over Australia, local time might be several
hours different to the server (in Sydney). I am ok with getting it
"close" if it's way off, even if it's not to the hour accurate. What I
really want to stop is people who have not set the date correctly and
are in 2000 for example. The data I am collecting is timestamped with
the local (handheld) time, so it's kinda important.

I have discovered in the FAQ how to set the PocketPC time, which is
great. This relies apon a systemtime structure as follows..

Public Structure SYSTEMTIME
' See
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#6.16
' used for changing datetime on unit
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure

Ok, so armed with the server date/time as a string (eg "10/2/2005
1:15:25 PM") how do I best compare this to now() to see if it's wrong
by more than 4 hours, and then put what it should be (from the server
string again) into the structure above to update the time/date?

The things that are freaking me out are - if it's done close to
midnight (so the days will be different), or midnight end of month
(month different) or midnight new years eve (unlikely, everyone will
be too drunk for that, but you know). I am also really not confident
either in converting the string into a comparable date. For example,
the day and month don't have leading zeros so how best to pull them
out? As usual, I'm probably missing a whole bunch of really obvious
things.

Any and all help greatly appreciated. Thanks for reading this and post
here if you want me to clarify anything!

Regards,
Saul
 
Back
Top