Vous êtes sur la page 1sur 8

Work|ng w|th A8A date and t|me data

types
O 8eprlnLs
lo tbls 5Al ltess book cboptet excetpt yooll floJ oo lottoJoctloo to 5Al A8Al Jote ooJ tlme
Joto types keoJ oo to floJ sttoteqles fot cteotloq colcolotloos osloq tbe bolltlo Jote ooJ tlme
Joto wotkloq wltb tlmestomps lo A8Al ooJ osloq tbe 5Al coleoJot
Advanced A8A operaLlons wlLh elemenLary daLa Lypes
Work|ng w|th A8A date and t|me data types
undersLandlng A8A programmlng blLs and byLes

22 Date and 1|me rocess|ng
Cnllne LransacLlon processlng (CL1) sysLems such as Lhe ones LhaL make up Lhe
SA 8uslness SulLe malnLaln qulLe a blL of LlmesenslLlve daLa so lL's lmporLanL
LhaL you undersLand how Lo work wlLh Lhe bullLln daLe and Llme Lypes provlded
ln A8A ln Lhe followlng subsecLlons we dlscuss Lhese Lypes and explaln how Lo
use Lhem Lo perform calculaLlons and converslons
1hls excerpL from A8A Cookbook rogrammlng 8eclpes for Lveryday SoluLlons by !ames
Woods ls reprlnLed here wlLh permlsslon from SA ress copyrlghL 2010 uownload aul
of Lhls chapLer

221 Understand|ng A8A Date and 1|me 1ypes
A8A provldes Lwo bullLln Lypes Lo work wlLh daLes and Llmes Lhe u (daLe) daLa
Lype and Lhe 1 (Llme) daLa Lype 8oLh of Lhese Lypes are flxedlengLh characLer Lypes
LhaL have Lhe form ????MMuu and PPMMSS respecLlvely ln addlLlon Lo Lhese bullLln
Lypes Lhe A8A ulcLlonary Lypes 1lMLS1AM and 1lMLS1AML are belng used more
and more ln many sLandard appllcaLlon Lables and so on Lo sLore a LlmesLamp ln Lhe
u1C formaL1 1able 22 shows Lhe baslc daLe and Llme Lypes avallable ln A8A
1be tetm u1c ls oo obbtevlotloo fot coosollJoteJ uolvetsol 1lme wblcb ls o tlme stooJotJ
boseJ oo tbe lotetootloool Atomlc 1lme stooJotJ u1c ls tooqbly epolvoleot to tbe Cteeowlcb
Meoo 1lme stooJotJ (ot CM1) wblcb tefets to tbe meoo solot tlme ot tbe koyol Obsetvototy lo
Cteeowlcb looJoo collectlvely tbese stooJotJs Jefloe o qlobol tlme stooJotJ tbot coo be oseJ
to coovett o qlveo tlme to locol tlme ooJ vlce vetso
Data Type Description
D A built-in Iixed-length date type oI the Iorm
YYYYMMDD. For
example, the value 20100913 represents the date
September
13, 2010.
T A built-in Iixed-length time type oI the Iorm
HHMMSS. For
example, the value 102305 represents the time
10:23:05 AM.
TIMESTAMP
(Type P
Length 8
No decimals)
An ABAP Dictionary type used to represent
short timestamps
in the Iorm YYYYMMDDhhmmss. For
example, the value
20100913102305 represents the date September
13, 2010 at
10:23:05 AM.
TIMESTAMPL
(Type P -
Length 11
Decimals 7)
An ABAP Dictionary type used to represent
long timestamps
in the Iorm YYYYMMDDhhmmssmmmuuun.
The additional digits
mmmuuun represent Iractions oI a second.
1able 22 A8A uaLe and 1lme uaLa 1ypes
222 Date and 1|me Ca|cu|at|ons
When you're worklng wlLh daLes you ofLen need Lo perform varlous calculaLlons
Lo compuLe Lhe dlfference beLween Lwo daLes make comparlsons or deLermlne
a valld daLe range As we menLloned ln SecLlon 221 undersLandlng A8A uaLe
and 1lme 1ypes Lhe bullLln daLe and Llme Lypes ln A8A are characLer Lypes noL
numerlc Lypes neverLheless Lhe A8A runLlme envlronmenL allows you Lo perform
baslc numerlc operaLlons on Lhese Lypes by lmpllclLly converLlng Lhem Lo
numerlc Lypes behlnd Lhe scenes
1he code excerpL shown ln LlsLlng 23 demonsLraLes how Lhese calculaLlons work
lnlLlally Lhe varlable lv_daLe ls asslgned Lhe value of Lhe currenL sysLem daLe (eg
Lhe sysLem fleld S?uA1uM) nexL we lncremenL LhaL daLe value by 30 ln Lerms of
a daLe calculaLlon ln A8A Lhls lmplles LhaL we're lncreaslng Lhe day componenL
of Lhe daLe ob[ecL by 30 days Pere noLe LhaL Lhe A8A runLlme envlronmenL ls
smarL enough Lo roll over Lhe daLe value whenever lL reaches Lhe end of a monLh
and so on ln oLher words you can rely on Lhe sysLem Lo ensure LhaL you don'L
calculaLe an lnvalld daLe value (eg 01/43/2011)
uA1A lv_daLe 1?L d
lv_daLe sydaLum
W8l1L / CurrenL uaLe lv_daLe MM/uu/????
lv_daLe lv_daLe + 30
W8l1L / luLure uaLe lv_daLe MM/uu/????
LlsLlng 23 erformlng uaLe CalculaLlons ln A8A
1lme calculaLlons ln A8A work very slmllarly Lo Lhe daLe calculaLlons shown ln
LlsLlng 23 WlLh Llme calculaLlons Lhe compuLaLlon ls based upon Lhe seconds
componenL of Lhe Llme ob[ecL 1he code ln LlsLlng 24 shows how we can lncremenL
Lhe currenL sysLem Llme by 90 seconds uslng baslc Llme arlLhmeLlc
uA1A lv_Llme 1?L L
lv_Llme syuzelL
W8l1L /(60) lv_Llme uSlnC Lul1 MASk
1he currenL Llme ls ______
lv_Llme lv_Llme + 90
W8l1L /(60) lv_Llme uSlnC Lul1 MASk
A mlnuLe and a half from now lL wlll be ______
LlsLlng 24 erformlng 1lme CalculaLlons ln A8A
ln addlLlon Lo Lyplcal numerlc calculaLlons you also have Lhe opLlon of worklng
wlLh daLe/Llme flelds uslng normal characLerbased semanLlcs lor lnsLance you
can use Lhe offseL/lengLh funcLlonallLy Lo lnlLlallze daLe or Llme componenLs 1he
code excerpL ln LlsLlng 23 demonsLraLes how you can ad[usL Lhe daLe 02/13/2003
Lo 01/13/2003 uslng offseL/lengLh semanLlcs
uA1A lv_daLe 1?L d vALuL 20030213
W8l1L / lv_daLe MM/uu/????
lv_daLe+4(2) 01
W8l1L / lv_daLe MM/uu/????
LlsLlng 23 ManlpulaLlng a uaLe uslng CffseL/LengLh luncLlonallLy
223 Work|ng w|th 1|mestamps
lf you've been worklng wlLh some of Lhe newer releases of Lhe producLs ln Lhe
SA 8uslness SulLe you may have encounLered cerLaln appllcaLlons LhaL use Lhe
1lMLS1AM or 1lMLS1AML daLa Lypes Lo sLore LlmesenslLlve daLa As you can see ln
1able 22 Lhese A8A ulcLlonary Lypes sLore LlmesLamps wlLh varylng degrees of
accuracy lnLeresLlngly Lhough Lhese Lypes aren'L bullLln Lypes llke u or 1 A8A
does provlde some naLlve supporL for Lhem ln Lhe form of a couple of bullLln sLaLemenLs
ln addlLlon SA also provldes a sysLem class called CL_A8A_1S1M whlch
can be used Lo slmpllfy Lhe process of worklng wlLh LlmesLamps We lnvesLlgaLe
Lhese feaLures ln Lhe followlng subsecLlons
?ou can reLrleve Lhe currenL sysLem Llme and sLore lL ln a LlmesLamp varlable uslng
Lhe CL1 1lML S1AM sLaLemenL whose synLax ls demonsLraLed ln LlsLlng 26 1he
CL1 1lML S1AM sLaLemenL sLores Lhe LlmesLamp ln a shorLhand or longhand formaL
dependlng upon Lhe Lype of Lhe LlmesLamp daLa ob[ecL used afLer Lhe llLLu addlLlon
1he LlmesLamp value ls encoded uslng Lhe u1C sLandard
uA1A lv_LsLamp_s 1?L LlmesLamp
lv_LsLamp_l 1?L LlmesLampl
CL1 1lML S1AM llLLu lv_LsLamp_s
W8l1L / ShorL 1lme SLamp lv_LsLamp_s
1lML ZCnL syzonlo
CL1 1lML S1AM llLLu lv_LsLamp_l
W8l1L / Long 1lme SLamp lv_LsLamp_l
1lML ZCnL syzonlo
LlsLlng 26 uslng Lhe CL1 1lML S1AM SLaLemenL
Looklng aL Lhe code excerpL ln LlsLlng 26 you can see LhaL we're dlsplaylng Lhe
LlmesLamp uslng Lhe 1lML ZCnL addlLlon of Lhe W8l1L sLaLemenL 1hls addlLlon formaLs
Lhe ouLpuL of Lhe LlmesLamp accordlng Lo Lhe rules for Lhe Llme zone speclfled
ln LlsLlng 26 we used Lhe sysLem fleld S?ZCnLC Lo dlsplay Lhe local Llme zone
conflgured ln Lhe user's preferences Powever we could have [usL as easlly used a
daLa ob[ecL of Lype 1lMLZCnL or even a hardcoded llLeral such as CS1
Time Zones
For a complete list oI time zones conIigured in the system, have a look at the contents
oI ABAP Dictionary Table TTZZ.

Convert|ng 1|mestamps
?ou can converL a LlmesLamp Lo a daLe/Llme daLa ob[ecL and vlce versa uslng Lhe
CCnvL81 sLaLemenL ln A8A LlsLlng 27 shows Lhe synLax used Lo converL a LlmesLamp
lnLo daLa ob[ecLs of Lype u and 1 1he 1lML ZCnL addlLlon ad[usLs Lhe u1C
daLe/Llme value wlLhln Lhe LlmesLamp ln accordance wlLh a parLlcular Llme zone
AddlLlonally Lhe opLlonal uA?LlCP1 SAvlnC 1lML addlLlon can be used Lo deLermlne
wheLher or noL Lhe LlmesLamp value happens Lo colnclde wlLh dayllghL savlngs
Llme lf lL does Lhe lv_dsL varlable has Lhe value x oLherwlse lL's blank
1hls feaLure can be helpful ln dlfferenLlaLlng beLween LlmesLamp values LhaL lle
wlLhln Lhe LranslLlonal perlod beLween summer Llme and wlnLer Llme2
CCnvL81 1lML S1AM lv_LsLamp 1lML ZCnL lv_Lzone
ln1C uA1L lv_daLe 1lML lv_Llme
uA?LlCP1 SAvlnC 1lML lv_dsL
LlsLlng 27 SynLax of CCnvL81 1lML S1AM SLaLemenL
LlsLlng 28 shows how Lhe CCnvL81 1lML S1AM sLaLemenL ls used Lo converL Lhe
currenL sysLem LlmesLamp Lo daLe/Llme daLa ob[ecLs uslng Lhe local Llme zone
1?LCCLS abap
uA1A lv_LsLamp 1?L LlmesLamp
lv_daLe 1?L d
lv_Llme 1?L L
lv_dsL 1?L abap_bool
CL1 1lML S1AM llLLu lv_LsLamp
CCnvL81 1lML S1AM lv_LsLamp 1lML ZCnL syzonlo
ln1C uA1L lv_daLe 1lML lv_Llme
uA?LlCP1 SAvlnC 1lML lv_dsL
W8l1L / 1odays daLe ls lv_daLe MM/uu/????
W8l1L /(60) lv_Llme uSlnC Lul1 MASk
1he currenL Llme ls ______
ll lv_dsL LC abap_Lrue
W8l1L / ln dayllghL savlngs Llme
LLSL
W8l1L / noL ln dayllghL savlngs Llme
Lnull
LlsLlng 28 ConverLlng 1lmesLamps Lo uaLe/1lme Cb[ecLs
1o creaLe a LlmesLamp uslng a daLe/Llme ob[ecL you can use Lhe synLax varlanL of
Lhe CCnvL81 sLaLemenL shown ln LlsLlng 29 1he daLe/Llme values are quallfled
uslng Lhe 1lML ZCnL addlLlon so LhaL Lhe approprlaLe offseLs can be applled as Lhe
u1C LlmesLamp ls generaLed
lot o complete llst of Joyllqbt sovloqs tlme toles bove o look ot tbe cooteots of tbe A8Al
ulctloooty toble 112uv
CCnvL81 uA1L lv_daLe
1lML lv_Llme uA?LlCP1 SAvlnC 1lML lv_dsL
ln1C 1lML S1AM lv_LsLamp 1lML ZCnL lv_Lzone
LlsLlng 29 SynLax of CCnvL81 uA1L SLaLemenL
1he code excerpL ln LlsLlng 210 shows how Lhe CCnvL81 uA1L sLaLemenL can be
used Lo generaLe a LlmesLamp ob[ecL from a daLe/Llme ob[ecL
1?LCCLS abap
uA1A lv_LsLamp 1?L LlmesLamp
lv_daLe 1?L d
lv_Llme 1?L L
lv_dsL 1?L abap_bool
lv_daLe sydaLum
lv_Llme syuzelL
CCnvL81 uA1L lv_daLe 1lML lv_Llme
ln1C 1lML S1AM lv_LsLamp 1lML ZCnL syzonlo
W8l1L / 1lme SLamp value lv_LsLamp 1lML ZCnL syzonlo
LlsLlng 210 CreaLlng a 1lmesLamp from a uaLe/1lme Cb[ecL
LABAPTSTMP
ADD( )
SUBTRAT( )
SUBTRATSES( )
TDADD( )
TDSUBTRAT( )
ISDOUBLEINTERVAL( )

SYSTEMTSTMPSYST2LO(
)

SYSTEMTSTMPLO2SYST(
)

SYSTEMTSTMPUT2SYST(
)
SYSTEMTSTMPSYST2UT(
)
TDNORMALIZE( )
NORMALIZE( )
llgure 24 uML Class ulagram for Class CL_A8A_1S1M
1|mestamp Cperat|ons Us|ng System C|ass CL_A8A_1S1M
unllke Lhe naLlve u and 1 Lypes Lhe A8A runLlme envlronmenL doesn'L have
bullLln funcLlonallLy Lo perform calculaLlons on LlmesLamps (eg add or subLracL
eLc) lnsLead SA provldes a sysLem class called CL_A8A_1S1M for Lhls purpose
llgure 24 conLalns a uML class dlagram LhaL shows Lhe publlcly avallable meLhods
provlded ln Lhls class As you would expecL Lhere are varlous forms of Auu() and Su818AC1()
meLhods Lo perform LlmesLamp calculaLlons ln addlLlon a serles of
converslon meLhods (eg S?S1LM1S1M_S?S12LCC() eLc) can be used Lo converL
a LlmesLamp Lo varlous Llme zones a 8oolean meLhod called lSuCu8LLln1L8vAL()
can be used Lo deLermlne lf a LlmesLamp ls ln dayllghL savlngs Llme and a couple
of meLhods can be used Lo normallze a LlmesLamp Pere normallzaLlon lmplles LhaL
an lnvalld Llme value such as 103060 would be ad[usLed Lo Lhe value 103100
ln uML class dlagram noLaLlon meLhods LhaL are underllned are deflned as class
meLhods Class meLhods can be lnvoked wlLhouL flrsL creaLlng an lnsLance of Lhe
class ln whlch Lhey are deflned as evldenced ln Lhe code excerpL shown ln LlsLlng
211 Pere we're uslng Lhe class meLhod Auu() Lo add 73 seconds Lo Lhe currenL
sysLem Llme
uA1A lv_LsLamp 1?L LlmesLamp
lv_daLe 1?L d
lv_Llme 1?L L
CL1 1lML S1AM llLLu lv_LsLamp
W8l1L / 1lme SLamp value lv_LsLamp 1lML ZCnL syzonlo
18?
CALL ML1PCu cl_abap_LsLmpadd
LxC81lnC
LsLmp lv_LsLamp
secs 73
8LCLlvlnC
r_LsLmp lv_LsLamp
CA1CP Cx_A8AML1L8_lnvALlu_8AnCL
CA1CP Cx_A8AML1L8_lnvALlu_1?L
Lnu18?
W8l1L / 1lme SLamp value lv_LsLamp 1lML ZCnL syzonlo
LlsLlng 211 Worklng wlLh 1lmesLamps uslng CL_A8A_1S1M
1he call slgnaLures of mosL of Lhe oLher meLhods ln class CL_A8A_1S1M are slmllar
Lo Lhe Auu() meLhod demonsLraLed ln LlsLlng 211 lor more deLalls concernlng Lhe
funcLlonallLy of parLlcular meLhods ln Lhls class see Lhe class/meLhod documenLaLlon
for Lhls class ln Lhe Class 8ullder (1ransacLlon SL24)

Vous aimerez peut-être aussi