IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Paradox
Search Paradox Group:

Advanced
-Collapse +Expand Paradox To/From
To/FromCODEGuides
-Collapse +Expand Paradox Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBDesktop Data...Paradox & Ob...ObjectPAL Co...OPAL: Langua...   Print This     
  From the January 2010 Issue of Prestwood eMag
 
Paradox OPAL: Language Details:
ObjectPAL Date, Time, and DateTime Data Types
 
Posted 16 years ago on 6/5/2008 and updated 10/30/2008
Take Away: Demonstration of how to handle dates and times in ObjectPAL.

KB101162

Date, Time, DateTime

Although Time and Date data types arent related, each shares data and methods with the DateTime data type. When deciding between these three data types, ask yourself whether you need time in the same field as the date information. Whenever possible, separate date and time.

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: msgInfo("Beginning Date", Date(0))
 4: endMethod

This displays the string 00/00/0000 in a dialog box. You can type in negative numbers that represent B.C. dates, too.

Subtract Two Dates

If you subtract two dates, you get another date. For example, type the following code into the ushbutton event of a button.

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3:   message(date("01/15/94") - date("01/01/94"))
 4: endMethod 

This displays 01/14/0001 in the status bar. This information isn't very useful. Usually, you want the number of days or years between the two dates. The trick to getting the number of days between two dates is to cast the result as a number. For example, type the following code into the ushbutton event of a button:

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: var
 4:   dBorn   Date
 5: endVar
 6:
 7: dBorn = Date("01/20/1967")
 8: dBorn.view("Enter your birthdate")
 9: msgInfo("Days since your birthdate", Number(Today() - dBorn))
10: endMethod

 

Calculate Years

To calculate years, simply cast the value as a year. For example, type the following code into the ushbutton event of a button:

 1: ;DATATYPE :: btnBirth :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: var
 4:   dBorn   Date
 5: endVar
 6:
 7: dBorn = Date("01/20/1967")
 8: dBorn.view("Enter your birthdate")
 9: msgInfo("Your age", Year(Today() - dBorn))
10: endMethod

Calculate Seconds, Hours, and Years

The following pushButton code snippet uses a similar technique to the one just demonstrated to display the number of seconds, hours, days, and years since your birth.

method pushButton(var eventInfo Event)
 var
  dtBirth Datetime
  n       Number
 endVar
 ;Set your birthdate and time
 dtBirth = DateTime("07:32:00 AM, 01/08/65")
 dtBirth.view("Enter your birthday and time")
 ;show years, days, hours, minutes, and seconds old
 n = number(year(today()) - year(dtBirth))
 n.view("Number of years old")
 n = number(today() - dtBirth)/1000/60/60/24
 n.view("Number of days old")
 n = number(today() - dtBirth)/1000/60/60
 n.view("Number of hours old")
 n = number(today() - dtBirth)/1000/60
 n.view("Number of minutes old")
 n = number(today() - dtBirth)/1000
 n.view("Number of seconds old")
endmethod

 

Time Variables

A Time variable is stored in the format HH:MM:SS AM/PM. You could use any of the following as separators: blank, tab, space, comma, hyphen, slash, period, colon, or semicolon. If you want, type the following code into the ushbutton event of a button. (All the following strings are legal time strings in ObjectPAL.)

 1: ;DATATYPE :: btnTimeFormats :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: var t Time endVar
 4: t = time("10:05:32 AM")
 5: t.view()
 6:
 7: t = time("10;05;32 AM")
 8: t.view()
 9:
10: t = time("10 05 32 AM")
11: t.view()
12:
13: t = time("10,05,32 AM")
14: t.view()
15:
16: t = time("10/05/32 AM")
17: t.view()
18: endMethod

Note that although you can type time value strings in any of the legal formats of ObjectPAL, the time in the displayed view box is the same because it is guided by the operating system. It's displayed to the user in the format specified in Control Panel. The values the user inputs must be in accordance with his or her Control Panel settings. Internally, ObjectPAL stores the time all the way down to the millisecond.

Return Current Time

Call Time() with no parameters to return the current time. For example, type the following code into the ushbutton event of a button:

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3:   msgInfo("Current time", time())
 4: endMethod

The user's Control Panel settings determine whether the 12-hour time format (HH:MM:SS AM/PM) or the 24-hour (military) time format is used. Sometimes you want to convert 12-hour time to 24-hour time, however. The following routine casts a string as Time and then displays it in 24-hour format. You can type the code into the ushbutton event of a button.

Demo Files: WORLD-T.FSL

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: var
 4:   s   String
 5:   t   Time
 6: endVar
 7:
 8: formatSetTimeDefault("hh:mm:ss am")
 9: s = "2:20:00 PM"
10: t = time(s)
11: formatAdd("24", "to(%h:%m:%s)na()np()")
12: formatSetTimeDefault("24")
13: t.view()
14: endMethod

 

Subtract Two Times

Suppose that you need to get the number of seconds between two times. Type the following into the ushbutton event of a button:

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: var
 4:   t1, t2 Time
 5: endVar
 6:
 7: t1 = time("1:35:30 PM")
 8: t2 = time("2:10:15 PM")
 9: view(Number(t2 - t1) / 1000)  ;For seconds.
10: view(Number(t2 - t1) / 1000 / 60);For minutes.
11: view(Number(t2 - t1) / 1000 / 60 / 60) ;For hours.
12: endMethod

DateTime Data type

DateTime is a special data type that stores both date and time values in the same variable. DateTime stores data in the form of hour-minute-second-millisecond year-month-day. The DateTime() method returns the current date and time. For example, to return the current date and time in a message information box, type line 3 into the ushbutton event of a button.

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: msgInfo("Current Date & time", DateTime())
 4: endMethod

As an interesting experiment, you can cast values with the procedure DateTime() and display the results. This experimenation helps you to understand the numbers behind the DateTime data type. For example, type line 3 into the pushButton event of a button:

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: msgInfo("Beginning Date/Time", DateTime(0))
 4: endMethod

Line 3 displays 12:00:00 AM, 00/00/0000 in a dialog box. This shows you that going back all the way to year 0 is legal in ObjectPAL. In fact, you can even use B.C. dates and times (negative numbers).

DateTimes are numbers!

If you store data in an alphanumeric field type and later need to do a calculation on the value, you must cast the string in the field as a number. To cast a string as a number, use Number and DateTime() together. For example, type lines 3-8 into the ushbutton event of a button:

 1: ;Button :: pushbutton
 2: method pushbutton(var eventInfo Event)
 3: var
 4:   nBirth   Number
 5: endVar
 6:
 7: nBirth = Number(DateTime("07:30:00 PM, January 20, 1967"))
 8: msgInfo("Lisa's Birthday", nBirth)
 9: endMethod

The preceding line 8 displays the number 62042700600000 in a dialog box. This isn't very useful, but you can use this code in a calculation with values (including another DateTime converted to a Number).

Linked Message Board Threads

 How do you add/subtract time? in ObjectPAL MB Topic (1 replies)

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P183A1
Enter key:
KB Post Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101162 Counter
14397
Since 6/5/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]