Author Topic: How to get a future date  (Read 1759 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
How to get a future date
« on: April 17, 2018, 07:21:52 PM »
Hello

i'm looking for a program that can increment the current date in order
 to get a date of say 30 days in the future.

for instance, today's date is Apr 17 2018  and the 30 days ahead date would be May 30 2018.

the below O2 code only get the current date

Code: [Select]

$ FileName  "DateFormat.exe"
use rtl64
' Program to get today date

extern lib "kernel32.dll"

'http://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

void GetSystemTime(SYSTEMTIME*t);
void GetLocalTime(SYSTEMTIME*t);

end extern

SYSTEMTIME t
'GetSystemTime t 'GMT (Greenwich Mean Time)
GetLocalTime t

String WeekDay[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
String MonthName[12]={"January","February","March","April","May","June",
        "July","August","September","October","November","December"}
String month=str t.wMonth : if len(month)<2 then month="0"+month
String day=str t.wDay

 if len(day)<2 then day="0"+day
'
print  " today date :  " +      "" t.wYear "-" month "-" day
print WeekDay[t.wDayOfWeek+1 and 7 ] " " MonthName[t.wMonth and 31] " " t.wDay " " t.wYear




chrisc

  • Guest
Re: How to get a future date
« Reply #1 on: April 17, 2018, 07:24:02 PM »
In PB we have the following code that can do this future date

Code: [Select]
#COMPILE EXE
#INCLUDE "win32api.inc"


' Increment Time.bas


TYPE ScottsDateTime
    dwLowDateTime  AS DWORD
    dwHighDateTime AS DWORD
END TYPE

UNION QuadFILETIME
    dwDateTime AS ScottsDateTime
    qdTime     AS QUAD
END UNION



 '===========================================
FUNCTION PBMAIN() AS LONG

LOCAL iMinutes AS LONG


   ? " Current Date and Time : " +   GitCurDateTime ,, "Current Date and Time"


' you could also use a negative value here to subtract time from
' the current system date and time.
iMinutes = 720 ' 12 * 60              ' Increment 12 hours
  MSGBOX  SetNextCheckTime(iMinutes) ,, " 12 hours ahead "

   iMinutes =  30 * 24 *60  ' 30 days ahead in minutes
   MSGBOX SetNextCheckTime(iMinutes)  ,, " 30 days ahead "


END FUNCTION




'=================================================
' This function does the same thing as
'  GetPCTimeAndDate except uses the input SYSTEMTIME structure
FUNCTION FormatTime(st AS SYSTEMTIME, TimeFormat AS STRING, DateFormat AS STRING) EXPORT AS STRING

LOCAL tTime AS ASCIIZ * 64
LOCAL tDay  AS ASCIIZ * 64

IF LEN(TimeFormat) = 0 THEN TimeFormat =  "hh':'mm':'ss tt"
IF LEN(DateFormat) = 0 THEN DateFormat = "dddd',' MMMM dd'ø' yyyy"

GetTimeFormat 0,0, st, BYVAL STRPTR(TimeFormat), tTime, 64
GetDateFormat 0,0, st, BYVAL STRPTR(DateFormat), tDay, 64

FUNCTION = tDay + "   " + tTime

END FUNCTION


'=================================================
' This function does the same thing as
'  GetPCTimeAndDate except uses the input SYSTEMTIME structure
FUNCTION SpFormatTime(st AS SYSTEMTIME, TimeFormat AS STRING, DateFormat AS STRING) EXPORT AS STRING

LOCAL tTime AS ASCIIZ * 64
LOCAL tDay  AS ASCIIZ * 64

IF LEN(TimeFormat) = 0 THEN
    TimeFormat =  "hh':'mm':'ss tt"
END IF

IF LEN(DateFormat) = 0 THEN
    DateFormat = "dddd',' MMMM dd'ø' yyyy"
END IF

GetTimeFormat 0,0, st, BYVAL STRPTR(TimeFormat), tTime, 64
GetDateFormat 0,0, st, BYVAL STRPTR(DateFormat), tDay, 64

FUNCTION = tDay + "   " + tTime

END FUNCTION


'=================================================
FUNCTION GitCurDateTime  AS STRING

LOCAL syst AS SYSTEMTIME

' Get the current time
 CALL GetlocalTime(syst)


 ' When you need the date in  MM-dd-yyyy format
 'FUNCTION = spFormatTime(syst, "hh':'mm':'ss tt", "MM-dd-yyyy")

 '  When time is up to secs format
 'FUNCTION = spFormatTime(syst, "hh':'mm':'ss tt", "yyyy-MM-dd")

 ' When time is up to min format
 FUNCTION = spFormatTime(syst, "hh':'mm tt", "yyyy-MM-dd")

END FUNCTION





'==============================================
FUNCTION IncrSystemTime(st AS SYSTEMTIME, BYVAL IncrValue AS LONG) EXPORT AS LONG

DIM QT AS QuadFILETIME

' NOTE IncrValue would always need to be in seconds
'      for the rest of this to work


' Convert to QUAD time
CALL SystemTimeToFileTime(ST, QT)

' FileTime returns 100's of NANOSECONDS so you have to adjust
' **** IncrValue * 10000000 to convert to Nanoseconds
' Now Add the delay To the Quad-element that overlay the FILETIME-struct
QT.qdTime = QT.qdTime + (IncrValue * 10000000)

' Convert back To SYSTEMTIME
FileTimeToSystemTime QT, ST


END FUNCTION



'=================================================
FUNCTION SetNextCheckTime(MinutesToAdvance AS LONG) AS STRING

LOCAL iSeconds  AS LONG
LOCAL st        AS SYSTEMTIME
LOCAL QT        AS QuadFILETIME

' Get the current time
CALL GetlocalTime(st)

' this would always be zero
'db = (MinutesToAdvance \ 60) \ 10000

' *** fixed *** change minutes to seconds
iSeconds = (MinutesToAdvance * 60)

CALL IncrSystemTime(st, iSeconds)

FUNCTION = FormatTime(st, "hh':'mm':'ss tt", "MM-dd-yyyy")

END FUNCTION

chrisc

  • Guest
Re: How to get a future date
« Reply #2 on: April 19, 2018, 03:01:05 PM »
Hello all

any help on this, or clues to increment the dates?

Charles Pegge

  • Guest
Re: How to get a future date
« Reply #3 on: April 19, 2018, 09:23:39 PM »
Hi Chris,

The MS time functions:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx

But there doesn't seem to be a readily available API for calendar calculations.

The tricky part is making adjustments for leap-years:

if mod(year,4)=0 then '2016 2020 2024 etc
  if mod(year,100)<>0 then leap=1 'EXCLUDE  YEARS 2100 2200 2300 etc
  if mod(year,400)=0 then leap=1 'INCLUDE YEARS 2000 2400 2800 etc
end if

In a leap year, an extra day is inserted before day 60.
 
« Last Edit: April 19, 2018, 09:51:41 PM by Charles Pegge »

chrisc

  • Guest
Re: How to get a future date
« Reply #4 on: April 20, 2018, 04:41:22 AM »
Thanxx Charles,
i will try to convert the PB program to do this date increment thing in reply #2

Charles Pegge

  • Guest
Re: How to get a future date
« Reply #5 on: April 23, 2018, 04:29:40 PM »
Hi Chris,

I've added some functions to timeUtil.inc, so you can calculate past and future dates. These are based on the Gregorian Calendar, first instigated by the Roman Catholic Church on the 15th October 1582, and adopted piecemeal by the rest of the world over the next 400 years.

https://en.wikipedia.org/wiki/Gregorian_calendar

Example:
Code: [Select]
uses TimeUtil
SYSTEMTIME ti,tj
ti.wYear=1582
ti.wMonth=10
ti.wDay=15
'friday=5
int d
d=CalcDay ti
d+=365
'print d
CalcDate tj,d
'print "DateTimes:"+chr(13,10)+
'timeStr(ti)  chr(13,10) +
'timeStr(tj)  chr(13,10) +
'tj.wDayOfWeek
print "Dates:"+chr(13,10)+
ti.wYear "-" ti.wMonth "-" ti.wDay chr(13,10) +
tj.wYear "-" tj.wMonth "-" tj.wDay chr(13,10) +
tj.wDayOfWeek

chrisc

  • Guest
Re: How to get a future date
« Reply #6 on: April 23, 2018, 05:04:01 PM »
Thanxx a lot Charles

that was what i needed