Operating System - HP-UX
1748123 Members
3343 Online
108758 Solutions
New Discussion

Re: Date format conversion

 
ManojK_1
Valued Contributor

Date format conversion

Hi,

 

I want to convert date field of one command output to a different format.

 

For example

 

# /usr/lbin/getprpw -m slogint test
slogint=Mon Nov 22 08:31:51 2010

I want to convert the date format in to a different variable in a script to slogintt=11/22/2010

How can i do it?

 

Manoj K

Thanks and Regards,
Manoj K
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Date format conversion


@ManojK_1 wrote:

Hi,

 

I want to convert date field of one command output to a different format.

 

For example

 

# /usr/lbin/getprpw -m slogint test
slogint=Mon Nov 22 08:31:51 2010

I want to convert the date format in to a different variable in a script to slogintt=11/22/2010

How can i do it?

 

Manoj K


Hi:

 

If the format of the output you want to convert is "WEEKDAY MONTH DAY TIME YEAR" as you have shown, then a simple shell script to snip ('cut') the necessary fields and rearrange them is all you need.  A simple array of MONTH names can be defined and indexed to convert the integer MONTH to its name.

 

Regards!

 

...JRF...

H.Merijn Brand (procura
Honored Contributor

Re: Date format conversion

Whatever method you choose to convert any format to another with dates, please consider not using the stupid US date sustem unless for documentation only. When exchanging dates, the US MM/DD/YYYY is almost imposible to unambiguously do caculations on.

When exchanging dates, use ISO 8601 YYYY-MM-DD optionally with time 

In speech and written text, it is fine to use any style you like, as long as you know the audience, but when communicating beyond national borders you'd have to think about this and how many problems you impose on the receivers. 

Living in the Netherlands I realy hate MM/DD/YYYY and the default "Letter" paper format. 

Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: Date format conversion


@H.Merijn Brand (procura wrote:

 

When exchanging dates, use ISO 8601 YYYY-MM-DD optionally with time 



As a convert to this presentation format in logs, particularly, adopting this format makes sorting date stamps easier and avoids the ambiguity of wondering whether "01/11" is MMDD or DDMM.

 

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Date format conversion

>please consider not using the stupid US date system

 

Exactly.  I don't feel so bad because ISO 8601 is also better than the stupid UK (European) system too.  ;-)

H.Merijn Brand (procura
Honored Contributor

Re: Date format conversion

We seem to agree on this

 

In our company we even take this to the databases. We never ever use date or time (stamp) types, but plain numerics (YYYYMMDD and HHMMSS), which makes it extremely easy to sort or select with ranges.

 

Even with dates that allow "unknown" values for day (and month) - as per law - this approach stands strong

 

 select * from foo where d_start between 19200100 and 19370631

 

is legal :) The fact that we can - without annoying and unpredictable conversions - now exchange data between Oracle, Unify, PostgreSQL, MySQL, MariaDB, SQLite and CSV weighs up to the fact that internal calculation support (add so many days) is not available. I really don't care that reporting tools have to write their own dat checkers now. The law tells us that 19611200 is a legal date of birth (somewhere in December 1961), as not all birth certificates actually have a day specified.

 

Date representation (from date/time typed fields) in databases is unprediactable  as it may change based on localisation of the system, the database, the schema, the table, the session, the user settings or even a local environment variable. Fine when making reports for print, but completely useless when exchanging data. The worst in this world is Microsoft Excel: if it looks like a (US) date, it is converted to (the wrong) date, even when written as YYYYMMDD or "YYYYMMDD" (with quotes). Worse: it depends on how you open the data file in how wrong things get converted.

 

To show where trouble starts, here is what different formats are by default supported on my box for the different locales:

 

Date format: %-d.%-m.%Y
  cs_CZ, cs_CZ.utf8
Date format: %A %d %B %Y
  ar_IN, ar_IN.utf8, en_BE, en_BE.utf8, en_BE@euro, en_IN, en_IN.utf8,
  ml_IN, ml_IN.utf8, ta_IN, ta_IN.utf8
Date format: %A %d %b %Y
  bn_BD, bn_BD.utf8, bn_IN, bn_IN.utf8, gu_IN, hi_IN, hi_IN.utf8,
  hne_IN, kn_IN, ks_IN, ks_IN@devanagari, mai_IN, mr_IN, mr_IN.utf8,
  ms_MY, ms_MY.utf8, ne_NP, ne_NP.utf8, pa_IN, pa_IN.utf8, sa_IN,
  sd_IN, sd_IN@devanagari
Date format: %A %d,%B,%Y
  en_SG, en_SG.utf8
Date format: %A %e %B %Y
  ar_SA, ar_SA.utf8
Date format: %A, %B %d, %Y
  en_HK, en_HK.utf8
Date format: %A, %d %B, %Y
  en_PH, en_PH.utf8
Date format: %A, %d ta %b, %Y
  mt_MT, mt_MT.utf8
Date format: %A, %d. %B %Y.
  sh_YU, sh_YU.utf8
Date format: %B %d %A %Y
  te_IN, te_IN.utf8
Date format: %F
  nan_TW@latin
Date format: %OC%Oy %b %Od %A
  my_MM
Date format: %Od-%Om-%Oy
  or_IN
Date format: %Oy/%Om/%Od
  fa_IR, fa_IR.utf8
Date format: %Y-%b-%d
  sq_AL, sq_AL.utf8
Date format: %Y-%m-%d
  csb_PL, de_AT, de_AT.utf8, de_AT@euro, de_BE, de_BE.utf8,
  de_BE@euro, de_LU, de_LU.utf8, de_LU@euro, en_DK, en_DK.utf8, fr_CA,
  fr_CA.utf8, hu_HU, hu_HU.utf8, se_NO, se_NO.utf8, si_LK, sv_SE,
  sv_SE.iso885915, sv_SE.utf8, ug_CN
Date format: %Y.%m.%d
  lt_LT, lt_LT.utf8, mn_MN, mn_MN.utf8
Date format: %Y.%m.%d.
  lv_LV, lv_LV.utf8
Date format: %Y�N%m��%d��
  ja_JP.sjis
Date format: %Y�~%m��%d�� %A
  zh_HK
Date format: %Y�� %m�� %d�
  ko_KR.euckr
Date format: %Y�%m�%d�
  zh_CN, zh_CN.gb18030, zh_CN.gbk, zh_SG, zh_SG.gbk
Date format: %Yǯ%m��%d�
  ja_JP.eucjp
Date format: %Y年%m月%d日
  ja_JP.utf8, zh_CN.utf8, zh_SG.utf8
Date format: %Y年%m月%d日 %A
  zh_HK.utf8
Date format: %Y년 %m월 %d일
  ko_KR.utf8
Date format: %a %e.%b %Y
  is_IS, is_IS.utf8
Date format: %a, %Y.eko %bren %da
  eu_ES, eu_ES.utf8, eu_ES@euro
Date format: %d %b %Y
  kl_GL, kl_GL.utf8
Date format: %d %b, %Y
  ar_AE, ar_AE.utf8, ar_BH, ar_BH.utf8, ar_DZ, ar_DZ.utf8, ar_EG,
  ar_EG.utf8, ar_IQ, ar_IQ.utf8, ar_JO, ar_JO.utf8, ar_KW, ar_KW.utf8,
  ar_LB, ar_LB.utf8, ar_LY, ar_LY.utf8, ar_MA, ar_MA.utf8, ar_OM,
  ar_OM.utf8, ar_QA, ar_QA.utf8, ar_SD, ar_SD.utf8, ar_SY, ar_SY.utf8,
  ar_TN, ar_TN.utf8, ar_YE, ar_YE.utf8
Date format: %d-%m-%Y
  da_DK, da_DK.utf8, pt_BR, pt_BR.utf8, pt_PT, pt_PT.utf8, pt_PT@euro,
  tr_CY, tr_CY.utf8, tr_TR, tr_TR.utf8
Date format: %d-%m-%y
  fy_NL, nl_AW, nl_BE, nl_BE.utf8, nl_BE@euro, nl_NL, nl_NL.utf8,
  nl_NL@euro, pap_AN
Date format: %d. %b %Y
  nb_NO, nb_NO.utf8, nn_NO, nn_NO.utf8, no_NO, no_NO.utf8
Date format: %d. %m. %Y
  sl_SI, sl_SI.utf8
Date format: %d. %m. %y
  fr_CH, fr_CH.utf8, fur_IT, it_CH, it_CH.utf8, sc_IT
Date format: %d.%m.%Y
  aa_DJ, aa_DJ.utf8, az_AZ.utf8, be_BY, be_BY.utf8, be_BY@latin,
  ber_DZ, ber_MA, br_FR, br_FR.utf8, br_FR@euro, bs_BA, bs_BA.utf8,
  crh_UA, de_CH, de_CH.utf8, de_DE, de_DE.utf8, de_DE@euro, et_EE,
  et_EE.iso885915, et_EE.utf8, fi_FI, fi_FI.utf8, fi_FI@euro, fr_LU,
  fr_LU.utf8, fr_LU@euro, fy_DE, hr_HR, hr_HR.utf8, hsb_DE,
  hsb_DE.utf8, kk_KZ, kk_KZ.utf8, ky_KG, li_BE, li_NL, mg_MG,
  mg_MG.utf8, mk_MK, mk_MK.utf8, nds_DE, nds_NL, oc_FR, oc_FR.utf8,
  pl_PL, pl_PL.utf8, ro_RO, ro_RO.utf8, ru_RU, ru_RU.koi8r,
  ru_RU.utf8, ru_UA, ru_UA.utf8, rw_RW, sk_SK, sk_SK.utf8, so_DJ,
  so_DJ.utf8, sv_FI, sv_FI.utf8, sv_FI@euro, tg_TJ, tg_TJ.utf8, tk_TM,
  tt_RU.utf8, tt_RU@iqtelif.UTF-8, wo_SN
Date format: %d.%m.%Y.
  sr_ME, sr_RS, sr_RS@latin
Date format: %d.%m.%y
  cy_GB, cy_GB.utf8, ga_IE, ga_IE.utf8, ga_IE@euro, uk_UA, uk_UA.utf8
Date format: %d/%m-%Y
  fo_FO, fo_FO.utf8
Date format: %d/%m/%Ey
  lo_LA, th_TH, th_TH.utf8
Date format: %d/%m/%Y
  aa_ER, aa_ER.utf8, aa_ER@saaho, aa_ET, aa_ET.utf8, af_ZA,
  af_ZA.utf8, am_ET, am_ET.utf8, byn_ER, byn_ER.utf8, dv_MV, el_CY,
  el_CY.utf8, el_GR, el_GR.utf8, en_BW, en_BW.utf8, en_NG, en_ZA,
  en_ZA.utf8, en_ZW, en_ZW.utf8, fr_FR, fr_FR.utf8, fr_FR@euro,
  gez_ER, gez_ER@abegede, gez_ET, gez_ET@abegede, it_IT, it_IT.utf8,
  it_IT@euro, ku_TR, ku_TR.utf8, nr_ZA, nso_ZA, om_ET, om_ET.utf8,
  om_KE, om_KE.utf8, pa_PK, sid_ET, sid_ET.utf8, so_ET, so_ET.utf8,
  so_KE, so_KE.utf8, so_SO, so_SO.utf8, ss_ZA, st_ZA, st_ZA.utf8,
  ti_ER, ti_ER.utf8, ti_ET, ti_ET.utf8, tig_ER, tig_ER.utf8, tn_ZA,
  ts_ZA, ur_PK, ur_PK.utf8, ve_ZA, vi_VN, vi_VN.tcvn, wa_BE,
  wa_BE.utf8, wa_BE@euro, xh_ZA, xh_ZA.utf8, zu_ZA, zu_ZA.utf8
Date format: %d/%m/%y
  an_ES, an_ES.utf8, ast_ES, ast_ES.utf8, ca_AD, ca_AD.utf8, ca_ES,
  ca_ES.utf8, ca_ES@euro, ca_FR, ca_FR.utf8, ca_IT, ca_IT.utf8, en_AG,
  en_AU, en_AU.utf8, en_CA, en_CA.utf8, en_GB, en_GB.iso885915,
  en_GB.utf8, en_IE, en_IE.utf8, en_IE@euro, en_NZ, en_NZ.utf8, es_AR,
  es_AR.utf8, es_BO, es_BO.utf8, es_CL, es_CL.utf8, es_CO, es_CO.utf8,
  es_CR, es_CR.utf8, es_DO, es_DO.utf8, es_EC, es_EC.utf8, es_ES,
  es_ES.utf8, es_ES@euro, es_GT, es_GT.utf8, es_HN, es_HN.utf8, es_MX,
  es_MX.utf8, es_NI, es_NI.utf8, es_PA, es_PA.utf8, es_PE, es_PE.utf8,
  es_PR, es_PR.utf8, es_PY, es_PY.utf8, es_SV, es_SV.utf8, es_US,
  es_US.utf8, es_UY, es_UY.utf8, es_VE, es_VE.utf8, fr_BE, fr_BE.utf8,
  fr_BE@euro, gd_GB, gd_GB.utf8, gl_ES, gl_ES.utf8, gl_ES@euro, gv_GB,
  gv_GB.utf8, ha_NG, he_IL, he_IL.utf8, ht_HT, id_ID, id_ID.utf8,
  ig_NG, ik_CA, iw_IL, iw_IL.utf8, kw_GB, kw_GB.utf8, lg_UG,
  lg_UG.utf8, mi_NZ, mi_NZ.utf8, shs_CA, uz_UZ, uz_UZ@cyrillic, yi_US,
  yi_US.utf8, yo_NG
Date format: %e %B %Y
  km_KH
Date format: %e-%m-%Y
  as_IN.utf8
Date format: %e.%m.%Y
  bg_BG, bg_BG.utf8
Date format: %m/%d/%Y
  en_US, en_US.UTF-8, en_US.iso885915, en_US.utf8, ka_GE, ka_GE.utf8
Date format: %m/%d/%y
  C, POSIX, fil_PH, hy_AM, hy_AM.armscii8, iu_CA, ja_JP.shiftjisx0213,
  tl_PH, tl_PH.utf8
Date format: �褸%Y�~%m��%d��
  zh_TW
Date format: ��%Yȡ%m�%d�
  zh_TW.euctw
Date format: د %Y د %B %e
  ps_AF
Date format: པསྱི་ལོ%yཟལ%mཚེས%d
  bo_CN, bo_IN, dz_BT
Date format: 西元%Y年%m月%d日
  zh_TW.utf8

 Feel my pain? Feel why I do not like NLS, locales and anything related when doing conversions or data-exchange?

 

Enjoy, Have FUN! H.Merijn
Matti_Kurkela
Honored Contributor

Re: Date format conversion

Since your question is a generic one, it's hard to give any specific advice.

 

  1. Find out if you can request the output in different formats. If you can, find the format that is easiest for you to parse.
  2. Write a script that parses the input and then converts it to the form you require.

There are probably almost an infinite number of ways to solve each problem of this type.

In your example, you might do something like this:

#!/bin/sh

USERNAME=test

# This function takes the entire output of "getprpw -m slogint" as arguments. convslogint() {
MONTHNAME=$2 DAY=$3 YEAR=$5 case "$MONTHNAME" in Jan) MONTH="01" ;; Feb) MONTH="02" ;; Mar) MONTH="03" ;; Apr) MONTH="04" ;; May) MONTH="05" ;; Jun) MONTH="06" ;; Jul) MONTH="07" ;; Aug) MONTH="08" ;; Sep) MONTH="09" ;; Oct) MONTH="10" ;; Nov) MONTH="11" ;; Dec) MONTH="12" ;; *) echo "ERROR: unknown month name $MONTHNAME" >&2 return 1 ;; esac echo "$MONTH/$DAY/$YEAR" # FIXME: not an ISO 8601 time format! return 0 } # example function call slogintt=$(convslogint $(/usr/lbin/getprpw -m slogint $USERNAME)) echo "slogintt=$slogintt"

 

MK