# Storing and parsing Dates.CompoundPeriod?

**URL:** https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295
**Category:** General Usage
**Tags:** dates
**Created:** [October 26, 2022, 11:55am UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295 "2022-10-26T11:55:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)
#### Post date: [October 26, 2022, 11:55am UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295/1 "2022-10-26T11:55:53Z")

</div>

Hi,  
In a small application I need to store and do simple arithmetics on durations (estimates and effort).  
So far the best option I have found seem to be Dates.CompoundPeriod, which works fine for now, providing addition out of the box and division of my own implementation (umm a bit of local type piracy I guess…)  
So far I get by with storing my data (tree) with JLD, but wanting to store the data in a database eg SQLite is not unreasonable here, which brings me to the question.  
What is the best way of storing Dates.CompoundPeriod data, and in particular in a database?  
String seems like the obvious choice as it is supported by any database, but then I have not found a supported way to parse a string back to a Dates.CompoundPeriod…  
I did find [https://discourse.julialang.org/t/parsing-string-to-dates-compoundperiod/25484/7](https://discourse.julialang.org/t/parsing-string-to-dates-compoundperiod/25484/7) but that is not really about trying to parse a Dates.CompundPeriod in string representation back to the actual data type.  
Or is there simply a more suitable way way to represent a duration than Dates.CompoundPeriod in Julia?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 26, 2022, 1:37pm UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295/2 "2022-10-26T13:37:51Z")

</div>

Not sure if the approach below makes sense, but fwiw:

```julia
using Dates

# CompoundPeriod
cp1 = Hour(25) + Second(62) + Microsecond(10)

# Output as 2 strings
v1 = string.(Dates.value.(cp1.periods))
s1 = string.(typeof.(cp1.periods))

# Parse strings
cp2 = sum([eval(Meta.parse("$s($v)")) for (v,s) in zip(v1,s1)])

cp2 == cp1 # true

```

---

<div class="post-metadata">

### Author: ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)
#### Post date: [October 26, 2022, 2:21pm UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295/3 "2022-10-26T14:21:23Z")

</div>

Not quite, as the string representation in the example is  
`"25 hours, 62 seconds, 10 microseconds"`  
and thus that is what needs to be parsed. Can’t directly get the “component” Periods from that.  
But the Meta.parse stuff was helpful, should be possible to use a regexp and get essentially the same building blocks that you picked directly from cp1.periods. Thx!

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 26, 2022, 2:50pm UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295/4 "2022-10-26T14:50:27Z")

</div>

OK.  
One option without regexp:

```julia
using Dates

# CompoundPeriod
cp1 = Hour(25) + Second(62) + Microsecond(10)
str1 = string(cp1)

# Parse string
x = split.(split(str1, ','))
v1 = first.(x)
s1 = chop.(uppercasefirst.(last.(x)))
cp2 = sum([eval(Meta.parse("$s($v)")) for (v,s) in zip(v1,s1)])

cp2 == cp1 # true

```

---

<div class="post-metadata">

### Author: ![chris-b1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-b1/32/14165_2.png) [@chris-b1](https://discourse.julialang.org/u/chris-b1)
#### Post date: [October 26, 2022, 2:51pm UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295/5 "2022-10-26T14:51:16Z")

</div>

You could use the ISO 8601 duration format - though I don’t believe an existing parser for julia exists. So in that example it would be `"PT25H62.000010"`. Postgres has a native datatype that can store this format too

> **[ISO 8601 | Durations](https://en.wikipedia.org/wiki/ISO_8601#Durations)**
>
> Durations define the amount of intervening time in a time interval and are represented by the format P\[n\]Y\[n\]M\[n\]DT\[n\]H\[n\]M\[n\]S or P\[n\]W as shown on the aside. In these representations, the \[n\] is replaced by the value for each of the date and time elements that follow the \[n\]. Leading zeros are not required, but the maximum number of digits for each element should be agreed to by the communicating parties. The capital letters P, Y, M, W, D, T, H, M, and S are designators for each of the date and...

> **[8.5. Date/Time Types](https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT)**
>
> 8.5. Date/Time Types # 8.5.1. Date/Time Input 8.5.2. Date/Time Output 8.5.3. Time Zones 8.5.4. Interval Input 8.5.5. Interval Output PostgreSQL supports …

---

<div class="post-metadata">

### Author: ![hacklint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hacklint/32/25568_2.png) [@hacklint](https://discourse.julialang.org/u/hacklint)
#### Post date: [October 26, 2022, 5:34pm UTC](https://discourse.julialang.org/t/storing-and-parsing-dates-compoundperiod/89295/6 "2022-10-26T17:34:59Z")

</div>

Really neat!
