1
1
import _ , { noop } from "lodash" ;
2
2
import dayjs from "dayjs" ;
3
- import utc from 'dayjs/plugin/utc' ;
4
3
import { RecordConstructorToComp , RecordConstructorToView } from "lowcoder-core" ;
5
4
import {
6
5
BoolCodeControl ,
@@ -53,8 +52,6 @@ import { dropdownControl } from "comps/controls/dropdownControl";
53
52
import { timeZoneOptions } from "./timeZone" ;
54
53
55
54
56
- dayjs . extend ( utc ) ;
57
-
58
55
59
56
const EventOptions = [ changeEvent , focusEvent , blurEvent ] as const ;
60
57
@@ -87,7 +84,7 @@ const commonChildren = {
87
84
...validationChildren ,
88
85
viewRef : RefControl < CommonPickerMethods > ,
89
86
inputFieldStyle : styleControl ( DateTimeStyle , 'inputFieldStyle' ) ,
90
- timeZone : dropdownControl ( timeZoneOptions , "Etc/UTC" ) ,
87
+ timeZone : dropdownControl ( timeZoneOptions , Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ) ,
91
88
} ;
92
89
type CommonChildrenType = RecordConstructorToComp < typeof commonChildren > ;
93
90
@@ -145,7 +142,7 @@ function validate(
145
142
146
143
const childrenMap = {
147
144
value : stringExposingStateControl ( "value" ) ,
148
- userTimeZone : stringExposingStateControl ( "userTimeZone" , 'Etc/UTC' ) ,
145
+ userTimeZone : stringExposingStateControl ( "userTimeZone" , Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ) ,
149
146
...commonChildren ,
150
147
...formDataChildren ,
151
148
} ;
@@ -306,7 +303,7 @@ export const dateRangeControl = (function () {
306
303
const childrenMap = {
307
304
start : stringExposingStateControl ( "start" ) ,
308
305
end : stringExposingStateControl ( "end" ) ,
309
- userRangeTimeZone : stringExposingStateControl ( "userRangeTimeZone" , 'Etc/UTC' ) ,
306
+ userRangeTimeZone : stringExposingStateControl ( "userRangeTimeZone" , Intl . DateTimeFormat ( ) . resolvedOptions ( ) . timeZone ) ,
310
307
...formDataChildren ,
311
308
...commonChildren ,
312
309
} ;
@@ -465,6 +462,14 @@ export const dateRangeControl = (function () {
465
462
. build ( ) ;
466
463
} ) ( ) ;
467
464
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
+
468
473
export const DatePickerComp = withExposingConfigs ( datePickerControl , [
469
474
depsConfig ( {
470
475
name : "value" ,
@@ -478,10 +483,16 @@ export const DatePickerComp = withExposingConfigs(datePickerControl, [
478
483
depsConfig ( {
479
484
name : "formattedValue" ,
480
485
desc : trans ( "export.datePickerFormattedValueDesc" ) ,
481
- depKeys : [ "value" , "format" ] ,
486
+ depKeys : [ "value" , "format" , "timeZone" , "userTimeZone" ] ,
482
487
func : ( input ) => {
483
488
const 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
+ : "" ;
485
496
} ,
486
497
} ) ,
487
498
depsConfig ( {
@@ -507,11 +518,8 @@ export const DatePickerComp = withExposingConfigs(datePickerControl, [
507
518
name : "timeZone" ,
508
519
desc : trans ( "export.timeZoneDesc" ) ,
509
520
depKeys : [ "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 )
513
522
514
- } ,
515
523
} ) ,
516
524
...CommonNameConfig ,
517
525
] ) ;
@@ -556,36 +564,58 @@ export let DateRangeComp = withExposingConfigs(dateRangeControl, [
556
564
depsConfig ( {
557
565
name : "formattedValue" ,
558
566
desc : trans ( "export.dateRangeFormattedValueDesc" ) ,
559
- depKeys : [ "start" , "end" , "format" ] ,
567
+ depKeys : [ "start" , "end" , "format" , "timeZone" , "userRangeTimeZone" ] ,
560
568
func : ( input ) => {
561
569
const start = Boolean ( input . start ) ? dayjs ( input . start , DateParser ) : null ;
562
570
const 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
+
563
575
return [
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 ) ,
566
582
]
567
583
. filter ( ( item ) => item )
568
584
. join ( " - " ) ;
569
585
} ,
570
586
} ) ,
571
587
depsConfig ( {
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
+ } ) ,
580
600
depsConfig ( {
581
601
name : "formattedEndValue" ,
582
602
desc : trans ( "export.dateRangeFormattedEndValueDesc" ) ,
583
- depKeys : [ "end" , "format" ] ,
603
+ depKeys : [ "end" , "format" , "timeZone" , "userRangeTimeZone" ] ,
584
604
func : ( input ) => {
585
605
const 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 ) ;
587
611
} ,
588
612
} ) ,
613
+ depsConfig ( {
614
+ name : "timeZone" ,
615
+ desc : trans ( "export.timeZoneDesc" ) ,
616
+ depKeys : [ "timeZone" , "userRangeTimeZone" ] ,
617
+ func : ( input :any ) => getTimeZoneInfo ( input . timeZone , input . userRangeTimeZone )
618
+ } ) ,
589
619
depsConfig ( {
590
620
name : "invalid" ,
591
621
desc : trans ( "export.invalidDesc" ) ,
0 commit comments