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.
Mainframe files often store a date and a time in one of these ways:
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.
| Layout | Bytes | Sort key |
|---|---|---|
| YYYYMMDD + HHMMSS | 8 + 6 | SORT FIELDS=(date_pos,8,CH,A,time_pos,6,CH,A) |
| YYYYMMDDHHMMSS | 14 | SORT FIELDS=(1,14,CH,A) |
| YYYYDDD + HHMMSS | 7 + 6 | SORT FIELDS=(julian_pos,7,CH,A,time_pos,6,CH,A) |
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:
Example (syntax is product-dependent):
12OUTFIL 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.
When the record has separate date and time fields in sortable form:
1SORT 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.
Use INCLUDE or OMIT with COND= on the date field (and optionally the time field). For example, keep records in January 2024:
1INCLUDE 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.
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:
12INREC 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.
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.
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.
1. How can you put the current time (run time) in a DFSORT report header?
2. Your input has date at 1,8 (YYYYMMDD) and time at 9,6 (HHMMSS). How do you sort by date then time?
3. What is a common mainframe timestamp layout for sorting?
4. Can DFSORT do date/time arithmetic on a timestamp field (e.g. add 1 hour)?
5. Why put both DATE= and TIME= in a report header?