All Projects → elm → Time

elm / Time

Licence: other
A simplified approach to working with dates, times, and time zones.

Programming Languages

elm
856 projects

Projects that are alternatives of or similar to Time

Posix tz db
Generates POSIX timezones strings
Stars: ✭ 57 (-31.33%)
Mutual labels:  time, timezone, posix
Friend-Time
Discord bot - Friend Time helps your server coordinate times and events by converting times mentioned in chat between time zones!
Stars: ✭ 62 (-25.3%)
Mutual labels:  time, timezone
Prayer Times Android Azan
Prayer + Time + Android + Kotlin + Azan + Library + timezone + islamic + salah + Library aiming to calculate prayer time with one line code , if you implement prayer time application , there is no need to do this headache again .
Stars: ✭ 251 (+202.41%)
Mutual labels:  time, timezone
TimesDates.jl
Nanosecond resolution for Time and Date, TimeZones
Stars: ✭ 28 (-66.27%)
Mutual labels:  time, timezone
Time
Building a better date/time library for Swift
Stars: ✭ 1,983 (+2289.16%)
Mutual labels:  time, timezone
Date
A date and time library based on the C++11/14/17 <chrono> header
Stars: ✭ 2,389 (+2778.31%)
Mutual labels:  time, timezone
time machine
A date and time API for Dart
Stars: ✭ 120 (+44.58%)
Mutual labels:  time, timezone
Date Fns Timezone
Parsing and formatting date strings using IANA time zones for date-fns.
Stars: ✭ 118 (+42.17%)
Mutual labels:  time, timezone
Shell-Scripts
Shell scripts about some basic topics, current time, calculator, sorting, restaurant and more.
Stars: ✭ 100 (+20.48%)
Mutual labels:  time, timezone
timezones
Nim timezone library compatible with the standard library.
Stars: ✭ 37 (-55.42%)
Mutual labels:  time, timezone
Delorean
Delorean: Time Travel Made Easy
Stars: ✭ 1,793 (+2060.24%)
Mutual labels:  time, timezone
NTP
NTP library for Arduino framework
Stars: ✭ 20 (-75.9%)
Mutual labels:  time, timezone
Tzupdate
Set the system timezone based on IP geolocation
Stars: ✭ 130 (+56.63%)
Mutual labels:  time, timezone
Timezone
Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments.
Stars: ✭ 205 (+146.99%)
Mutual labels:  time, timezone
Ticktock
A timezone data management library for the JVM and Android targeting java.time APIs in Java 8+
Stars: ✭ 122 (+46.99%)
Mutual labels:  time, timezone
temps-lite
A smart, good-looking little app which tries to speak your language the way you are used to.
Stars: ✭ 40 (-51.81%)
Mutual labels:  time, timezone
Js Joda
🕑 Immutable date and time library for javascript
Stars: ✭ 1,298 (+1463.86%)
Mutual labels:  time, timezone
Luatz
Time, Date and Timezone library for lua
Stars: ✭ 92 (+10.84%)
Mutual labels:  time, timezone
hs-hourglass
efficient and simpler time API for haskell
Stars: ✭ 43 (-48.19%)
Mutual labels:  time, timezone
nepali-datetime
Python's core datetime inspired nepali datetime (BS date & NPT) package 🇳🇵
Stars: ✭ 36 (-56.63%)
Mutual labels:  time, timezone

Time

To work with time successfully in programming, we need three different concepts:

  • Human Time — This is what you see on clocks (8am) or on calendars (May 3rd). Great! But if my phone call is at 8am in Boston, what time is it for my friend in Vancouver? If it is at 8am in Tokyo, is that even the same day in New York? (No!) So between time zones based on ever-changing political boundaries and inconsistent use of daylight saving time, human time should basically never be stored in your Model or database! It is only for display!

  • POSIX Time — With POSIX time, it does not matter where you live or what time of year it is. It is just the number of seconds elapsed since some arbitrary moment (in 1970). Everywhere you go on Earth, POSIX time is the same.

  • Time Zones — A “time zone” is a bunch of data that allows you to turn POSIX time into human time. This is not just UTC-7 or UTC+3 though! Time zones are way more complicated than a simple offset! Every time Florida switches to DST forever or Samoa switches from UTC-11 to UTC+13, some poor soul adds a note to the IANA time zone database. That database is loaded onto every computer, and between POSIX time and all the corner cases in the database, we can figure out human times!

So to show a human being a time, you must always know the POSIX time and their time zone. That is it. So all that “human time” stuff is for your view function, not your Model.

Example

To figure out a human time, you need to ask two questions: (1) what POSIX time is it? and (2) what time zone am I in? Once you have that, you can decide how to show it on screen:

import Time exposing (utc, toHour, toMinute, toSecond)

toUtcString : Time.Posix -> String
toUtcString time =
  String.fromInt (toHour utc time)
  ++ ":" ++
  String.fromInt (toMinute utc time)
  ++ ":" ++
  String.fromInt (toSecond utc time)
  ++ " (UTC)"

Notice that we provide the utc time zone to toHour and toMinute!

Go here for a little example application that uses time. It can help you get everything hooked up in practice!

Recurring Events

A lot of programmers need to handle recurring events. This meeting repeats every Monday. This event is the first Wednesday of each month. And there are always exceptions where a recurring event gets moved! Using human time does not solve this!

To properly handle recurring events, you need to create a custom type for your particular problem. Say you want to model a weekly event:

import Time

type alias WeeklyEvent =
  { weekday : Time.Weekday                  -- which day is it on
  , hour : Int                              -- at what hour?
  , zone : Time.Zone                        -- what time zone is that hour in?
  , start : Time.Posix                      -- when was the first event?
  , exceptions : List (Int, Maybe Event)    -- are there any skips?
  }

The first two fields (weekday and hour) are the most straight forward. You gotta know what day and what time! But that is not enough information for people in different time zones. Tom created the recurring event for hour 16, but how do I show that in Tokyo or New York? Or even in Tom’s location?! The zone lets us pin weekday and hour to a specific POSIX time, so we can show it elsewhere.

Great! But what about shifting the meeting by one day for a holiday? Well, if you define a start time, you can store exceptions as offsets from the first ever event. So if only the third event was cancelled, you could store [ (3, Nothing) ] which would say “ignore the third event, and do not replace it with some other specific event.”

Implications

Now the particular kinds of recurring events you need are specific to your application. Weekly? Monthly? Always has start and end of range? Allows exceptions? I am not convinced that a generic design is possible for all scenarios, but maybe with further exploration, we will find that it is.

So if you need recurring events, you have to model them yourself. There is no shortcut. Putting May 3rd in your Model is not gonna do it. It is a trap. Thinking in human time is always a trap!

ISO 8601

ISO 8601 is not supported by this package because:

The ISO 8601 format has lead to a great deal of confusion. Specifically because it gives the illusion that it can handle time zones. It cannot! It allows you to specify an offset from UTC like -05:00, but is that a time in Quebec, Miami, Cuba, or Equador? Are they doing daylight saving time right now?

Point is, the only thing ISO 8601 is good for is representing a Time.Posix, but less memory efficient and more confusing. So I recommend using Time.posixToMillis and Time.millisToPosix for any client/server communication you control.

That said, many endpoints use ISO 8601 for some reason, and it can therefore be quite useful in practice. I think the community should make some packages that define fromIso8601 : String -> Maybe Time.Posix in Elm. People can use elm/parser to make a fancy implementation, but maybe there is some faster or smaller implementation possible with String.split and such. Folks should experiment, and later on, we can revisit if any of them belong in this library.

Future Plans

Right now this library gives basic Posix and Zone functions, but there are a couple important things it does not cover right now:

  1. How do I get my time zone?
  2. How do I get another time zone by name?
  3. How do I display a time for a specific region? (e.g. DD/MM/YYYY vs MM/DD/YYYY)

I think points (2) and (3) should be explored by the community before we add anything here. Maybe we can have a package that hard codes the IANA time zone database? Maybe we can have a package that provides HTTP requests to ask for specific time zone data? Etc.

Note: If you make progress that potentially needs coordination with other developers, talk to people. Present your work on discourse to learn what the next steps might be. Is the idea good? Does it need more work? Are there other things to consider? Etc. Just opening an issue like “I totally redid the API” is not how we run things here, so focus on making a strong case through normal packages and friendly communication! And on timelines, we try to make one great choice ever (not a different choice every month) so things will take longer than in the JS world.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].