├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bin ├── covertool └── rebar3 ├── rebar.config ├── src ├── strftimerl.app.src └── strftimerl.erl └── test └── strftimerl_tests.erl /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Erlang template 3 | .eunit 4 | deps 5 | *.o 6 | *.beam 7 | *.plt 8 | erl_crash.dump 9 | ebin 10 | rel/example_project 11 | .concrete/DEV_MODE 12 | .rebar 13 | _build 14 | TEST*.xml 15 | 16 | ### JetBrains template 17 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 18 | 19 | *.iml 20 | 21 | ## Directory-based project format: 22 | .idea/ 23 | # if you remove the above rule, at least ignore the following: 24 | 25 | # User-specific stuff: 26 | # .idea/workspace.xml 27 | # .idea/tasks.xml 28 | # .idea/dictionaries 29 | 30 | # Sensitive or high-churn files: 31 | # .idea/dataSources.ids 32 | # .idea/dataSources.xml 33 | # .idea/sqlDataSources.xml 34 | # .idea/dynamic.xml 35 | # .idea/uiDesigner.xml 36 | 37 | # Gradle: 38 | # .idea/gradle.xml 39 | # .idea/libraries 40 | 41 | # Mongo Explorer plugin: 42 | # .idea/mongoSettings.xml 43 | 44 | ## File-based project format: 45 | *.ipr 46 | *.iws 47 | 48 | ## Plugin-specific files: 49 | 50 | # IntelliJ 51 | /out/ 52 | 53 | # mpeltonen/sbt-idea plugin 54 | .idea_modules/ 55 | 56 | # JIRA plugin 57 | atlassian-ide-plugin.xml 58 | 59 | # Crashlytics plugin (for Android Studio and IntelliJ) 60 | com_crashlytics_export_strings.xml 61 | crashlytics.properties 62 | crashlytics-build.properties 63 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: erlang 3 | otp_release: 4 | - R16B03 5 | - 17.0 6 | - 17.1 7 | - 17.3 8 | - 17.4 9 | - 17.5 10 | - 18.0 11 | - 18.1 12 | - 18.2 13 | - 18.2.1 14 | before_install: 15 | - pip install --user codecov 16 | script: bin/rebar3 eunit 17 | after_success: 18 | - bin/covertool -cover _build/test/cover/eunit.coverdata -appname strftimerl -output cobertura.xml 19 | - codecov 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015-2016, Gavin M. Roy 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of strftimerl nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | strftimerl 2 | ========== 3 | Erlang implementation of strftime. This library implements most of the strftime format specifiers. It currently does not 4 | support the any of the locale related text specifiers (day of week name, month name, etc), ``%U``, ``%W``, 5 | timezone specifiers, or the locale preferred date specifiers (``%x``, ``%X``). 6 | 7 | [![Version](https://img.shields.io/hexpm/v/strftimerl.svg)](https://hex.pm/packages/strftimerl) [![Downloads](https://img.shields.io/hexpm/dt/strftimerl.svg)](https://hex.pm/packages/strftimerl) [![Build Status](https://travis-ci.org/gmr/strftimerl.svg?branch=master)](https://travis-ci.org/gmr/strftimerl) [![codecov.io](https://codecov.io/github/gmr/strftimerl/coverage.svg?branch=master)](https://codecov.io/github/gmr/strftimerl?branch=master) 8 | 9 | ### format/2 ### 10 | ```erlang 11 | format(Value, Datetime) -> string() 12 | ``` 13 | 14 | 15 | 16 | Format a date and time. Supported conversion specifications include: 17 | 18 | Specifier | Meaning 19 | -----------|--------- 20 | ``%C`` | The century number (year/100) as a 2-digit integer. 21 | ``%d`` | The day of the month as a decimal number (range 01 to 31). 22 | ``%D`` | Equivalent to ``%m/%d/%y``. 23 | ``%F`` | Equivalent to ``%Y-%m-%d``. 24 | ``%G`` | The ISO 8601 week-based year with century as a decimal number. 25 | ``%g`` | Like ``%G``, but without century, that is, with a 2-digit year (00-99). 26 | ``%H`` | The hour as a decimal number using a 24-hour clock (range 00 to 23). 27 | ``%I`` | The hour as a decimal number using a 12-hour clock (range 01 to 12). 28 | ``%j`` | The day of the year as a decimal number (range 001 to 366). 29 | ``%k`` | The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also ``%H``) 30 | ``%l`` | The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also ``%I``) 31 | ``%m`` | The month as a decimal number (range 01 to 12). 32 | ``%M`` | The minute as a decimal number (range 00 to 59). 33 | ``%n`` | A newline character. 34 | ``%p`` | Either "AM" or "PM" according to the given time value. Noon is treated as "PM" and midnight as "AM". 35 | ``%P`` | Like ``%p`` but in lowercase: "am" or "pm". 36 | ``%r`` | The time in a.m. or p.m. notation. This is equivalent to ``%I:%M:%S %p``. 37 | ``%R`` | The time in 24-hour notation (``%H:%M``). For a version including the seconds, see ``%T`` below. 38 | ``%s`` | The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). 39 | ``%S`` | The second as a decimal number (range 00 to 60). The range is up to 60 to allow for occasional leap seconds.
 40 | ``%t`` | A tab character. 41 | ``%T`` | The time in 24-hour notation (``%H:%M:%S``). 42 | ``%u`` | The day of the week as a decimal, range 1 to 7, Monday being 1. See also ``%w``. 43 | ``%V`` | The ISO 8601 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year. 44 | ``%w`` | The day of the week as a decimal, range 0 to 6, Sunday being 0. See also ``%u``. 45 | ``%y`` | The year as a decimal number without a century (range 00 to 99). 46 | ``%Y`` | The year as a decimal number including the century. 47 | 48 | #### Examples #### 49 | 50 | ```erlang 51 | Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] 52 | 53 | Eshell V6.4 (abort with ^G) 54 | 1> strftimerl:format("%Y-%m-%d %H:%M:%S", calendar:universal_time()). 55 | "2015-08-17 22:02:26" 56 | 2> strftimerl:format("%s", calendar:universal_time()). 57 | "1439848958" 58 | 3> strftimerl:format("%D", calendar:universal_time()). 59 | "08/17/2015" 60 | 4> io:format("~s~n", [strftimerl:format("%Y-%m-%d%n%I:%M %P", calendar:universal_time())]). 61 | 2015-08-17 62 | 10:04 pm 63 | ok 64 | ``` 65 | -------------------------------------------------------------------------------- /bin/covertool: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmr/strftimerl/6e1e1bca40392030e0967d0286885182d0266720/bin/covertool -------------------------------------------------------------------------------- /bin/rebar3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmr/strftimerl/6e1e1bca40392030e0967d0286885182d0266720/bin/rebar3 -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | %% -*- erlang -*- 2 | {cover_enabled, true}. 3 | {cover_opts, [verbose]}. 4 | {clean_files, ["*.eunit", "ebin/*.beam"]}. 5 | {dialyzer, [{plt_extra_apps, [kernel, stdlib]}]}. 6 | {erl_opts, [fail_on_warning]}. 7 | {eunit_exclude_deps, true}. 8 | {eunit_opts, [verbose, {skip_deps, true}]}. 9 | {lib_dirs,["deps"]}. 10 | {minimum_otp_vsn, "R16B03"}. 11 | -------------------------------------------------------------------------------- /src/strftimerl.app.src: -------------------------------------------------------------------------------- 1 | {application, strftimerl, [ 2 | {description, "strftime formatting in erlang"}, 3 | {vsn, "0.1.2"}, 4 | {licenses, ["BSD"]}, 5 | {links, [{"Github", "https://github.com/gmr/strftimerl"}]}, 6 | {maintainers, ["Gavin M. Roy"]}, 7 | {registered, []}, 8 | {applications, [ 9 | kernel, 10 | stdlib 11 | ]}, 12 | {env, []}, 13 | {modules, []} 14 | ]}. 15 | -------------------------------------------------------------------------------- /src/strftimerl.erl: -------------------------------------------------------------------------------- 1 | %% ============================================================================= 2 | %% @author Gavin M. Roy 3 | %% @copyright 2015-2016 4 | %% @end 5 | %% ============================================================================= 6 | -module(strftimerl). 7 | -author("gavinr"). 8 | 9 | %% API 10 | -export([format/2]). 11 | 12 | %% Export all for unit tests 13 | -ifdef(TEST). 14 | -compile(export_all). 15 | -endif. 16 | 17 | -define(CONVERSION_SPECIFICATIONS, ["%C", "%d", "%D", "%F", "%g", "%G", "%H", 18 | "%I", "%j", "%k", "%l", "%m", "%M", "%n", 19 | "%p", "%P", "%r", "%R", "%s", "%S", "%t", 20 | "%T", "%u", "%V", "%w", "%y", "%Y"]). 21 | 22 | -spec format(Value :: string(), Datetime :: calendar:datetime()) -> string(). 23 | %% @spec format(Value, Datetime) -> Result 24 | %% where 25 | %% Value = string() 26 | %% Datetime = datetime() 27 | %% Result = string() 28 | %% @end 29 | %% @doc Format a date and time. Supported conversion specifications include: 30 | %% 31 | %% 32 | %% 33 | %% 34 | %% 35 | %% 36 | %% 37 | %% 38 | %% 39 | %% 40 | %% 41 | %% 42 | %% 43 | %% 44 | %% 45 | %% 46 | %% 47 | %% 48 | %% 49 | %% 50 | %% 51 | %% 52 | %% 53 | %% 54 | %% 55 | %% 56 | %% 57 | %% 
 58 | %% 59 | %% 60 | %% 61 | %% 62 | %% 63 | %% 64 | %% 65 | %% 66 | %%
SpecifierMeaning
``%C''The century number (year/100) as a 2-digit integer.
``%d''The day of the month as a decimal number (range 01 to 31).
``%D''Equivalent to ``%m/%d/%y''.
``%F''Equivalent to ``%Y-%m-%d''.
``%G''The ISO 8601 week-based year with century as a decimal number.
``%g''Like ``%G'', but without century, that is, with a 2-digit year (00-99).
``%H''The hour as a decimal number using a 24-hour clock (range 00 to 23).
``%I''The hour as a decimal number using a 12-hour clock (range 01 to 12).
``%j''The day of the year as a decimal number (range 001 to 366).
``%k''The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also ``%H'')
``%l''The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also ``%I'')
``%m''The month as a decimal number (range 01 to 12).
``%M''The minute as a decimal number (range 00 to 59).
``%n''A newline character.
``%p''Either "AM" or "PM" according to the given time value. Noon is treated as "PM" and midnight as "AM".
``%P''Like %p but in lowercase: "am" or "pm".
``%r''The time in a.m. or p.m. notation. This is equivalent to ``%I:%M:%S %p''.
``%R''The time in 24-hour notation (``%H:%M''). For a version including the seconds, see ``%T'' below.
``%s''The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
``%S''The second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap seconds.)
``%t''A tab character.
``%T''The time in 24-hour notation (``%H:%M:%S'').
``%u''The day of the week as a decimal, range 1 to 7, Monday being 1. See also ``%w''.
``%V''The ISO 8601 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year.
``%w''The day of the week as a decimal, range 0 to 6, Sunday being 0. See also ``%u''.
``%y''The year as a decimal number without a century (range 00 to 99).
``%Y''The year as a decimal number including the century.
67 | %% 68 | %% @end 69 | format(Value, Datetime) -> 70 | format(?CONVERSION_SPECIFICATIONS, Datetime, Value). 71 | 72 | -spec format(Specifications :: [string()], Datetime :: calendar:datetime(), Value :: string()) -> string(). 73 | %% @private 74 | %% @spec format(Specifications, Datetime, Value) -> string() 75 | %% where 76 | %% Specifications = [string()] 77 | %% Value = string() 78 | %% Datetime = datetime() 79 | %% @doc Return a interpolated version of the path string. 80 | %% @end 81 | %% 82 | format([], _, Value) -> Value; 83 | 84 | format([H|T], Datetime, Value) when H == "%C" -> 85 | {{V, _, _}, _} = Datetime, 86 | format(T, Datetime, replace(H, Value, lists:sublist(as_string(V), 1, 2))); 87 | 88 | format([H|T], Datetime, Value) when H == "%d" -> 89 | {{_, _, D},_} = Datetime, 90 | V = lists:flatten(io_lib:format("~2..0B", [D])), 91 | format(T, Datetime, replace(H, Value, as_string(V))); 92 | 93 | format([H|T], Datetime, Value) when H == "%D" -> 94 | {{Y, M, D},_} = Datetime, 95 | V = lists:flatten(io_lib:format("~2..0B/~2..0B/~4..0B", [M, D, Y])), 96 | format(T, Datetime, replace(H, Value, as_string(V))); 97 | 98 | format([H|T], Datetime, Value) when H == "%F" -> 99 | {{Y, M, D},_} = Datetime, 100 | V = lists:flatten(io_lib:format("~4..0B-~2..0B-~2..0B", [Y, M, D])), 101 | format(T, Datetime, replace(H, Value, as_string(V))); 102 | 103 | format([H|T], Datetime, Value) when H == "%g" -> 104 | {Date,_} = Datetime, 105 | {Y, _} = calendar:iso_week_number(Date), 106 | format(T, Datetime, replace(H, Value, lists:nthtail(2, as_string(Y)))); 107 | 108 | format([H|T], Datetime, Value) when H == "%G" -> 109 | {Date,_} = Datetime, 110 | {Y, _} = calendar:iso_week_number(Date), 111 | format(T, Datetime, replace(H, Value, as_string(Y))); 112 | 113 | format([H|T], Datetime, Value) when H == "%H" -> 114 | {_,{V,_,_}} = Datetime, 115 | Hour = lists:flatten(io_lib:format("~2..0B", [V])), 116 | format(T, Datetime, replace(H, Value, Hour)); 117 | 118 | format([H|T], Datetime, Value) when H == "%I" -> 119 | {_,{V,_,_}} = Datetime, 120 | Hour = case V > 12 of 121 | true -> lists:flatten(io_lib:format("~2..0B", [V - 12])); 122 | false -> lists:flatten(io_lib:format("~2..0B", [V])) 123 | end, 124 | format(T, Datetime, replace(H, Value, Hour)); 125 | 126 | format([H|T], Datetime, Value) when H == "%j" -> 127 | {Date,_} = Datetime, 128 | {_, W} = calendar:iso_week_number(Date), 129 | V = ((W-1) * 7) + (calendar:day_of_the_week(Date) - 1), 130 | Hour = lists:flatten(io_lib:format("~3..0B", [V])), 131 | format(T, Datetime, replace(H, Value, Hour)); 132 | 133 | format([H|T], Datetime, Value) when H == "%k" -> 134 | {_,{V,_,_}} = Datetime, 135 | Hour = lists:flatten(io_lib:format("~2.. B", [V])), 136 | format(T, Datetime, replace(H, Value, Hour)); 137 | 138 | format([H|T], Datetime, Value) when H == "%l" -> 139 | {_,{V,_,_}} = Datetime, 140 | Hour = case V > 12 of 141 | true -> lists:flatten(io_lib:format("~2.. B", [V - 12])); 142 | false -> lists:flatten(io_lib:format("~2.. B", [V])) 143 | end, 144 | format(T, Datetime, replace(H, Value, Hour)); 145 | 146 | format([H|T], Datetime, Value) when H == "%m" -> 147 | {{_, M, _},_} = Datetime, 148 | V = lists:flatten(io_lib:format("~2..0B", [M])), 149 | format(T, Datetime, replace(H, Value, as_string(V))); 150 | 151 | format([H|T], Datetime, Value) when H == "%M" -> 152 | {_,{_, M, _}} = Datetime, 153 | V = lists:flatten(io_lib:format("~2..0B", [M])), 154 | format(T, Datetime, replace(H, Value, as_string(V))); 155 | 156 | format([H|T], Datetime, Value) when H == "%n" -> 157 | format(T, Datetime, replace(H, Value, "\n")); 158 | 159 | format([H|T], Datetime, Value) when H == "%p" -> 160 | {_,{V,_,_}} = Datetime, 161 | Hour = case V > 12 of 162 | true -> "PM"; 163 | false -> "AM" 164 | end, 165 | format(T, Datetime, replace(H, Value, Hour)); 166 | 167 | format([H|T], Datetime, Value) when H == "%P" -> 168 | {_,{V,_,_}} = Datetime, 169 | Hour = case V > 12 of 170 | true -> "pm"; 171 | false -> "am" 172 | end, 173 | format(T, Datetime, replace(H, Value, Hour)); 174 | 175 | format([H|T], Datetime, Value) when H == "%r" -> 176 | {_,{Hr, M, S}} = Datetime, 177 | {Hour, I} = case Hr > 12 of 178 | true -> {Hr - 12, "PM"}; 179 | false -> {Hr, "AM"} 180 | end, 181 | V = lists:flatten(io_lib:format("~2..0B:~2..0B:~2..0B ~s", [Hour, M, S, I])), 182 | format(T, Datetime, replace(H, Value, as_string(V))); 183 | 184 | format([H|T], Datetime, Value) when H == "%R" -> 185 | {_,{Hr, M, _}} = Datetime, 186 | V = lists:flatten(io_lib:format("~2..0B:~2..0B", [Hr, M])), 187 | format(T, Datetime, replace(H, Value, as_string(V))); 188 | 189 | format([H|T], Datetime, Value) when H == "%s" -> 190 | V = calendar:datetime_to_gregorian_seconds(Datetime) - 62167219200, 191 | format(T, Datetime, replace(H, Value, as_string(V))); 192 | 193 | format([H|T], Datetime, Value) when H == "%S" -> 194 | {_,{_, _, S}} = Datetime, 195 | V = lists:flatten(io_lib:format("~2..0B", [S])), 196 | format(T, Datetime, replace(H, Value, as_string(V))); 197 | 198 | format([H|T], Datetime, Value) when H == "%t" -> 199 | format(T, Datetime, replace(H, Value, "\t")); 200 | 201 | format([H|T], Datetime, Value) when H == "%T" -> 202 | {_,{Hr, M, S}} = Datetime, 203 | V = lists:flatten(io_lib:format("~2..0B:~2..0B:~2..0B", [Hr, M, S])), 204 | format(T, Datetime, replace(H, Value, as_string(V))); 205 | 206 | format([H|T], Datetime, Value) when H == "%u" -> 207 | {Date,_} = Datetime, 208 | V = calendar:day_of_the_week(Date), 209 | format(T, Datetime, replace(H, Value, as_string(V))); 210 | 211 | format([H|T], Datetime, Value) when H == "%V" -> 212 | {Date,_} = Datetime, 213 | {_, W} = calendar:iso_week_number(Date), 214 | V = lists:flatten(io_lib:format("~2..0B", [W])), 215 | format(T, Datetime, replace(H, Value, V)); 216 | 217 | format([H|T], Datetime, Value) when H == "%w" -> 218 | {Date,_} = Datetime, 219 | V = case calendar:day_of_the_week(Date) of 220 | 7 -> 0; 221 | O -> O 222 | end, 223 | format(T, Datetime, replace(H, Value, as_string(V))); 224 | 225 | format([H|T], Datetime, Value) when H == "%y" -> 226 | {{Y, _, _},_} = Datetime, 227 | format(T, Datetime, replace(H, Value, as_string(lists:nthtail(2, as_string(Y))))); 228 | 229 | format([H|T], Datetime, Value) when H == "%Y" -> 230 | {{V, _, _},_} = Datetime, 231 | format(T, Datetime, replace(H, Value, as_string(V))); 232 | 233 | format([H|T], Datetime, Value) -> 234 | error_logger:warning_msg("Unsupported specifier ~s", [H]), 235 | format(T, Datetime, Value). 236 | 237 | 238 | -spec as_string(Needle :: atom() | integer() | binary() | list()) -> string(). 239 | %% @private 240 | %% @spec as_string(Value) ->string() 241 | %% where Value = list()|integer() 242 | %% @doc Return the value as a list 243 | %% @end 244 | %% 245 | as_string([]) -> undefined; 246 | as_string(Value) when is_atom(Value) =:= true -> atom_to_list(Value); 247 | as_string(Value) when is_binary(Value) =:= true -> binary_to_list(Value); 248 | as_string(Value) when is_integer(Value) =:= true -> integer_to_list(Value); 249 | as_string(Value) when is_list(Value) =:= true -> Value; 250 | as_string(Value) -> Value. 251 | 252 | 253 | -spec replace(Needle :: string(), Haystack :: string(), Value :: string()) -> string(). 254 | %% @private 255 | %% @doc Replace Needle with Value in Haystack 256 | %% @spec replace(Needle, Haystack, Value) -> string() 257 | %% where 258 | %% Needle = string() 259 | %% Haystack = string() 260 | %% Value = string() 261 | %% @end 262 | %% 263 | replace(Needle, Haystack, Value) -> 264 | case string:str(Haystack, Needle) of 265 | 0 -> Haystack; 266 | P -> 267 | New = string:left(Haystack, P - 1) ++ Value ++ string:right(Haystack, length(Haystack) - P - 1), 268 | replace(Needle, New, Value) 269 | end. 270 | -------------------------------------------------------------------------------- /test/strftimerl_tests.erl: -------------------------------------------------------------------------------- 1 | -module(strftimerl_tests). 2 | 3 | -include_lib("eunit/include/eunit.hrl"). 4 | 5 | as_string_binary_test() -> 6 | ?assertEqual("foo", strftimerl:as_string(<<"foo">>)). 7 | 8 | as_string_list_test() -> 9 | ?assertEqual("bar", strftimerl:as_string("bar")). 10 | 11 | as_string_integer_test() -> 12 | ?assertEqual("42", strftimerl:as_string(42)). 13 | 14 | format_C_test() -> 15 | Datetime = {{2002,12,20},{7,46,0}}, 16 | ?assertEqual(":20:", strftimerl:format(":%C:", Datetime)). 17 | 18 | format_d_test() -> 19 | Datetime = {{2002,12,20},{7,46,0}}, 20 | ?assertEqual(":20:", strftimerl:format(":%d:", Datetime)). 21 | 22 | format_d_single_digit_test() -> 23 | Datetime = {{2002,12,2},{7,46,0}}, 24 | ?assertEqual(":02:", strftimerl:format(":%d:", Datetime)). 25 | 26 | format_D_test() -> 27 | Datetime = {{2002,12,20},{7,46,0}}, 28 | ?assertEqual(":12/20/2002:", strftimerl:format(":%D:", Datetime)). 29 | 30 | format_F_test() -> 31 | Datetime = {{2002,12,20},{7,46,0}}, 32 | ?assertEqual(":2002-12-20:", strftimerl:format(":%F:", Datetime)). 33 | 34 | format_g_test() -> 35 | Datetime = {{2002,12,20},{7,46,0}}, 36 | ?assertEqual(":02:", strftimerl:format(":%g:", Datetime)). 37 | 38 | format_G_test() -> 39 | Datetime = {{2002,12,20},{7,46,0}}, 40 | ?assertEqual(":2002:", strftimerl:format(":%G:", Datetime)). 41 | 42 | format_H_test() -> 43 | Datetime = {{2002,12,20},{7,46,0}}, 44 | ?assertEqual(":07:", strftimerl:format(":%H:", Datetime)). 45 | 46 | format_H17_test() -> 47 | Datetime = {{2002,12,20},{17,46,0}}, 48 | ?assertEqual(":17:", strftimerl:format(":%H:", Datetime)). 49 | 50 | format_I_test() -> 51 | Datetime = {{2002,12,20},{17,46,0}}, 52 | ?assertEqual(":05:", strftimerl:format(":%I:", Datetime)). 53 | 54 | format_j_test() -> 55 | Datetime = {{2002,12,20},{17,46,0}}, 56 | ?assertEqual(":354:", strftimerl:format(":%j:", Datetime)). 57 | 58 | format_j2_test() -> 59 | Datetime = {{2002,9,6},{17,46,0}}, 60 | ?assertEqual(":249:", strftimerl:format(":%j:", Datetime)). 61 | 62 | format_k_test() -> 63 | Datetime = {{2002,12,20},{7,46,0}}, 64 | ?assertEqual(": 7:", strftimerl:format(":%k:", Datetime)). 65 | 66 | format_k17_test() -> 67 | Datetime = {{2002,12,20},{17,46,0}}, 68 | ?assertEqual(":17:", strftimerl:format(":%k:", Datetime)). 69 | 70 | format_l_test() -> 71 | Datetime = {{2002,12,20},{17,46,0}}, 72 | ?assertEqual(": 5:", strftimerl:format(":%l:", Datetime)). 73 | 74 | format_m_test() -> 75 | Datetime = {{2002,12,20},{17,46,0}}, 76 | ?assertEqual(":12:", strftimerl:format(":%m:", Datetime)). 77 | 78 | format_M_test() -> 79 | Datetime = {{2002,12,20},{17,46,0}}, 80 | ?assertEqual(":46:", strftimerl:format(":%M:", Datetime)). 81 | 82 | format_M_single_digit_test() -> 83 | Datetime = {{2002,12,20},{17,4,0}}, 84 | ?assertEqual(":04:", strftimerl:format(":%M:", Datetime)). 85 | 86 | format_n_test() -> 87 | Datetime = {{2002,12,20},{17,46,0}}, 88 | ?assertEqual(":\n:", strftimerl:format(":%n:", Datetime)). 89 | 90 | format_p_am_test() -> 91 | Datetime = {{2002,12,20},{7,46,0}}, 92 | ?assertEqual(":AM:", strftimerl:format(":%p:", Datetime)). 93 | 94 | format_p_pm_test() -> 95 | Datetime = {{2002,12,20},{17,46,0}}, 96 | ?assertEqual(":PM:", strftimerl:format(":%p:", Datetime)). 97 | 98 | format_P_am_test() -> 99 | Datetime = {{2002,12,20},{7,46,0}}, 100 | ?assertEqual(":am:", strftimerl:format(":%P:", Datetime)). 101 | 102 | format_P_pm_test() -> 103 | Datetime = {{2002,12,20},{17,46,0}}, 104 | ?assertEqual(":pm:", strftimerl:format(":%P:", Datetime)). 105 | 106 | format_r_am_test() -> 107 | Datetime = {{2002,12,20},{17,46,10}}, 108 | ?assertEqual(":05:46:10 PM:", strftimerl:format(":%r:", Datetime)). 109 | 110 | format_r_pm_test() -> 111 | Datetime = {{2002,12,20},{7,46,10}}, 112 | ?assertEqual(":07:46:10 AM:", strftimerl:format(":%r:", Datetime)). 113 | 114 | format_R_am_test() -> 115 | Datetime = {{2002,12,20},{7,46,10}}, 116 | ?assertEqual(":07:46:", strftimerl:format(":%R:", Datetime)). 117 | 118 | format_R_pm_test() -> 119 | Datetime = {{2002,12,20},{17,46,10}}, 120 | ?assertEqual(":17:46:", strftimerl:format(":%R:", Datetime)). 121 | 122 | format_s_test() -> 123 | Datetime = {{2002,12,20},{7,46,10}}, 124 | ?assertEqual(":1040370370:", strftimerl:format(":%s:", Datetime)). 125 | 126 | format_S_test() -> 127 | Datetime = {{2002,12,20},{17,46,10}}, 128 | ?assertEqual(":10:", strftimerl:format(":%S:", Datetime)). 129 | 130 | format_S_single_digit_test() -> 131 | Datetime = {{2002,12,20},{17,46,5}}, 132 | ?assertEqual(":05:", strftimerl:format(":%S:", Datetime)). 133 | 134 | format_t_test() -> 135 | Datetime = {{2002,12,20},{17,46,0}}, 136 | ?assertEqual(":\t:", strftimerl:format(":%t:", Datetime)). 137 | 138 | format_T_test() -> 139 | Datetime = {{2002,12,20},{17,46,0}}, 140 | ?assertEqual(":17:46:00:", strftimerl:format(":%T:", Datetime)). 141 | 142 | format_T_single_digit_test() -> 143 | Datetime = {{2002,12,20},{7,46,0}}, 144 | ?assertEqual(":07:46:00:", strftimerl:format(":%T:", Datetime)). 145 | 146 | format_u_test() -> 147 | Datetime = {{2002,12,20},{17,46,0}}, 148 | ?assertEqual(":5:", strftimerl:format(":%u:", Datetime)). 149 | 150 | format_V_test() -> 151 | Datetime = {{2002,12,15},{17,46,0}}, 152 | ?assertEqual(":50:", strftimerl:format(":%V:", Datetime)). 153 | 154 | format_w_test() -> 155 | Datetime = {{2002,12,15},{17,46,0}}, 156 | ?assertEqual(":0:", strftimerl:format(":%w:", Datetime)). 157 | 158 | format_y_test() -> 159 | Datetime = {{2002,12,15},{17,46,0}}, 160 | ?assertEqual(":02:", strftimerl:format(":%y:", Datetime)). 161 | 162 | format_Y_test() -> 163 | Datetime = {{2002,12,15},{17,46,0}}, 164 | ?assertEqual(":2002:", strftimerl:format(":%Y:", Datetime)). 165 | 166 | replace_test() -> 167 | ?assertEqual("2002 foo 2002bar2002.", strftimerl:replace("%y", "%y foo %ybar%y.", "2002")). 168 | --------------------------------------------------------------------------------