WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Time - Timer and Date Functions
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Unix standard time calculator

Keywords: 	  Unix standard time calculator 

Question:

Does anyone have a routine that will calculate Unix standard time based on seconds since standard epoch of 1/1/1970 into a readable date time format?

Answer:

seconds=1239257890

t1=strcat("0000:00:00:00:00:",seconds)
t2=TimeAdd("1970:01:01:00:00:00",t1)

Message(seconds,t2)
Converts it to the standard Winbatch YYYY;MM:dd:hh:mm:ss format, then it is simply a matter of converting it to a desired rendition.

You can then change the ymdhms format into something more readable using the following.

If you need times, you can add some more options to the select statement like "mm/dd/yy hh:mm:ss", etc. and strcat the time portion onto the return string.

#definefunction format_date(dateymd, format)
        format = strlower(format)
        ; make sure the date is in ymdhms() format
        if strlen(dateymd)<10 then return(-1)
        ; parse the date string...
        year = strsub(dateymd, 1, 4)
        month = strsub(dateymd, 6, 2)
        day = strsub(dateymd, 9, 2)

        if strindexwild(format, "yyyy", 1) == 0
                ;       then we use the two digit date
                year = strsub(year, 3, 2)
        endif
        if strindexwild(format, "-", 1) > 0 || format == "sql"
                ; use the dash as a delim
                delim = "-"
        else
                ; use the slash as a delim
                delim = "/"
        endif
        ; make sure the date and format are valid
        select @true
                case format == "sql" || format == "dd-mmm-yy"
                        monthnames = "JAN;FEB;MAR;APR;MAY;JUN;JUL;AUG;SEP;OCT;NOV;DEC"
                        sqlmonth = itemextract(month, monthnames, ";")
                        fdate = strcat(day, delim, sqlmonth, delim, year)
                break
                case format == "mm/dd/yy"
                        fdate = strcat(month, delim, day, delim, year)
                break
                case format == "mm/dd/yyyy"
                        fdate = strcat(month, delim, day, delim, year)
                break
                case format == "mm-dd-yy"
                        fdate = strcat(month, delim, day, delim, year)
                break
                case format == "mm-dd-yyyy"
                        fdate = strcat(month, delim, day, delim, year)
                break
                case @true
                        return
                break
        endselect
        return(fdate)
#endfunction

now = timeymdhms()
message("Debug", format_date(now, "mm/dd/yy"))

Article ID:   W15307
File Created: 2002:09:05:13:51:18
Last Updated: 2002:09:05:13:51:18