This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Date.TimeStamp

Utility functions used by Date.Op but may be of some use.

Utils

fromMonth : Date.Month -> Int

Converts a Month used in Date to an Int.

fromWeekDay : Date.Day -> Int

Converts a weekday used in Date to an Int. The range is 0 to 6 and starts with Monday.

Convert

toIsoDate : TimeStamp -> String

Used by to toDate to convert to a date.

toDate:TimeStamp -> Date.Date

Convert a TimeStamp to a Date.

Build TimeStamp

type alias TimeStamp = { year : Int , month : Int , day : Int , hour : Int , minute : Int , second : Int , millisecond : Int }

This type is used to hold the information gathered by the parser and can be easily converted to a Date.

fromDate : Date.Date -> TimeStamp

Creates a TimeStamp from a date.

fromFragment : TimeStampFragment -> TimeStamp

Creates a TimeStamp from a TimeStampFragment. If the fragment does not describe an aspect the information from stdTimeStamp is used instead.

stdTimeStamp : TimeStamp

A standard TimeStamp used to fill empty aspects of a TimeStampFragment at conversion.

fromFragmentWithTimeStamp : TimeStamp -> TimeStampFragment -> TimeStamp

Creates a TimeStamp from a TimeStampFragment. If the fragment does not describe an aspect the information from the provided TimeStamp is used instead.

Build TimeFragment

type alias TimeStampFragment = { year : Maybe Int , month : Maybe Int , day : Maybe Int , hour : Maybe Int , minute : Maybe Int , second : Maybe Int , millisecond : Maybe Int }

This type is used to describe zero or more aspects of a TimeStamp.

union : TimeStampFragment -> TimeStampFragment -> TimeStampFragment

Combines two fragments. Values from the base(first argument) are given precedence.

emptyTimeStampFragment : TimeStampFragment

Creates a empty fragment

module Date.TimeStamp(..) where

{-|
Utility functions used by Date.Op but may be of some use.

# Utils
@docs fromMonth, fromWeekDay

# Convert
@docs toIsoDate, toDate

# Build TimeStamp
@docs TimeStamp, fromDate, fromFragment, stdTimeStamp, fromFragmentWithTimeStamp

# Build TimeFragment
@docs TimeStampFragment, union, emptyTimeStampFragment

--}

import Date
import String
import Result
import Maybe exposing (oneOf, withDefault)

import Native.DateOp

{-| This type is used to hold the information gathered by the parser and can be easily converted to a Date.
--}
type alias TimeStamp =
    { year : Int
    , month : Int
    , day : Int
    , hour : Int
    , minute : Int
    , second : Int
    , millisecond : Int
    }

{-| This type is used to describe zero or more aspects of a TimeStamp.
--}
type alias TimeStampFragment =
    { year : Maybe Int
    , month : Maybe Int
    , day : Maybe Int
    , hour : Maybe Int
    , minute : Maybe Int
    , second : Maybe Int
    , millisecond : Maybe Int
    }

{-| A standard TimeStamp used to fill empty aspects of a TimeStampFragment at conversion.
--}
stdTimeStamp : TimeStamp
stdTimeStamp = TimeStamp 1970 1 1 0 0 0 0

emptyTimeStamp : TimeStamp
emptyTimeStamp = TimeStamp 0 0 0 0 0 0 0

{-| Creates a empty fragment
--}
emptyTimeStampFragment : TimeStampFragment
emptyTimeStampFragment = TimeStampFragment Nothing Nothing Nothing Nothing Nothing Nothing Nothing 

{-| Creates a TimeStamp from a TimeStampFragment. If the fragment does not 
describe an aspect the information from the provided TimeStamp is used instead.
--}
fromFragmentWithTimeStamp : TimeStamp -> TimeStampFragment -> TimeStamp
fromFragmentWithTimeStamp std tsf =
    { year = withDefault std.year tsf.year
    , month = withDefault std.month tsf.month
    , day = withDefault std.day tsf.day
    , hour = withDefault std.hour tsf.hour
    , minute = withDefault std.minute tsf.minute
    , second = withDefault std.second tsf.second
    , millisecond = withDefault std.millisecond tsf.millisecond
    }
    
{-| Creates a TimeStamp from a TimeStampFragment. If the fragment does not 
describe an aspect the information from stdTimeStamp is used instead.
--}
fromFragment : TimeStampFragment -> TimeStamp
fromFragment tsf = fromFragmentWithTimeStamp stdTimeStamp tsf

{-| Creates a TimeStamp from a date.
--}
fromDate : Date.Date -> TimeStamp
fromDate d =
    { year = Date.year d
    , month = Date.month d |> fromMonth
    , day = Date.day d
    , hour = Date.hour d
    , minute = Date.minute d
    , second = Date.second d
    , millisecond = Date.millisecond d
    }

{-| Used by to toDate to convert to a date.
--}
toIsoDate : TimeStamp -> String
toIsoDate ts =
    toString ts.year ++ "-" ++
    ( toString ts.month |>String.padLeft 2 '0' ) ++ "-" ++
    ( toString ts.day |>String.padLeft 2 '0' ) ++ "T" ++
    ( toString ts.hour |>String.padLeft 2 '0' ) ++ ":" ++
    ( toString ts.minute |>String.padLeft 2 '0' ) ++":" ++
    ( toString ts.second |> String.padLeft 2 '0')

{-| Convert a TimeStamp to a Date.
--}
toDate:TimeStamp -> Date.Date
toDate ts = Native.DateOp.fromMoment ts.year ts.month ts.day ts.hour ts.minute ts.second ts.millisecond

{-| Converts a Month used in Date to an Int.
--}
fromMonth : Date.Month -> Int
fromMonth m = case m of
    Date.Jan -> 1
    Date.Feb -> 2
    Date.Mar -> 3
    Date.Apr -> 4
    Date.May -> 5
    Date.Jun -> 6
    Date.Jul -> 7
    Date.Aug -> 8
    Date.Sep -> 9
    Date.Oct -> 10
    Date.Nov -> 11
    Date.Dec -> 12

{-| Converts a weekday used in Date to an Int. The range is 0 to 6 and starts with Monday.
--}
fromWeekDay : Date.Day -> Int
fromWeekDay d=case d of
    Date.Mon ->0
    Date.Tue ->1
    Date.Wed ->2
    Date.Thu ->3
    Date.Fri ->4
    Date.Sat ->5
    Date.Sun ->6

alter : (Int -> Int -> Int) -> TimeStamp -> TimeStamp -> TimeStamp
alter op ts1 ts2 = TimeStamp
    (op ts1.year ts2.year)
    (op ts1.month ts2.month)
    (op ts1.day ts2.day)
    (op ts1.hour ts2.hour)
    (op ts1.minute ts2.minute)
    (op ts1.second ts2.second)
    (op ts1.millisecond ts2.millisecond)
    
{-| Combines two fragments. Values from the base(first argument) are given precedence.
--}
union : TimeStampFragment -> TimeStampFragment -> TimeStampFragment
union base extend = TimeStampFragment
    ( oneOf [base.year, extend.year] )
    ( oneOf [base.month, extend.month] )
    ( oneOf [base.day, extend.day] )
    ( oneOf [base.hour, extend.hour] )
    ( oneOf [base.minute, extend.minute] )
    ( oneOf [base.second, extend.second] )
    ( oneOf [base.millisecond, extend.millisecond] )