M Web Magazine 006 (March 5, 1998 - June 4, 1998)

M Tutorial (part 6/6)

 

Of Date and Time

M is Year 2000 compliant. It has been compliant long before the Y2K publicity concern shot to the sky. Obviously, M implementations get their date from the computer systems they operate on meaning that whether they will report Jan 1st 1900 or Jan 1st 2000 does depend on what the computer provides them. If you follow newsgroups, or some M publication, you might find some reference to M systems not being Y2K compliant. Remember that even though M does provide Y2K facilities, nothing stops a programmer from adopting in his or her code non compliant dates.

 

$H

M has a special variable (this is not a function) that stores within the date and time. WRITE $H will give <date>,<time>, where <date> is the number of days since Dec 31, 1840 and <time> is the number of seconds since midnight.

 

$ZDATE(<idate>[,<type>]) or $ZD (<idate>[,<type>])

This function converts the internal integer date format to human understandable format. <type> allows you to specify how the output date will appear.

INTFN123Þ;Program demonstrates date functions - Chris Bonnici - Oct 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞW !,"Type 1: ",$ZD($H,1)
ÞW !,"Type 2: ",$ZD($P($H,",",1),2)
ÞW !,"Type 3: ",$ZD(+$H,3)
ÞQ

INTFN123 displays the current date in the various formats. One question that might come to mind is how we are varying the $H in the different examples. The first $ZD uses $H without alteration. Some functions such as $ZD might be able to detect whether the date alone or the date and time is being supplied. The second example used $P to specifically extract the integer date part. By placing a unitary plus in front of $H, you are converting the string <date>,<time> into <date>. This is not something unique to dates as the following should help clarify.

WRITE +"12345,2223"

 

System Supplied Date Routines

%D

The %D utility displays the current date as does $ZDATE in the form DD-MMM-YY.

%D has a label called INT. When you call %D via this label, the date formatting operation is not outputted to the screen but two variables, %DAT and %DAT1 are created with the date. %DAT holds the date as MM/DD/YYYY and %DAT1 will contain the date as DD-MMM-YY.

K
D INT^%D
W

%T

This utility displays the current time in the form HH:MM APM. As with %D, you can D INT^%T to have the time stored within variables rather then displayed on screen. The internal call will result in two variables: %TIM and %TIM1.

The value in %TIM is in the form HH:MM (HH being based upon a 24hr clock) while %TIM1 stores the dates as it is displayed.

 

%DI

Up to now we have limited ourselves to outputting the current date and time. The Date Input routine allows you to convert a human readable date to the internal ($H) format (i.e. number of days since Dec 31, 1840.

To convert a date, assign the human readable date to the variable %DS. The date may be in any of the following formats:

  Example
MMMM DD, YYYY OCTOBER 20, 1998
MMM DD, YYYY OCT 20, 1998
DD MMM YYYY 20 OCT 1998
M/D/YYYY 10/20/1998
M-D-YYYY 10-20-1998
M D YYYY 10 20 1998
T± <num>  

 

Although we quote all years as four digits, you can use the 2 digit representation. M will assume the current century.

T is used to mean today. The example below demonstrates how to work with today.

INTFN124Þ;Program demonstrates date functions - Chris Bonnici - Oct 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞS %DS="T-100"
ÞD ^%DI
ÞW:$D(%ER) !!,"Error"
ÞW !!,%DN
ÞQ

The routine creates %DN containing the corresponding internal computer date.

If an error occurs the variable %ER will be defined and have a value of 1. If everything gets converted smoothly this variable will not be defined. Use $DATA to check for this condition.

 

%DO

This utility converts an internal computer date to the human understandable format. As with %DI the same three variables are used:

%DN The internal form of the date. This value is not changed by the utility.
%DS The external format of the date.
%ER Not defined if there are not problems during the conversion, otherwise it will be defined and will hold a value of 1.

This routine has a number of entry points. Calling different entry points will alter what will be stored in %DS.

Entry Date Format Eg
^%DO MMM DD YYYY OCT 20 1998
INT^%DO DD MMM YYYY 20 OCT 1998
300^%DO YYYYMMDD 19981020
400^%DO MM/DD/YY 10/20/98

 

%TI

Now that we’ve looked at converting dates, we’ll focus on the time. %TI utility converts an external time to its corresponding internal (i.e. $HOROLOG) value.

The variables involved in this operation are:

%TS The external form of the time. This value is not changed by the utility.
%TN The corresponding internal computer time.
%ER Defined if an error occurs during conversion.

 

As with %DS, %TS can take numerous formats mainly:

  • 24 hour clock with colons or spaces between hours and minutes
  • 12 hour clock with AM or PM and colons or spaces between hours and minutes.
  • The hour only based on a 24 hour clock.
  • The use of the terms NOON and MIDNIGHT

Try out the various combinations with our little program below.

INTFN125Þ;Program demonstrates date functions - Chris Bonnici - Oct 1997
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN %TS,%TN,%ER
10ÞR !,"Enter Time :",%TS
ÞQ:%TS=""
ÞD ^%TI
ÞW:$D(%ER) !!,"Error"
ÞW !!,%TN
ÞG 10

 

%TO

Converting from the internal to the external format should by now be a piece of cake. Will just let you know that the two variables you will need here:

  • %TN into which you place the internal format and
  • %TS into which you’ll eventually have the human readable format.

Calling %TO returns the time based on the 24 hour clock. Entry point 100 (100^%TO) returns the time with AM/PM at the end.

 

Bye

We hope you’ve enjoyed this mega issue of M Tutorial. We would like to hear from you and maybe share some of your code. Till we meet again, keep that code rolling.

E&OE

1