Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date utilities module #17

Open
adobni opened this issue Jun 19, 2020 · 6 comments
Open

Date utilities module #17

adobni opened this issue Jun 19, 2020 · 6 comments
Labels
proposal For new language proposals

Comments

@adobni
Copy link

adobni commented Jun 19, 2020

It could be useful to have a date module, with common operations that are used in businesses, like days in a month, and epoch time conversion to date/time.

@adobni adobni added the proposal For new language proposals label Jun 19, 2020
@machaval
Copy link
Contributor

machaval commented Jun 19, 2020

For epoch we have :

To Epoc

%dw 2.0
output application/json
---
now() as Number {unit: "seconds"}

From Epoc

%dw 2.0
output application/json
---
1592601210 as DateTime {unit: "seconds"}

Days in a month is tricky as it may depend in the year (We all like feb)

But the other day a request came that we should be able to extract more information from timezeone

{
"displayName": "Eastern Standard Time",
"dstSavings": 3600000,
"id": "America/New_York",
"observesDaylightTime": true,
"rawOffset": -18000000,
"useDaylightTime": true
}

This kind of information. This may be interesting to analyze. Regarding date functions I think we also need Period factory methods.

Today if I want to substract a dynamic amount of days I need to use string interpolation and coercion.

now() - Period::days(1)

@jerneyio
Copy link
Contributor

daysInMonth could be simple if you pass it a specific date:

daysInMonth(|2020-02-03|)

@psmith
Copy link

psmith commented Jun 25, 2020

As Jerney said, days in month is easy, if you have a given date. A little weird, but I use something similar to get the first or last day of the month.

I wrote my own date module for stuff like this.

%dw 2.0
output application/json
fun lastDayOfMonth(d: Date) = d + |P1M| - ('P$(d.day as String)D') as Period
fun daysInMonth(d: Date) = lastDayOfMonth(d).day
---
{
    leapYearFeb: daysInMonth(|2020-02-13|),
    nonLeapYearFeb: daysInMonth(|2019-02-13|)
}

Results:

{
  "leapYearFeb": 29,
  "nonLeapYearFeb": 28
}

@psmith
Copy link

psmith commented Jun 25, 2020

This kind of information. This may be interesting to analyze. Regarding date functions I think we also need Period factory methods.

Today if I want to substract a dynamic amount of days I need to use string interpolation and coercion.

now() - Period::days(1)

I love this idea, the string interpolation for dynamic periods is really annoying when you don't know how to do it. Easy, like most things, after you figure it out.

@machaval
Copy link
Contributor

I'm going to separate this thing in two parts

  1. Is Convertions and Formating: Going from a Date to a number or a String and Back. I think we have a general problem with this that is not just for Dates
  2. Missing functionality arround dates. This issue is going to be about this. And a new one is going to be created for 1.

@machaval
Copy link
Contributor

I propose one single module dw::core::Dates with all the functionality. This will make it simple to remember. One Module that will allow to do Date Arithmetic date manipulation.

%dw 2.0

fun today():Date =
  now() as Date

fun month(months: Number):Period =
  "P$(months)M" as Period

fun years(months: Number):Period =
  "P$(months)Y" as Period

fun days(nDays: Number):Period =
  "P$(nDays)D" as Period

fun hours(nHours: Number):Period =
  "PT$(nHours)H" as Period

fun minutes(nMinutes: Number):Period =
  "PT$(nMinutes)M" as Period

fun seconds(nSecs: Number):Period =
  "PT$(nSecs)S" as Period

fun toBeginningOfHour(dateTime: DateTime) =
  "$(dateTime as Date)T$(dateTime.hour):00:00.000$(dateTime.timezone)" as DateTime

fun toBeginningOfDay(dateTime: DateTime) =
  "$(dateTime as Date)T00:00:00.000$(dateTime.timezone)" as DateTime

fun toBeginningOfMonth(dateTime: DateTime) =
  "$(dateTime as Date - days(dateTime.day))T00:00:00.000$(dateTime.timezone)" as DateTime

fun toBeginningOfWeek(dateTime: DateTime) =
  "$(dateTime as Date - days(dateTime.dayOfWeek))T00:00:00.000$(dateTime.timezone)" as DateTime

fun toBeginningOfYear(dateTime: DateTime) =
  "$(dateTime as Date - days(dateTime.dayOfYear))T00:00:00.000$(dateTime.timezone)" as DateTime

Now we also have things like :

{
  "wday": now().dayOfWeek, //This will return 2
  "yday": |2020-09-29T16:11:12.195-03:00|.dayOfYear, // This return the day of the year for example 273 
}

The question is shouldn't this be functions? there are transformations in this selection. For me . should only be used for extracting data |2020-09-29T16:11:12.195-03:00|.year is ok because you are taking date out of the date but I don't see this on this examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal For new language proposals
Projects
None yet
Development

No branches or pull requests

4 participants