 Lao and I'm a software developer. I really like elixir How many of you like elixir? Yeah, and I have done some open source a lot of elixir Stuff and one of the things I've done is calendar library and also time zone T-set data library and I have a blog where I've written a lot about Time and date and that kind of stuff See it's creative deletion the calm and I'm also on Twitter and github so First of all, what is a type? There are a lot of definitions of it one of them is that it's a set of possible values And why do we want types why even have them? So again, there's a lot of reasons to have types a lot of Opinions one reason is that for instance if you have five divided by two you get something meaningful But if you have five five by Orlando that doesn't make so much sense so in that case we want errors and That's a good reason to have types that if something goes wrong we want to know about it and I'm gonna talk about explicit data Do any of you know what this is? Well, it isn't anymore That was that the Mars climate orbiter and it went along towards Mars on a 286-day journey and Then it failed and Why was that? because there's some people that was supposed to send data about how it was going About some thruster data and they used pound seconds instead of Newton seconds and I'm used to the metric system, but I really like the imperial system because it's very convenient I like the whole thing with like feet and inches based on your body. So Vince my hide I found out just recently that Converted to the imperial system. It's One foot one leg one torso one neck and one head So it's very easy. I like that but It's not really necessary that important What units you use as long as you know which units you're using? And in Physics class in school. I was taught that you always need to carry your units with you when you do calculations So at the end, do you know what you? What you have because if you just have numbers At some point something might go wrong. You don't know what you end up with so imagine if They're probably just sending numbers But imagine if with this example of this spacecraft, they'd send numbers and the unit Maybe as a top like this in elixir or maybe as A structs and then you could do pattern matching and so imagine that You required to have Newton seconds and then you you sent Pound seconds instead you get an error, right? So imagine if you had they've been doing testing with just integers It would all work and they were like this is ready to go to space But if they use this instead, maybe they would have seen the error Because one thing is to have something in documentation. Another thing is to have code So with pattern matching and testing You get errors, which is good. I really like errors It's a bit like, you know, like pain. It's not so nice But if you have your hand on a hot plate, it can help save your hand. So There are a lot of units of time and date We have years we have years and months and we have Times we have data and times together and they know time together in a specific location So how's that handled in? computers often For instance in Unix, there's just one type which is A date a time in a specific time zone, which is always UTC so if you have a date it gets jammed into this concept, which is a Date time in a specific time zone if you have a time it gets jammed into the same concept again and Even if it's a daytime you always get that Time zone attached to it so then you run into a problem of If you have you want to encode just a time and you ask afterwards what years that time in and There might be some random year that was never there in the data originally But somehow it got into your system Because it has to be there because it's kind of forced into this concept of a daytime Even though it was just a time And the same thing for just dates I wrote also blog posts that Everyone knows what a date is except programmers because For most people including the dictionary if they can be Time sorry, it cannot be a time can be see even I'm confused So you have you have if they can be a year a month or A year in a month or a year and a month and a day of the month But it doesn't really have a time So I think in general in computer science and programming a lot of these concepts get confusing because They're made more confusing that they really are So instead of having just one one type We can have multiple ones and so I made this library to the county library where there are four different types and you could have Many different ones you could have for instance a type just for a year But in this case There's no time for just a year for that you would have to just use an integer But the ones that are there are ones that are commonly used and They also fit well with for instance Databases in a database that date has a year a month and a day of the month So let's look at them Yeah, so in 2014 you had encounter You haven't so have these these types A date a time they're pretty simple Like what you would expect them to be and then an a daytime is a combination of a date and a time and then you have a Date time so let's see here date time And they also have a microsecond And here's the combination So the date time is special at date time is the result of a calculation where you also have time zones Into it. So let's say what is the time right now? in New York, which is the the time zone we're in right now here in Florida as well It's like an ephemeral Struct that is based on also the current time zone rules So in addition to what you have in an a daytime you have a time zone To see offset a standard offset and so on abbreviation So the the reason I say is ephemeral is because the rules can change so you ask the library for a daytime and and with the current rules the standard offset are in one way, but if you Do the same calculation in 10 years? Maybe it's different because the rules were changed So something about the the naming in In some languages you have something called local in Java this is the Description of a local day date in Java. So if you have 2007 December the 3rd that is a local date so why not call a Date in elixir the local date. Well, it turns out that This is another case of things being more confusing That they should be because actually local means something that belongs to a particular area or a neighborhood or something that that is has something else then it's not global Right, it's it's local so Then if you ask yourself 2007-1203 Which area does that belong to? Yeah, I don't know either like All right, it's it's it's not a local thing It's just a more like a global date Exist without any tie to a particular place So that is why it is called Just a date. So here's a quiz Does anyone want to try there? No lifelines? Anyone want to get it's a it's a trick question And then the hands went down Right, so a lot of a lot of bees here The answer is that We can't know What the answer is here and what why is that because we do not know What time zone it is so Eunice Unix time is always in UTC so if you have this string of 1970 January 1st at midnight You don't know what the Unix time is the equivalent of that unless you know what time zone it is in Yeah, so so you like you like errors. I like errors too. So The way it works right now in in elixir 1.3 is like this you can do this and you get an error Because it doesn't match if you have this is not naive I'm gonna you're gonna see more of that like it's a sickle for a naive daytime and It doesn't match if you try to Pass that into this date time Function it needs a daytime that has as we saw before it has time zone information So these are the structs. They're built into elixir 1.3 So they're Pretty much they're almost the same But the difference is that there is no Calendar propended in the module names that does one of the main differences and We also have sickles for date for time and for a night daytime and You might notice that there's no sickle for daytime So we have it just for the three of these So one of the the changes from the calendar library to the To the structs that are built into elixir 1.3 is that we have this field called calendar Which is so that if you want to use a different calendar than the ISO calendar, for instance a cop take calendar Something like that. You could you could use the same struct But there's no functionality for that built into elixir. So Some external library would have to To do that kind of calculation, but the good news is that most of the world Except like one country uses the ISO calendar at least for business purposes so for for most of us we don't have to Most of us don't have to Use any other calendar So another thing we have is micro seconds That is and there was that in the calendar library as well But this is a new thing where you have a couple of two integers so the first Element is the number of microseconds and the second one is the Significant fractional seconds so a cool thing about that is for instance if you have Here the first example you have point one two three four and in many other libraries Maybe the result you wouldn't know that you only knew about these first four Decimals so it would be converted to point one two three four zero zero zero Or two zeros and and in the second example you see that when you parse this String again, we only have dot one and we don't get dot one zero zero zero So the way it works is if you if you get the microsecond Field you can see that this is how it works. So in the second example for instance you have two significant Decimals, so that's why it says to There's a hundred thousand microseconds and then two significant digits so in order to With the sickles and the built-in Modules in elixir You can do some things But you can't do much with time zones for that you need You should add this to your dependencies and then you can for instance you can do stuff like Converting and I date time to a daytime so if you remember remember the example from the From our little quiz That We didn't have a date time To pass to the date time to your next function, but in this case we explicitly Transform this late daytime to a daytime in we say it's UTC like if you know that what you have here is UTC you can transform it and then Functions that only accept Date times that are not made date times. They can use it So here's an example of a function that's built in so we see there's no calendar propended to the module and We can pretty much the only thing the only way you can create a date time With the built-in library as of now is from a UNIX integer, but if we If we then have calendar there's a way to shift that That daytime to a different time zone so you can notice that The hour here was 17 hours and when we shift it it's 13 hours and then here's an example of How you can You can combine combine the different Libraries you can like the building library returns a daytime and then you can shift it and then because you get the same struck back you can use a function that again expects daytime and you You see you get a string back and here's another example where you have a string for time and Yeah, again, you can notice that there's combination of the different Different modules and here's a sickle We can ask if it's Friday and here we can compare if today is before tomorrow and another example For more examples and here is You can get the day today when you provide a time zone I think you should notice is When you ask what what date is it today you could have like a function that just says Today and then there's a default, but that's not really a good idea because If you don't if you're not aware of What you have like if you're not aware of the time zone you're not aware what the default is you might get confused so that's why it takes a specific time zone and then Here's yet another example and this is we can add dates to a date from a single and then call it here So what about getting a time zone of the user? There's there's a bunch of ways to do that. The thing is For instance, if you're in Phoenix You and you have some web app with the JavaScript You might use what you can get is you can get an offset but JavaScript Doesn't really have a way to get the canonical time zone of the user There are some libraries that Can get can can guess Make a qualified guess about it So what you want is for instance something like America slash New York Instead of let's say minus four hours, so there's a bunch of ways you can also You can use those JavaScript libraries and hopefully I hope that in the future That in JavaScript is going to be a standard where you can just say what is the time zone of the current user another thing you can do is you can You can use like geo IP so there are ways that you provide an IP address and you get back Iana times ID again like America slash New York So if you have a web app You could try to do all these guesses, but really the best way is to have a drop-down and the user can choose Can choose the time zone so persistence in Exile so she was if a lean told me that in the plan is that in version 2.1 There's the these structs are going to be built into ecto But for now what you can do is Use the the collector library so you add that and then for the for the types in your schema instead of having for instance ecto dot Date you can have collector dot date and there's a special There's a special thing for if you use postgres You can have a special postgres collector type where there's a Composite type that has This is for a daytime that has the time zone the local time and then an offset And the good thing about that is that if there are any changes to the rules, let's say for this That this Talk was scheduled a year ago, and in the meantime there have been changes to the rules If you had used this type it would still go and ask When you retreat retreat this from the database again after use the rules have changed it will it would use the new rules So that is Whatever it may come in to do So the the main takeaway here is that you want to Select a type that fits the data you have so if you have some Data that it comes in you want to preserve as much information about that as possible But you don't want to make up new things And and one thing about the new types is for instance one thing I would like to see is that let's say you have a log Currently the logging in elixir uses tuples, but it doesn't have any time zone information So you could configure your logging to be local time or to be utc but when you actually get that That information you don't know what it is So this might be passed on to your application a bunch of different places, and then you have a function That received this and you don't know what time zone is and it isn't so that means that you can't do There's a lot of things you can't do you can't compare it to another time and see what happened first right so in This case I would like to have Logger use always use utc like what one thing I think that people that work in in Computer science, especially sys admins and also programmers You should all know what the time is in utc and all set the service to utc like one thing is if you have Finishes your schedule a talk or sketch a schedule of conference You can't expect people to use you to see and I wouldn't want to either. I think time zones are very useful But for for things that happens on computers you You would want to use you to see because of the issues that if you use let's say you have a cron job in Local time you have your server set to something else in utc your cron job might never run it might run twice So come back to the elixir locks I Hope that in the future they will be in the native day time type and then you can do all kinds of things you like you have in your lock You can actually compare it to other day times and you can find out where it actually was When it actually happened so the whole idea of having these structs in elixir is that for instance in ecto you can use the same type as in other In other libraries and it can all work together so the in the speaking about the future of these of calendar types in in Elixir you can see that right now there's The functionality is kind of limited and you need an external library The hope is that in the future in future versions of elixir. There's going to be a Way to have time zone data And time zone data functionality shipping with elixir either you would have to add the time zone data library or would it would ship with elixir but if you right now you could use the the counter library and It should be in the future an easy transition when it gets when it if it hopefully gets built in This kind of functionality Yeah, that's a that's a that's a very good example Yeah, actually at first I did some start doing some code to handle those Structs or sorry those tuples and it was confused because ecto used microseconds and logger used Miliseconds, but that that's a very good example of Why it's these new Types are good because you could use either naive day times or day times and at least that the The micro the whole micro my millisecond thing would be solved right so we have some extra time and I would love to Answer any questions before we move on to the questions. Let's just give now a big round of applause right questions The country that does not use the calendar the rest of the world is that Thailand or the Buddhist calendar? Or is there another one that I don't know about Yeah, I I think it might be Ethiopia That's I think that's the one I was thinking about To correct myself. I think they do use the same calendar as we do but the year is They had 500 something to the year because they count since the year of Buddha's death Okay, so I guess Yeah, so So there are a lot of There are a bunch of places that have Other calendars than the ISO calendar But for business purposes basically the entire world when doing International business They use the ISO calendar So and sometimes you use I mean even Like in for Like for many holidays, they are calculated based on lunar calendars or other things but still in and Daily live for many of the other things we use the ISO calendar Hi First I just want to say thank you for working on this I know this isn't like the most obviously sexy thing, but it's so painful without your work So the question though is you mentioned The that there was an Ecto adapter like the Cal Ecto. Yeah, Postgres. Is does that apply to my sequel too? Or is that yeah, so yeah Collector works for all the databases. It's just that there's this one special Case where if you want to save at daytime Like a daytime that has the like the daytime time you have in an elixir that has all this The time zone information with it you can use this type so if you don't use Postgres what you can do is You're kind of forced to save the save it as a naive daytime And then what you can do is you along with that you can save the the time song So you can use it Yeah, you can use it with any of the the databases Sure, I just want to say real quick Fred Hebert's talk at Erling factory this year history of time was awesome and goes over all that stuff about Lunar calendars, right? Yeah, I have to watch it. It's Yeah, also If you're more interested in this kind of stuff, you can also see I did a talk last year And it's on it's on YouTube and it also talk about stuff that like Tommy clocks and Planets and stuff still up still have nice mess Some things I learned from Lous talk last year. It's well worth looking into Well More questions see a hand going up Hi I've been using time mix in combination with my sequel and run to a number of problems when doing tests, especially with Microseconds, mm-hmm because I take a date save in the database and when getting my record out again It would have lost the microseconds does Caliq to Offering help here or? Yeah, what what you can do is when you If you use collector the way you saw that is there is an option Where you say that you want to save the fractional seconds and then that gets Saved into your database and you get them back again You don't want it So the value that's returned from the database Has no microseconds, but the value that I initially sent to the database had microseconds, and I'm comparing them of course my testing fail Okay so you you you save Something with microseconds, but you don't want them to be saved or well Well, it's very simple. I create I just take the for example the current time. Yes I save it to the database and Later on in my tests I get the record out of the database and verify whether the date is the one that I send But the date that I send and microseconds and the one that I get from my sequel has no microseconds So my test fails Right. Yeah in this case It should if you configure it to save the microseconds you get you have them both Both places so they should match Yeah, but it's the database that's used in also in the real system and they're not allowed to have a microseconds for other reasons So it's just wondering there was a way to get rid of the microseconds. Yeah, you can Ask me after what's and I'll and in general Anyone also that wants to talk to me about this stuff find me after this and Think we'll type for another question. Okay then we have applause