Skip to main content

How can one access locale date/time within Cobol?

  • February 15, 2013
  • 0 replies
  • 0 views

Chris Glazier
Forum|alt.badge.img+2

Problem:

How can one access locale date/time within Cobol?

Resolution:

This demo demonstrates using strftime to format the local time in the default representation for the current locale:

       $set sourceformat(free) mf

working-storage section.

    01 time-parameters.

       03 time-now          pic x(4) comp-5.

    01 localtime-parameters.

       03 localtime-tm      pointer.

    01 strftime-parameters.

       03 locale-time       pic x(60).

       03 buffer-size       pic x(4) comp-5.

       03 format-string     pic xxx value z"%c".

procedure division.

    *> Get current time in UTC

    call "time" using by reference time-now

    *> Translate to local timezone

    call "localtime" using by reference time-now returning localtime-tm

    *> Translate to string representation of date and time in current

    *> locale

    move length of locale-time to buffer-size

    call "strftime"

       using

          by reference locale-time

          by value buffer-size

          by reference format-string

          by value localtime-tm

    *> Remove C null-byte string terminator

    inspect locale-time replacing all x"00" by " "

    *> Display the result

    display locale-time

    stop run.

Sample output, on RedHat EL4

> export LANG=en_GB

> cobrun demo

Mon 05 Mar 2007 17:28:17 GMT

> export LANG=es_ES

> cobrun demo

lun 05 mar 2007 17:28:44 GMT

Old KB# 2065