11import _ , { noop } from "lodash" ;
22import dayjs from "dayjs" ;
3- import utc from 'dayjs/plugin/utc' ;
43import { RecordConstructorToComp , RecordConstructorToView } from "lowcoder-core" ;
54import {
65BoolCodeControl ,
@@ -53,8 +52,6 @@ import { dropdownControl } from "comps/controls/dropdownControl";
5352import { timeZoneOptions } from "./timeZone" ;
5453
5554
56- dayjs . extend ( utc ) ;
57-
5855
5956const EventOptions = [ changeEvent , focusEvent , blurEvent ] as const ;
6057
@@ -87,7 +84,7 @@ const commonChildren = {
8784 ...validationChildren ,
8885viewRef :RefControl < CommonPickerMethods > ,
8986inputFieldStyle :styleControl ( DateTimeStyle , 'inputFieldStyle' ) ,
90- timeZone :dropdownControl ( timeZoneOptions , "Etc/UTC" ) ,
87+ timeZone :dropdownControl ( timeZoneOptions , Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ) ,
9188} ;
9289type CommonChildrenType = RecordConstructorToComp < typeof commonChildren > ;
9390
@@ -145,7 +142,7 @@ function validate(
145142
146143const childrenMap = {
147144value :stringExposingStateControl ( "value" ) ,
148- userTimeZone :stringExposingStateControl ( "userTimeZone" , 'Etc/UTC' ) ,
145+ userTimeZone :stringExposingStateControl ( "userTimeZone" , Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ) ,
149146 ...commonChildren ,
150147 ...formDataChildren ,
151148} ;
@@ -306,7 +303,7 @@ export const dateRangeControl = (function () {
306303const childrenMap = {
307304start :stringExposingStateControl ( "start" ) ,
308305end :stringExposingStateControl ( "end" ) ,
309- userRangeTimeZone :stringExposingStateControl ( "userRangeTimeZone" , 'Etc/UTC' ) ,
306+ userRangeTimeZone :stringExposingStateControl ( "userRangeTimeZone" , Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ) ,
310307 ...formDataChildren ,
311308 ...commonChildren ,
312309} ;
@@ -465,6 +462,14 @@ export const dateRangeControl = (function () {
465462. build ( ) ;
466463} ) ( ) ;
467464
465+ const getTimeZoneInfo = ( timeZone :any , othereTimeZone :any ) => {
466+ const tz = timeZone === 'UserChoice' ?othereTimeZone :timeZone ;
467+ return {
468+ TimeZone :tz ,
469+ Offset :dayjs ( ) . tz ( tz ) . format ( 'Z' ) // Get the UTC offset for the selected timezone
470+ } ;
471+ } ;
472+
468473export const DatePickerComp = withExposingConfigs ( datePickerControl , [
469474depsConfig ( {
470475name :"value" ,
@@ -478,10 +483,16 @@ export const DatePickerComp = withExposingConfigs(datePickerControl, [
478483depsConfig ( {
479484name :"formattedValue" ,
480485desc :trans ( "export.datePickerFormattedValueDesc" ) ,
481- depKeys :[ "value" , "format" ] ,
486+ depKeys :[ "value" , "format" , "timeZone" , "userTimeZone" ] ,
482487func :( input ) => {
483488const mom = Boolean ( input . value ) ?dayjs ( input . value , DateParser ) :null ;
484- return mom ?. isValid ( ) ?mom . format ( input . format ) :"" ;
489+ const tz = input . timeZone === 'UserChoice' ?input . userTimeZone :input . timeZone ; // Get the selected timezone
490+ const timeInTz = mom ?. clone ( ) . tz ( tz , true ) ; // Apply the selected timezone without altering the time itself (do not convert the time)
491+ return mom ?. isValid ( )
492+ ?( ! input . format || input . format . includes ( 'Z' ) || input . format . includes ( 'z' ) ) // Check if format is not available or contains 'Z'
493+ ?timeInTz ?. format ( input ?. format ) // Return formattedDateWithoffset if format includes 'Z' or is not available
494+ :mom . format ( input . format ) // Otherwise, return mom.format(input.format)
495+ :"" ;
485496} ,
486497} ) ,
487498depsConfig ( {
@@ -507,11 +518,8 @@ export const DatePickerComp = withExposingConfigs(datePickerControl, [
507518name :"timeZone" ,
508519desc :trans ( "export.timeZoneDesc" ) ,
509520depKeys :[ "timeZone" , "userTimeZone" ] ,
510- func :( input ) => {
511- console . log ( "input.timeZone" , input . timeZone )
512- return input . timeZone === 'UserChoice' ?input . userTimeZone :input . timeZone || 'UTC' ;
521+ func :( input :{ timeZone :any ; userTimeZone :any ; } ) => getTimeZoneInfo ( input . timeZone , input . userTimeZone )
513522
514- } ,
515523} ) ,
516524 ...CommonNameConfig ,
517525] ) ;
@@ -556,36 +564,58 @@ export let DateRangeComp = withExposingConfigs(dateRangeControl, [
556564depsConfig ( {
557565name :"formattedValue" ,
558566desc :trans ( "export.dateRangeFormattedValueDesc" ) ,
559- depKeys :[ "start" , "end" , "format" ] ,
567+ depKeys :[ "start" , "end" , "format" , "timeZone" , "userRangeTimeZone" ] ,
560568func :( input ) => {
561569const start = Boolean ( input . start ) ?dayjs ( input . start , DateParser ) :null ;
562570const end = Boolean ( input . end ) ?dayjs ( input . end , DateParser ) :null ;
571+ const tz = input . timeZone === 'UserChoice' ?input . userRangeTimeZone :input . timeZone ; // Get the selected timezone
572+ const startTimeInTz = start ?. clone ( ) . tz ( tz , true ) ; // Apply the selected timezone without altering the time itself (do not convert the time)
573+ const endTimeInTz = end ?. clone ( ) . tz ( tz , true ) ; // Apply the selected timezone without altering the time itself (do not convert the time)
574+
563575return [
564- start ?. isValid ( ) && start . format ( input . format ) ,
565- end ?. isValid ( ) && end . format ( input . format ) ,
576+ start ?. isValid ( ) && ( ! input . format || input . format . includes ( 'Z' ) || input . format . includes ( 'z' ) ) // Check if format is not available or contains 'Z'
577+ ?startTimeInTz ?. format ( input ?. format ) // Return formattedDateWithoffset if format includes 'Z' or is not available
578+ :start ?. format ( input . format ) ,
579+ end ?. isValid ( ) && ( ! input . format || input . format . includes ( 'Z' ) || input . format . includes ( 'z' ) ) // Check if format is not available or contains 'Z'
580+ ?endTimeInTz ?. format ( input ?. format ) // Return formattedDateWithoffset if format includes 'Z' or is not available
581+ :end ?. format ( input . format ) ,
566582]
567583. filter ( ( item ) => item )
568584. join ( " - " ) ;
569585} ,
570586} ) ,
571587depsConfig ( {
572- name :"formattedStartValue" ,
573- desc :trans ( "export.dateRangeFormattedStartValueDesc" ) ,
574- depKeys :[ "start" , "format" ] ,
575- func :( input ) => {
576- const start = Boolean ( input . start ) ?dayjs ( input . start , DateParser ) :null ;
577- return start ?. isValid ( ) && start . format ( input . format ) ;
578- } ,
579- } ) ,
588+ name :"formattedStartValue" ,
589+ desc :trans ( "export.dateRangeFormattedStartValueDesc" ) ,
590+ depKeys :[ "start" , "format" , "timeZone" , "userRangeTimeZone" ] ,
591+ func :( input ) => {
592+ const start = Boolean ( input . start ) ?dayjs ( input . start , DateParser ) :null ;
593+ const tz = input . timeZone === 'UserChoice' ?input . userRangeTimeZone :input . timeZone ;
594+ const startTimeInTz = start ?. clone ( ) . tz ( tz , true ) ;
595+ return start ?. isValid ( ) && ( ! input . format || input . format . includes ( 'Z' ) || input . format . includes ( 'z' ) )
596+ ?startTimeInTz ?. format ( input ?. format )
597+ :start ?. format ( input . format ) ;
598+ } ,
599+ } ) ,
580600depsConfig ( {
581601name :"formattedEndValue" ,
582602desc :trans ( "export.dateRangeFormattedEndValueDesc" ) ,
583- depKeys :[ "end" , "format" ] ,
603+ depKeys :[ "end" , "format" , "timeZone" , "userRangeTimeZone" ] ,
584604func :( input ) => {
585605const end = Boolean ( input . end ) ?dayjs ( input . end , DateParser ) :null ;
586- return end ?. isValid ( ) && end . format ( input . format ) ;
606+ const tz = input . timeZone === 'UserChoice' ?input . userRangeTimeZone :input . timeZone ;
607+ const endTimeInTz = end ?. clone ( ) . tz ( tz , true ) ;
608+ return end ?. isValid ( ) && ( ! input . format || input . format . includes ( 'Z' ) || input . format . includes ( 'z' ) )
609+ ?endTimeInTz ?. format ( input ?. format )
610+ :end ?. format ( input . format ) ;
587611} ,
588612} ) ,
613+ depsConfig ( {
614+ name :"timeZone" ,
615+ desc :trans ( "export.timeZoneDesc" ) ,
616+ depKeys :[ "timeZone" , "userRangeTimeZone" ] ,
617+ func :( input :any ) => getTimeZoneInfo ( input . timeZone , input . userRangeTimeZone )
618+ } ) ,
589619depsConfig ( {
590620name :"invalid" ,
591621desc :trans ( "export.invalidDesc" ) ,