MainframeMaster

Timestamp Handling

A timestamp is a date plus a time of day—for example YYYYMMDD plus HHMMSS. In DFSORT you deal with timestamps in two main ways: (1) data records that contain date and time fields (or a single combined field), and (2) report headers and trailers where you want to print the run date and run time (when the job executed). For data, you sort or filter by date and optionally time; the layout that sorts correctly as character is usually date first (YYYYMMDD) then time (HHMMSS). For headers, DFSORT often provides DATE= and TIME= (or TIMENS=) keywords so you can insert the current date and time without reading them from the data. This page covers timestamp layouts in data, using time in headers and trailers, and sorting/filtering by date and time. Exact syntax for time formats is product-dependent.

Date & Time Processing
Progress0 of 0 lessons

Timestamp Layouts in Data

Mainframe files often store a date and a time in one of these ways:

  • Separate fields: Date at one position (e.g. 1,8 as YYYYMMDD) and time at another (e.g. 9,6 as HHMMSS). You sort with two keys: SORT FIELDS=(1,8,CH,A,9,6,CH,A).
  • Single combined field: 14 bytes as YYYYMMDDHHMMSS. Character order is chronological, so SORT FIELDS=(1,14,CH,A) orders by timestamp.
  • Julian + time: 7-byte YYYYDDD plus 6-byte HHMMSS. Sort by Julian date then time.

As long as the date part is in year-first form (YYYYMMDD or YYYYDDD), character comparison gives correct order. Time as HHMMSS (or HHMM) also sorts correctly as character within the same date.

Common timestamp layouts
LayoutBytesSort key
YYYYMMDD + HHMMSS8 + 6SORT FIELDS=(date_pos,8,CH,A,time_pos,6,CH,A)
YYYYMMDDHHMMSS14SORT FIELDS=(1,14,CH,A)
YYYYDDD + HHMMSS7 + 6SORT FIELDS=(julian_pos,7,CH,A,time_pos,6,CH,A)

Run Date and Run Time in Headers and Trailers

In OUTFIL report headers (HEADER1=, HEADER2=) and trailers (TRAILER1=, etc.), you often want to show when the job ran—the run date and run time. DFSORT typically provides:

  • DATE= (or DATENS=) with a format code to insert the current date (e.g. Y4T for YYYYMMDD, or a display format with separators).
  • TIME= or TIMENS= (or equivalent) with a format code to insert the current time (e.g. HHMMSS or HHMM).

Example (syntax is product-dependent):

text
1
2
OUTFIL HEADER1=(1:'Report run on ',DATE=(Y4T),X,TIME=(HHMMSS),80:'Page ',PAGE) TRAILER1=(1:'End of report ',DATE=(Y4T),X,TIME=(HHMMSS))

That would produce a header line with literal text, the run date, a space, the run time, and a page number. The exact format codes (Y4T, HHMMSS, etc.) and keyword names (TIME vs TIMENS) vary by DFSORT version; check your manual. Some products use TIMENS(24) for 24-hour time or similar notation.

Sorting by Date and Time

When the record has separate date and time fields in sortable form:

text
1
SORT FIELDS=(1,8,CH,A,9,6,CH,A)

Primary key: positions 1–8 (YYYYMMDD). Secondary key: positions 9–14 (HHMMSS). Records are ordered by date first, then by time within the same date. If the timestamp is in non-sortable form (e.g. MMDDYYYY and HHMM in different order), use INREC to build YYYYMMDD and HHMMSS at fixed positions, then sort on those.

Filtering by Timestamp Range

Use INCLUDE or OMIT with COND= on the date field (and optionally the time field). For example, keep records in January 2024:

text
1
INCLUDE COND=(1,8,CH,GE,C'20240101',AND,1,8,CH,LE,C'20240131')

To restrict by time as well (e.g. between 09:00 and 17:00), add conditions on the time field: (9,6,CH,GE,C'090000',AND,9,6,CH,LE,C'170000'). Use INREC first if the date or time is not in a comparable form.

Building a Timestamp in INREC

If the input has date and time in scattered or non-sortable form, use INREC to build a single 14-byte field (YYYYMMDDHHMMSS) or to place date and time at fixed positions. For example, date at 50,8 (MMDDYYYY) and time at 58,6 (HHMMSS). Convert date to YYYYMMDD and move time:

text
1
2
INREC BUILD=(1:50,8,Y4W,TOGREG=Y4T,9:58,6,15:1,49,65:59,20) SORT FIELDS=(1,14,CH,A)

Output positions 1–8: converted date. Positions 9–14: time. Then SORT FIELDS=(1,14,CH,A) sorts by the full timestamp. Adjust positions and lengths to match your layout.

Time Format in Headers (Product-Dependent)

Some DFSORT versions support TIMENS with a format code (e.g. 24 for HHMMSS) in HEADER1 or TRAILER1. Others use TIME= with different format names. TIME2 or similar may be available in BUILD/OUTREC but not in headers. If the format you need (e.g. HHMM without seconds) is not available in headers, you can sometimes build it in OUTREC at a temporary position and reference that in the header, or accept the format the product provides. Check your documentation.

Explain It Like I'm Five

A timestamp is like writing "December 25, 2024, at 2:30 in the afternoon." The computer stores that as numbers: first the date (20241225), then the time (143000 for 14:30:00). When we sort a list of those, we want the earliest date first, and if two records have the same date, the earliest time first. So we put the date first and the time right after it. When we print a report, we also like to show "this report was printed on this date at this time" so people know when it was made—that's what the run date and run time in the header are for.

Exercises

  1. Input has date at 20,8 (YYYYMMDD) and time at 28,6 (HHMMSS). Write a SORT FIELDS= that sorts by date then time.
  2. Why is YYYYMMDDHHMMSS (14 bytes) a good choice for a single timestamp field?
  3. Where would you look to find the exact keyword for "current time" in a header on your system?

Quiz

Test Your Knowledge

1. How can you put the current time (run time) in a DFSORT report header?

  • Only DATE= is supported
  • Use TIME= or TIMENS= (or equivalent) in HEADER1= or HEADER2=—e.g. HEADER1=(1:'Run at ',TIME=(...)) or TIMENS; exact keyword and format are product-dependent
  • Only in OUTREC
  • Time cannot be in headers

2. Your input has date at 1,8 (YYYYMMDD) and time at 9,6 (HHMMSS). How do you sort by date then time?

  • SORT FIELDS=(1,14,CH,A) only
  • SORT FIELDS=(1,8,CH,A,9,6,CH,A)—primary key date, secondary key time; both sort correctly as character
  • Time cannot be a sort key
  • Use INREC to combine them first

3. What is a common mainframe timestamp layout for sorting?

  • MMDDYYYYHHMMSS
  • YYYYMMDD followed by HHMMSS (or HHMM)—year and day order first, then time; sorts correctly as character
  • Only Julian + time
  • Epoch seconds only

4. Can DFSORT do date/time arithmetic on a timestamp field (e.g. add 1 hour)?

  • Yes, always
  • Product-dependent; some versions have time arithmetic. Often you do such calculations in a COBOL or REXX step and then sort
  • Only ADDDAYS
  • Only in headers

5. Why put both DATE= and TIME= in a report header?

  • Only one is allowed
  • To show when the report was produced (run date and run time)—useful for audit and freshness
  • Only for sorting
  • TIME= is only for data records