Changing the Date a Week at a Time


#1

I have a form in Groov which I need to be able to move back and forth a week at a time:

I know there are a bunch of date and time commands in PAC Control but I don’t see where I can do this easily.

Has anyone done this before and can share how they did it? I’d be much obliged.

Thanks


#2

One way: Use the NTP conversion commands, convert the Opto date/time table to a NTP timestamp (64 bit) add/subtract 7 days (7 * 24 * 60 * 60 * 0x100000000i64) to the NTP timestamp and then convert back.


#3

To get a certain date for a particular day (Monday in this case) of the week using today’s date:

GetDateTime(ntDateTime);
//Get Diff to Monday
nDayDiff = 1 - ntDateTime[3]; //1 is monday
DateTimeToNtpTimestamp(ntDateTime, n64NTP);
n64NTP = n64NTP + (nDayDiff * 24 * 60 * 60 * 0x100000000i64);
NtpTimestampToDateTime(ntDateTimeMonday, n64NTP);

This will get you Mondays date for the week spanning from Sunday to Saturday.


#4

Thanks.

Here’s a problem that maybe warrants a seperate post:

Why do I get his error when I try to delete the variable n64NTP:
image

When there are no references:
image

I saved and recompiled the strategy but it makes no difference.

Thanks


#5

Tools | Regenerate Reference Counts or CTRL+R


#6

Thanks for the code Philip. Just to complicate things, If I wanted to move forward and back a week at a time, always starting on a Monday, what would I need to to do? Is it as simple as adding or subtracting 7 somewhere?

Thanks,

image


#7

Yes, that is pretty much what you need to do.

Something like this:

if(bGroovPressThisWeek or bInitGroovDateTime) then
  GetDateTime(ntDateTime);
  //Get Diff to Monday
  nDayDiff = 1 - ntDateTime[3]; //1 is monday
  DateTimeToNtpTimestamp(ntDateTime, n64NTP);
  n64NTP = n64NTP + (nDayDiff * 24 * 60 * 60 * 0x100000000i64);
  NtpTimestampToDateTime(ntGroovDateTime, n64NTP);
  bGroovPressThisWeek=0;
  bInitGroovDateTime=0;
  nDayDiff=0;
endif
if(bGroovPressNextWeek) then
  nDayDiff = 7;
  bGroovPressNextWeek=0;
endif
if(bGroovPressPreviousWeek) then
  nDayDiff = -7;
  bGroovPressPreviousWeek=0;
endif

if(nDayDiff <> 0) then
  DateTimeToNtpTimestamp(ntGroovDateTime, n64NTP);
  n64NTP = n64NTP + (nDayDiff * 24 * 60 * 60 * 0x100000000i64);
  NtpTimestampToDateTime(ntGroovDateTime, n64NTP);
  nDayDiff=0;
endif

#8

Thanks Philip. Does this incorporate the previous code for keeping things on Mondays? It looks like it does but I want to make sure before Incorporate into my strategy.


#9

Sure does. If you set bInitGroovDateTime to initialize to 1 on strategy run, then ntGroovDateTime will be set to this weeks Monday. Modify as you see fit.


#10

Like everything else you do, this worked like a champ!

Thanks for all your help.