ATOUTFOX
COMMUNAUTÉ FRANCOPHONE DES PROFESSIONNELS FOXPRO
Visual FoxPro : le développement durable

Formatter la date   



L'auteur

Gregory Adam
Belgique Belgique
Membre Actif (personne physique)
# 0000001121
enregistré le 04/06/2006

Fiche personnelle


Note des membres
pas de note

Contributions > 05 - API et appels systèmes

Formatter la date
# 0000000661
ajouté le 25/12/2008 13:56:51 et modifié le 25/12/2008
consulté 8358 fois
Niveau débutant

Description

Differentes facons de formatter une date dans la langue de l'utilisateur, ou toute autre langue supportee par windows

 

Code source :
#include  "FoxPro.h"

function do_it()

  && Format of date: see http://msdn.microsoft.com/en-us/library/ms776293.aspx

  && language of current user
  ?Date_Format(date())
  ?Date_Format(date(), 'dd MMMM yyyy')

  && see foxpro.h
  && French
  ?Date_Format(date(), 'dd MMMM yyyy', LANG_FRENCH)
  ?Date_Format(date(), '', LANG_FRENCH)
  ?Date_Format(date(), 'dddd, dd MMMM yyyy', LANG_FRENCH)

  ?Date_Format(date(), 'dd MMMM yyyy', LANG_SPANISH)
  ?Date_Format(date(), 'dd MMMM yyyy', LANG_ITALIAN)


endfunc
*---------------------------------------------------------------------------
function Date_Format(d, lpFormat, Language)

  #define  DATE_SHORTDATE  0x00000001
  #define  DATE_LONGDATE  0x00000002

  #define  LOCALE_SYSTEM_DEFAULT  0x00000800
  #define  LOCALE_USER_DEFAULT    0x00000400
  * LCID GetThreadLocale(void)

  declare long GetThreadLocale in win32api

  *int GetDateFormat(
  *  LCID Locale,               // locale
  *  DWORD dwFlags,             // options
  *  CONST SYSTEMTIME *lpDate,  // date
  *  LPCTSTR lpFormat,          // date format
  *  LPTSTR lpDateStr,          // formatted string buffer
  *  int cchDate                // size of buffer
  *);

  declare integer GetDateFormat in win32api ;
      long  Locale, ;
      long  dwFlags, ;
      string@  lpDate, ;
      String@  lpFormat, ;
      string@  lpDateStr, ;
      integer  cchDate

  local lpDate, lpFormat, DateFormatted, nchars
  lpDate = SystemTimeStruct(m.d)
  DateFormatted = space(128)

  do case
  case !inlist(vartype(m.d), T_DATE, T_DATETIME )
    assert FALSE
    return ''
  endcase

  lpFormat = evl(m.lpFormat, [ddd', 'dd MMM yyyy])

  nchars = GetDateFormat( evl(m.Language, GetThreadLocale()), 0, @m.lpDate, @m.lpFormat, @m.DateFormatted, len(m.DateFormatted))

  do case
  case empty(m.nchars)
    return ''

  case inlist(vartype(d), T_DATE)
    return left(m.DateFormatted, m.nchars-1)

  case inlist(vartype(d), T_DATETIME )
    return left(m.DateFormatted, m.nchars-1) + ' ' + ttoc(m.d,2)
  endcase

endfunc
*---------------------------------------------------------------------------
function SystemTimeStruct(d)

  local s

  do case
  case inlist(vartype(m.d), T_DATE, T_DATETIME )
    s =    BinToWord(year(m.d)) ;
      +  BinToWord(Month(m.d)) ;
      +  BinToWord(mod(dow(m.d,2),7)) ;
      +  BinToWord(day(m.d)) ;
      +  BinToWord(hour(m.d)) ;
      +  BinToWord(minute(m.d)) ;
      +  BinToWord(sec(m.d)) ;
      +  BinToWord(0)

  case vartype(m.d) == 'X'  && null
    s =    BinToWord(0) ;
      +  BinToWord(0) ;
      +  BinToWord(0) ;
      +  BinToWord(0) ;
      +  BinToWord(0) ;
      +  BinToWord(0) ;
      +  BinToWord(0) ;
      +  BinToWord(0)
  otherwise
    assert FALSE
    s = ''
  endcase

  return m.s
endfunc
*---------------------------------------------------------------------------
function BinToWord(x)

  return bintoc(int(m.x), '2rs')

endfunc
*----------------------------------------------------------------------------
Commentaires
le 25/12/2008, eddymaue a écrit :
Salut Gregory, T_Date T_DateTime ne sont pas définis à la ligne case inlist(vartype(m.d), T_DATE, T_DATETIME )

a+ eddy

le 25/12/2008, Gregory Adam a écrit :
J'ai modifie le code - au debut - pour les gens qui ignorent son existence
#include "FoxPro.h"

le 25/12/2008, eddymaue a écrit :
bon traite moi d'ignorant ;0)
le 25/12/2008, eddymaue a écrit :
beau travail, ca fonctionne bien

Publicité

Les pubs en cours :


www.atoutfox.org - Site de la Communauté Francophone des Professionnels FoxPro - v3.4.0 - © 2004-2024.
Cette page est générée par un composant COM+ développé en Visual FoxPro 9.0-SP2-HF3