github.com/simpleiot/simpleiot@v0.18.3/frontend/tests/UtilsTime.elm (about)

     1  module UtilsTime exposing (local, schedule, utc)
     2  
     3  import Expect
     4  import Test exposing (..)
     5  import Utils.Time exposing (scheduleToLocal, scheduleToUTC, toLocal, toUTC)
     6  
     7  
     8  local : Test
     9  local =
    10      describe "test toLocal"
    11          [ test "5:00" <|
    12              \_ ->
    13                  Expect.equal (toLocal -240 "5:00") "01:00"
    14          , test "1:00" <|
    15              \_ ->
    16                  Expect.equal (toLocal -240 "1:00") "21:00"
    17          , test "5:00+4" <|
    18              \_ ->
    19                  Expect.equal (toLocal 240 "5:00") "09:00"
    20          , test "1:00+4" <|
    21              \_ ->
    22                  Expect.equal (toLocal 240 "1:00") "05:00"
    23          ]
    24  
    25  
    26  utc : Test
    27  utc =
    28      describe "test toUTC"
    29          [ test "5:00" <|
    30              \_ ->
    31                  Expect.equal (toUTC -240 "1:00") "05:00"
    32          , test "22:00" <|
    33              \_ ->
    34                  Expect.equal (toUTC -240 "22:00") "02:00"
    35          , test "5:00+4" <|
    36              \_ ->
    37                  Expect.equal (toUTC 240 "1:00") "21:00"
    38          , test "22:00+4" <|
    39              \_ ->
    40                  Expect.equal (toUTC 240 "22:00") "18:00"
    41          ]
    42  
    43  
    44  schedule : Test
    45  schedule =
    46      describe "schedule tests"
    47          [ test "toLocal no weekday change" <|
    48              \_ ->
    49                  let
    50                      sUTC =
    51                          { startTime = "05:00"
    52                          , endTime = "08:00"
    53                          , weekdays = [ 2, 3 ]
    54                          , dates = []
    55                          , dateCount = 0
    56                          }
    57  
    58                      sExp =
    59                          { startTime = "01:00"
    60                          , endTime = "04:00"
    61                          , weekdays = [ 2, 3 ]
    62                          , dates = []
    63                          , dateCount = 0
    64                          }
    65                  in
    66                  Expect.equal sExp <| scheduleToLocal -240 sUTC
    67          , test "toLocal with weekday change" <|
    68              \_ ->
    69                  let
    70                      sUTC =
    71                          { startTime = "02:00"
    72                          , endTime = "08:00"
    73                          , weekdays = [ 0, 2, 3 ]
    74                          , dates = []
    75                          , dateCount = 0
    76                          }
    77  
    78                      sExp =
    79                          { startTime = "22:00"
    80                          , endTime = "04:00"
    81                          , weekdays = [ 1, 2, 6 ]
    82                          , dates = []
    83                          , dateCount = 0
    84                          }
    85                  in
    86                  Expect.equal sExp <| scheduleToLocal -240 sUTC
    87          , test "toLocal with weekday change pos offset" <|
    88              \_ ->
    89                  let
    90                      sUTC =
    91                          { startTime = "22:00"
    92                          , endTime = "02:00"
    93                          , weekdays = [ 2, 3, 6 ]
    94                          , dates = []
    95                          , dateCount = 0
    96                          }
    97  
    98                      sExp =
    99                          { startTime = "02:00"
   100                          , endTime = "06:00"
   101                          , weekdays = [ 0, 3, 4 ]
   102                          , dates = []
   103                          , dateCount = 0
   104                          }
   105                  in
   106                  Expect.equal sExp <| scheduleToLocal 240 sUTC
   107          , test "toLocal with no date change" <|
   108              \_ ->
   109                  let
   110                      sUTC =
   111                          { startTime = "10:00"
   112                          , endTime = "12:00"
   113                          , weekdays = []
   114                          , dates = [ "2023-01-01" ]
   115                          , dateCount = 1
   116                          }
   117  
   118                      sExp =
   119                          { startTime = "06:00"
   120                          , endTime = "08:00"
   121                          , weekdays = []
   122                          , dates = [ "2023-01-01" ]
   123                          , dateCount = 1
   124                          }
   125                  in
   126                  Expect.equal sExp <| scheduleToLocal -240 sUTC
   127          , test "toLocal with date change" <|
   128              \_ ->
   129                  let
   130                      sUTC =
   131                          { startTime = "2:00"
   132                          , endTime = "12:00"
   133                          , weekdays = []
   134                          , dates = [ "2023-01-01" ]
   135                          , dateCount = 1
   136                          }
   137  
   138                      sExp =
   139                          { startTime = "22:00"
   140                          , endTime = "08:00"
   141                          , weekdays = []
   142                          , dates = [ "2022-12-31" ]
   143                          , dateCount = 1
   144                          }
   145                  in
   146                  Expect.equal sExp <| scheduleToLocal -240 sUTC
   147          , test "toUTC with weekday change" <|
   148              \_ ->
   149                  let
   150                      sLocal =
   151                          { startTime = "22:00"
   152                          , endTime = "02:00"
   153                          , weekdays = [ 1, 2, 6 ]
   154                          , dates = []
   155                          , dateCount = 0
   156                          }
   157  
   158                      sExp =
   159                          { startTime = "02:00"
   160                          , endTime = "06:00"
   161                          , weekdays = [ 0, 2, 3 ]
   162                          , dates = []
   163                          , dateCount = 0
   164                          }
   165                  in
   166                  Expect.equal sExp <| scheduleToUTC -240 sLocal
   167          , test "toUTC with weekday change pos offset" <|
   168              \_ ->
   169                  let
   170                      sLocal =
   171                          { startTime = "02:00"
   172                          , endTime = "06:00"
   173                          , weekdays = [ 0, 3, 4 ]
   174                          , dates = []
   175                          , dateCount = 0
   176                          }
   177  
   178                      sExp =
   179                          { startTime = "22:00"
   180                          , endTime = "02:00"
   181                          , weekdays = [ 2, 3, 6 ]
   182                          , dates = []
   183                          , dateCount = 0
   184                          }
   185                  in
   186                  Expect.equal sExp <| scheduleToUTC 240 sLocal
   187          , test "toUTC with no date change" <|
   188              \_ ->
   189                  let
   190                      sLocal =
   191                          { startTime = "02:00"
   192                          , endTime = "06:00"
   193                          , weekdays = []
   194                          , dates = [ "2022-12-31" ]
   195                          , dateCount = 1
   196                          }
   197  
   198                      sExp =
   199                          { startTime = "06:00"
   200                          , endTime = "10:00"
   201                          , weekdays = []
   202                          , dates = [ "2022-12-31" ]
   203                          , dateCount = 1
   204                          }
   205                  in
   206                  Expect.equal sExp <| scheduleToUTC -240 sLocal
   207          , test "toUTC with with date change" <|
   208              \_ ->
   209                  let
   210                      sLocal =
   211                          { startTime = "22:00"
   212                          , endTime = "23:00"
   213                          , weekdays = []
   214                          , dates = [ "2022-12-31" ]
   215                          , dateCount = 1
   216                          }
   217  
   218                      sExp =
   219                          { startTime = "02:00"
   220                          , endTime = "03:00"
   221                          , weekdays = []
   222                          , dates = [ "2023-01-01" ]
   223                          , dateCount = 1
   224                          }
   225                  in
   226                  Expect.equal sExp <| scheduleToUTC -240 sLocal
   227          ]