You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Continue with the #17 here we are bringing a new module dw::core::Dates This module introduce factory methods for all Date types (Date, DateTime, Time..) We also are introducing helper function for scheduling.
== dw::core::Dates
%dw2.0import*fromdw::core::Periodsimport failIf fromdw::Runtime/*** Returns the Date for today.** _Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later._** === Example** This example shows the output of `today` function.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* today()* ----** ==== Output** [source,Json,linenums]* ----* "2020-10-06"* ----**/@Since(version="2.3.1")
funtoday() =now() asDate/*** Returns the Date for yesterday.** _Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later._** === Example** This example shows the output of `today` function.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* yesterday()* ----** ==== Output** [source,Json,linenums]* ----* "2020-10-05"* ----**/@Since(version="2.3.1")
funyesterday() =today() -days(1)
/*** Returns the Date for tomorrow.** _Introduced in DataWeave 2.3.1. Supported by Mule 4.3.1 and later._** === Example** This example shows the output of `tomorrow` function.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* tomorrow()* ----** ==== Output** [source,Json,linenums]* ----* "2020-10-07"* ----**/@Since(version="2.3.1")
funtomorrow() =today() +days(1)
typeZoned= {timeZone:TimeZone}
typeDateFactory= {day:Number, month:Number, year:Number}
typeLocalTimeFactory= {hour:Number, minutes:Number, seconds:Number}
typeTimeFactory=LocalTimeFactory &ZonedtypeDateTimeFactory=DateFactory &LocalTimeFactory &ZonedtypeLocalDateTimeFactory=DateFactory &LocalTimeFactory
/*** Creates a Date value with the given date properties (year, month, day).** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | parts | The date fields* |===** === Example** This example shows how to create a value of type Date** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* %dw 2.0* output application/json* import * from dw::core::Dates* ---* {* newDate: date({year: 2012, month: 10, day: 11})* }* ----** ==== Output** [source,Json,linenums]* ----* {* "newDate": "2012-10-11"* }* ----**/fundate(parts:DateFactory):Date=do {
varnYear=parts.yearasStringvarnMonth=failIf( parts.month , (month) ->month < 0 or month > 13, "Field 'month': `$(parts.month)` must be between 1 and 12.") asStringvarnDay=failIf( parts.day, (day) ->day < 0 or day > 31, "Field 'day': `$(parts.day)` must be between 1 and 31.") asString---"$(nYear)-$(nMonth)-$(nDay)"asDate
}
/*** Creates a DateTime value with the given datetime properties (year, month, day, hour, minutes, seconds and timeZone).** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | parts | The date fields* |===** === Example** This example shows how to create a value of type DateTime** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* %dw 2.0* output application/json* import * from dw::core::Dates* ---* {* newDateTime: dateTime({year: 2012, month: 10, day: 11, hour: 10, minutes: 10, seconds: 10 , timeZone: |-03:00|})* }* ----** ==== Output** [source,Json,linenums]* ----* {* "newDateTime": "2012-10-11T10:10:10-03:00",* }* ----**/fundateTime(parts:DateTimeFactory):DateTime=do {
varnYear=parts.yearasStringvarnMonth=failIf( parts.month , (month) ->month < 0 or month > 12, "Field 'month': `$(parts.month)` must be between 1 and 12.") asStringvarnDay=failIf( parts.day, (day) ->day < 0 or day > 31, "Field 'day': `$(parts.day)` must be between 1 and 31.") asStringvarnHours=failIf( parts.hour, (hour) ->hour < 0 or hour > 24, "Field 'hours': `$(parts.hour)` must be between 0 and 24.") asStringvarnMinutes=failIf( parts.minutes, (minutes) ->minutes < 0 or minutes > 60, "Field 'minutes': `$(parts.minutes)` must be between 0 and 60.") asString---"$(nYear)-$(nMonth)-$(nDay)T$(nHours):$(nMinutes):$(parts.secondsasString)$(parts.timeZoneasString)"asDateTime
}
/*** Creates a LocalDateTime value with the given localDatetime properties (year, month, day, hour, minutes, seconds).*** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | parts | The localDateTime fields* |===** === Example** This example shows how to create a value of type DateTime** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* %dw 2.0* output application/json* import * from dw::core::Dates* ---* {* newLocalDateTime: localDateTime({year: 2012, month: 10, day: 11, hour: 10, minutes: 10, seconds: 10})* }* ----** ==== Output** [source,Json,linenums]* ----* {* "newLocalDateTime": "2012-10-11T10:10:10",* }* ----**/funlocalDateTime(parts:LocalDateTimeFactory):LocalDateTime=do {
varnYear=parts.yearasStringvarnMonth=failIf( parts.month , (month) ->month < 0 and month > 12, "Field 'month': `$(parts.month)` must be between 1 and 12.") asStringvarnDay=failIf( parts.day, (day) ->day < 0 and day > 31, "Field 'day': `$(parts.day)` must be between 1 and 31.") asStringvarnHours=failIf( parts.hour, (hour) ->hour < 0 and hour > 24, "Field 'hours': `$(parts.hour)` must be between 0 and 24.") asStringvarnMinutes=failIf( parts.minutes, (minutes) ->minutes < 0 and minutes > 60, "Field 'minutes': `$(parts.minutes)` must be between 0 and 60.") asString---"$(nYear)-$(nMonth)-$(nDay)T$(nHours):$(nMinutes):$(parts.secondsasString)"asLocalDateTime
}
/*** Creates a LocalTime value with the given localtime properties (hour, minutes, seconds).** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | parts | The localTime fields* |===** === Example** This example shows how to create a value of type LocalTime** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* %dw 2.0* output application/json* import * from dw::core::Dates* ---* {* newLocalTime: localTime({ hour: 10, minutes: 10, seconds: 10}),* }* ----** ==== Output** [source,Json,linenums]* ----* {* "newLocalTime": "10:10:10"* }* ----**/funlocalTime(parts:LocalTimeFactory):LocalTime=do {
varnHours=failIf( parts.hour, (hour) ->hour < 0 and hour > 24, "Field 'hours': `$(parts.hour)` must be between 0 and 24.") asStringvarnMinutes=failIf( parts.minutes, (minutes) ->minutes < 0 and minutes > 60, "Field 'minutes': `$(parts.minutes)` must be between 0 and 60.") asString---"$(nHours):$(nMinutes):$(parts.secondsasString)"asLocalTime
}
/*** Creates a Time value with the given time properties (hour, minutes, seconds and timeZone).** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | parts | The time fields* |===** === Example** This example shows how to create a value of type Time** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* %dw 2.0* output application/json* import * from dw::core::Dates* ---* {* newTime: time({ hour: 10, minutes: 10, seconds: 10 , timeZone: |-03:00|}),* }* ----** ==== Output** [source,Json,linenums]* ----* {* "newTime": "10:10:10-03:00"* }* ----**/funtime(parts:TimeFactory):Time=do {
varnHours=failIf( parts.hour, (hour) ->hour < 0 and hour > 24, "Field 'hours': `$(parts.hour)` must be between 0 and 24.") asStringvarnMinutes=failIf( parts.minutes, (minutes) ->minutes < 0 and minutes > 60, "Field 'minutes': `$(parts.minutes)` must be between 0 and 60.") asString---"$(nHours):$(nMinutes):$(parts.secondsasString)$(parts.timeZoneasString)"asTime
}
/*** Returns a new DateTime with the Time changed to the start of the current hour** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The DateTime used as reference* |===** === Example** This shows how it changes the specified Time to the the start of the given hour.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfHour": atBeginningOfHour(|2020-10-06T18:23:20.351-03:00|)* }* ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfHour": "2020-10-06T18:00:00-03:00"* }* ----**/funatBeginningOfHour(dateTime:DateTime):DateTime="$(dateTimeasDateasString)T$(dateTime.hourasString):00:00.000$(dateTime.timezoneasString)"asDateTime/***Returns a new LocalDateTime changed to the beginning of the given hour** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | localDateTime | The LocalDateTime used as reference* |===** === Example** This shows how it changes the specified Time to the the start of the given hour.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfHour": atBeginningOfHour(|2020-10-06T18:23:20.351|)* }* ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfHour": "2020-10-06T18:00:00"* }* ----**/funatBeginningOfHour(localDateTime:LocalDateTime):LocalDateTime="$(localDateTimeasDateasString)T$(localDateTime.hourasString):00:00.000"asLocalDateTime/*** Returns a new LocalTime changed to the start of the current hour** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime |* |===** === Example** This shows how it changes the specified LocalTime to the the start of the given hour.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfHour": atBeginningOfHour(|18:23:20.351|)* }* ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfHour": "18:00:00"* }* ----**/funatBeginningOfHour(localTime:LocalTime):LocalTime="$(localTime.hourasString):00:00.000"asLocalTime/*** Returns a new Time changed to the beginning of the given hour.** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | time | The time to be used as reference* |===** === Example** This shows how it changes the specified Time to the the start of the given hour.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfHour": atBeginningOfHour(|18:23:20.351-03:00|)* }* ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfHour": "18:00:00-03:00"* }* ----**/funatBeginningOfHour(time:Time):Time="$(time.hourasString):00:00.000$(time.timezoneasString)"asTime/*** Returns a new DateTime but at the beginning of the day of the given DateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The dateTime to be used as reference* |===** === Example** This example shows how it transforms the DateTime `2020-10-06T18:23:20.351-03:00` to the same date but at the start of the given day.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfDayDateTime": atBeginningOfDay(|2020-10-06T18:23:20.351-03:00|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfDayDateTime": "2020-10-06T00:00:00-03:00"* }* ----**/funatBeginningOfDay(dateTime:DateTime):DateTime="$(dateTimeasDateasString)T00:00:00.000$(dateTime.timezoneasString)"asDateTime/*** Returns a new LocalDateTime but at the beginning of the day of the given LocalDateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The dateTime to be used as reference* |===** === Example** This example shows how it transforms the LocalDateTime `2020-10-06T18:23:20.351-03:00` to the same date but at the start of the given day.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfDayLocalDateTime": atBeginningOfDay(|2020-10-06T18:23:20.351|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfDayLocalDateTime": "2020-10-06T00:00:00"* }* ----**/funatBeginningOfDay(dateTime:LocalDateTime):LocalDateTime="$(dateTimeasDateasString)T00:00:00.000"asLocalDateTime/*** Returns a new DateTime but at the beginning of the Month of the given DateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The dateTime to be used as reference* |===** === Example** This example shows how it transforms the DateTime `2020-10-06T18:23:20.351-03:00` to the same date but at the start of the given Month.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfMonth(|2020-10-06T18:23:20.351-03:00|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfMonth": "2020-10-01T00:00:00-03:00"* }* ----**/funatBeginningOfMonth(dateTime:DateTime):DateTime="$((dateTimeasDate-days(dateTime.day-1)) asString)T00:00:00.000$(dateTime.timezoneasString)"asDateTime/*** Returns a new LocalDateTime but at the beginning of the Month of the given LocalDateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The localDateTime to be used as reference* |===** === Example** This example shows how it transforms the LocalDateTime `2020-10-06T18:23:20.351` to the same date but at the start of the given Month.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* "atBeginningOfMonth": atBeginningOfMonth(|2020-10-06T18:23:20.351|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfMonth": "2020-10-01T00:00:00"* }* ----**/funatBeginningOfMonth(localDateTime:LocalDateTime):LocalDateTime="$((localDateTimeasDate-days(localDateTime.day-1)) asString)T00:00:00.000"asLocalDateTime/*** Returns a new Date but at the beginning of the Month of the given Date** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The date to be used as reference* |===** === Example** This example shows how it transforms the Date |2020-10-06| to the same date but at the start of the given Month.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfMonthDate: atBeginningOfMonth(|2020-10-06|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfMonth": "2020-10-01"* }* ----**/funatBeginningOfMonth(date:Date):Date="$((date-days(date.day-1)) asString)"asDate/*** Returns a new DateTime but at the beginning of the Week of the given DateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The dateTime to be used as reference* |===** === Example** This example shows how it transforms the DateTime |2020-10-06T18:23:20.351-03:00| to the same date but at the start of the given Week.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfWeekDateTime: atBeginningOfWeek(|2020-10-06T18:23:20.351-03:00|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfMonth": "2020-10-04T00:00:00-03:00"* }* ----**/funatBeginningOfWeek(dateTime:DateTime):DateTime="$((dateTimeasDate-days(dateTime.dayOfWeek)) asString)T00:00:00.000$(dateTime.timezoneasString)"asDateTime/*** Returns a new LocalDateTime but at the beginning of the Week of the given LocalDateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | localDateTime | The localDateTime to be used as reference* |===** === Example** This example shows how it transforms the LocalDateTime |2020-10-06T18:23:20.351| to the same date but at the start of the given Week.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfMonth: atBeginningOfWeek(|2020-10-06T18:23:20.351|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfMonth": "2020-10-04T00:00:00"* }* ----**/funatBeginningOfWeek(localDateTime:LocalDateTime):LocalDateTime="$((localDateTimeasDate-days(localDateTime.dayOfWeek)) asString)T00:00:00.000"asLocalDateTime/*** Returns a new Date but at the beginning of the Week of the given Date** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | date | The date to be used as reference* |===** === Example** This example shows how it transforms the Date |2020-10-06| to the same date but at the start of the given Week.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfMonth: atBeginningOfWeek(|2020-10-06|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfMonth": "2020-10-04"* }* ----**/funatBeginningOfWeek(date:Date):Date="$((date-days(date.dayOfWeek)) asString)"asDate/*** Returns a new DateTime but at the beginning of the Year of the given DateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | dateTime | The dateTime to be used as reference* |===** === Example** This example shows how it transforms the DateTime |2020-10-06T18:23:20.351-03:00| to the same date but at the start of the given Year.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfYear: atBeginningOfYear(|2020-10-06T18:23:20.351-03:00|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfYear": "2020-01-01T00:00:00.000-03:00"* }* ----**/funatBeginningOfYear(dateTime:DateTime):DateTime="$((dateTimeasDate-days(dateTime.dayOfYear-1)) asString)T00:00:00.000$(dateTime.timezoneasString)"asDateTime/*** Returns a new LocalDateTime but at the beginning of the Year of the given LocalDateTime** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | localDateTime | The localDateTime to be used as reference* |===** === Example** This example shows how it transforms the LocalDateTime |2020-10-06T18:23:20.351| to the same date but at the start of the given Year.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfYear: atBeginningOfYear(|2020-10-06T18:23:20.351|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfYear": "2020-01-01T00:00:00.000"* }* ----**/funatBeginningOfYear(localDateTime:LocalDateTime):LocalDateTime="$((localDateTimeasDate-days(localDateTime.dayOfYear-1)) asString)T00:00:00.000"asLocalDateTime/*** Returns a new Date but at the beginning of the Year of the given Date** === Parameters** [%header, cols="1,3"]* |===* | Name | Description* | date | The date to be used as reference* |===** === Example** This example shows how it transforms the Date |2020-10-06| to the same date but at the start of the given Year.** ==== Source** [source,DataWeave,linenums]* ----* %dw 2.0* output application/json* ---* {* atBeginningOfYear: atBeginningOfYear(|2020-10-06|)* }** ----** ==== Output** [source,Json,linenums]* ----* {* "atBeginningOfYear": "2020-01-01"* }* ----**/funatBeginningOfYear(date:Date):Date="$((dateasDate-days(date.dayOfYear-1)) asString)"asDate
Feedback on method names and missing functionality that would be interesting to have here
The text was updated successfully, but these errors were encountered:
Continue with the #17 here we are bringing a new module
dw::core::Dates
This module introduce factory methods for all Date types (Date, DateTime, Time..) We also are introducing helper function for scheduling.== dw::core::Dates
Feedback on method names and missing functionality that would be interesting to have here
The text was updated successfully, but these errors were encountered: