github.com/simpleiot/simpleiot@v0.18.3/frontend/src/Utils/Iso8601.elm (about) 1 module Utils.Iso8601 exposing 2 ( toDateTimeString 3 , Mode(..) 4 , toString 5 ) 6 7 {-| Format a posix time to a ISO8601 String. 8 9 None of the generated Strings include timezone postfix. 10 11 12 # UTC strings 13 14 15 # Custom timezone 16 17 @docs toDateTimeString 18 19 20 ## Mode for different precission 21 22 @docs Mode 23 24 25 # Custom format helper 26 27 -} 28 29 -- this module is copied from https://github.com/f0i/iso8601/blob/1.1.1/src/Iso8601.elm 30 -- it has name conflicts with Richard F's module that exports the same namespace. 31 -- BSD 3-Clause license 32 33 import Time exposing (Month, Posix, Zone) 34 35 36 {-| The precission of the resulting String can be defined by the Mode parameter 37 38 The resulting string will have the following format 39 40 - Year: "YYYY" 41 - Month: "YYYY-MM" 42 - Day: "YYYY-MM-DD" 43 - Hour: "YYYY-MM-DDThh" 44 - Minute: "YYYY-MM-DDThh:mm" 45 - Second: "YYYY-MM-DDThh:mm:ss" 46 - Milli: "YYYY-MM-DDThh:mm:ss.sss" 47 - HourOnly: "hh" 48 - HourMinute: "hh:mm" 49 - HourSecond: "hh:mm:ss" 50 - HourMilli: "hh:mm:ss.sss" 51 52 -} 53 type Mode 54 = Day 55 | Second 56 | HourSecond 57 58 59 {-| Format a date time ("YYYY-MM-DDThh:mm:ss") 60 -} 61 toDateTimeString : Zone -> Posix -> String 62 toDateTimeString zone time = 63 time |> toString Second zone 64 65 66 {-| convert a positive integer into a string of at least two digits 67 -} 68 iToS2 : Int -> String 69 iToS2 i = 70 if i < 10 then 71 "0" ++ String.fromInt i 72 73 else 74 String.fromInt i 75 76 77 {-| Convert a Time.Month to a number string 78 -} 79 monthToS : Month -> String 80 monthToS month = 81 case month of 82 Time.Jan -> 83 "01" 84 85 Time.Feb -> 86 "02" 87 88 Time.Mar -> 89 "03" 90 91 Time.Apr -> 92 "04" 93 94 Time.May -> 95 "05" 96 97 Time.Jun -> 98 "06" 99 100 Time.Jul -> 101 "07" 102 103 Time.Aug -> 104 "08" 105 106 Time.Sep -> 107 "09" 108 109 Time.Oct -> 110 "10" 111 112 Time.Nov -> 113 "11" 114 115 Time.Dec -> 116 "12" 117 118 119 {-| Convert a posix time into a ISO8601 string 120 -} 121 toString : Mode -> Zone -> Posix -> String 122 toString mode zone time = 123 case mode of 124 Day -> 125 (time |> Time.toYear zone |> String.fromInt) 126 ++ "-" 127 ++ (time |> Time.toMonth zone |> monthToS) 128 ++ "-" 129 ++ (time |> Time.toDay zone |> iToS2) 130 131 HourSecond -> 132 (time |> Time.toHour zone |> iToS2) 133 ++ ":" 134 ++ (time |> Time.toMinute zone |> iToS2) 135 ++ ":" 136 ++ (time |> Time.toSecond zone |> iToS2) 137 138 Second -> 139 (time |> toString Day zone) 140 ++ "T" 141 ++ (time |> toString HourSecond zone)