Merge pull request #3676 from mempool/mononaut/better-durations
Improve unit selection for duration formatting
This commit is contained in:
		
						commit
						3e6c672a1e
					
				| @ -37,7 +37,7 @@ | |||||||
|         <div class="difficulty-stats"> |         <div class="difficulty-stats"> | ||||||
|           <div class="item"> |           <div class="item"> | ||||||
|             <div class="card-text"> |             <div class="card-text"> | ||||||
|               ~<app-time [time]="epochData.timeAvg / 1000" [forceFloorOnTimeIntervals]="['minute']" [fractionDigits]="1"></app-time> |               ~<app-time [time]="epochData.timeAvg / 1000" [fractionDigits]="1"></app-time> | ||||||
|             </div> |             </div> | ||||||
|             <div class="symbol" i18n="difficulty-box.average-block-time">Average block time</div> |             <div class="symbol" i18n="difficulty-box.average-block-time">Average block time</div> | ||||||
|           </div> |           </div> | ||||||
|  | |||||||
| @ -26,7 +26,7 @@ | |||||||
|                 <app-time kind="until" [time]="(1 * i) + now + 61000" [fastRender]="false" [fixedRender]="true"></app-time> |                 <app-time kind="until" [time]="(1 * i) + now + 61000" [fastRender]="false" [fixedRender]="true"></app-time> | ||||||
|               </ng-template> |               </ng-template> | ||||||
|               <ng-template #timeDiffMainnet> |               <ng-template #timeDiffMainnet> | ||||||
|                 <app-time kind="until" [time]="da.timeAvg * (i + 1) + now + da.timeOffset" [fastRender]="false" [fixedRender]="true" [forceFloorOnTimeIntervals]="['hour']"></app-time> |                 <app-time kind="until" [time]="da.timeAvg * (i + 1) + now + da.timeOffset" [fastRender]="false" [fixedRender]="true"></app-time> | ||||||
|               </ng-template> |               </ng-template> | ||||||
|             </div> |             </div> | ||||||
|             <ng-template #mergedBlock> |             <ng-template #mergedBlock> | ||||||
|  | |||||||
| @ -18,7 +18,6 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { | |||||||
|   @Input() fastRender = false; |   @Input() fastRender = false; | ||||||
|   @Input() fixedRender = false; |   @Input() fixedRender = false; | ||||||
|   @Input() relative = false; |   @Input() relative = false; | ||||||
|   @Input() forceFloorOnTimeIntervals: string[]; |  | ||||||
|   @Input() fractionDigits: number = 0; |   @Input() fractionDigits: number = 0; | ||||||
| 
 | 
 | ||||||
|   constructor( |   constructor( | ||||||
| @ -84,21 +83,17 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { | |||||||
| 
 | 
 | ||||||
|     let counter: number; |     let counter: number; | ||||||
|     for (const i in this.intervals) { |     for (const i in this.intervals) { | ||||||
|       if (this.kind !== 'until' || this.forceFloorOnTimeIntervals && this.forceFloorOnTimeIntervals.indexOf(i) > -1) { |  | ||||||
|       counter = Math.floor(seconds / this.intervals[i]); |       counter = Math.floor(seconds / this.intervals[i]); | ||||||
|       } else { |       if (counter > 0) { | ||||||
|         counter = Math.round(seconds / this.intervals[i]); |         let rounded = Math.round(seconds / this.intervals[i]); | ||||||
|       } |  | ||||||
|       let rounded = counter; |  | ||||||
|         if (this.fractionDigits) { |         if (this.fractionDigits) { | ||||||
|           const roundFactor = Math.pow(10,this.fractionDigits); |           const roundFactor = Math.pow(10,this.fractionDigits); | ||||||
|           rounded = Math.round((seconds / this.intervals[i]) * roundFactor) / roundFactor; |           rounded = Math.round((seconds / this.intervals[i]) * roundFactor) / roundFactor; | ||||||
|         } |         } | ||||||
|         const dateStrings = dates(rounded); |         const dateStrings = dates(rounded); | ||||||
|       if (counter > 0) { |  | ||||||
|         switch (this.kind) { |         switch (this.kind) { | ||||||
|           case 'since': |           case 'since': | ||||||
|             if (counter === 1) { |             if (rounded === 1) { | ||||||
|               switch (i) { // singular (1 day)
 |               switch (i) { // singular (1 day)
 | ||||||
|                 case 'year': return $localize`:@@time-since:${dateStrings.i18nYear}:DATE: ago`; break; |                 case 'year': return $localize`:@@time-since:${dateStrings.i18nYear}:DATE: ago`; break; | ||||||
|                 case 'month': return $localize`:@@time-since:${dateStrings.i18nMonth}:DATE: ago`; break; |                 case 'month': return $localize`:@@time-since:${dateStrings.i18nMonth}:DATE: ago`; break; | ||||||
| @ -121,7 +116,7 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { | |||||||
|             } |             } | ||||||
|             break; |             break; | ||||||
|           case 'until': |           case 'until': | ||||||
|             if (counter === 1) { |             if (rounded === 1) { | ||||||
|               switch (i) { // singular (In ~1 day)
 |               switch (i) { // singular (In ~1 day)
 | ||||||
|                 case 'year': return $localize`:@@time-until:In ~${dateStrings.i18nYear}:DATE:`; break; |                 case 'year': return $localize`:@@time-until:In ~${dateStrings.i18nYear}:DATE:`; break; | ||||||
|                 case 'month': return $localize`:@@time-until:In ~${dateStrings.i18nMonth}:DATE:`; break; |                 case 'month': return $localize`:@@time-until:In ~${dateStrings.i18nMonth}:DATE:`; break; | ||||||
| @ -144,7 +139,7 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { | |||||||
|             } |             } | ||||||
|             break; |             break; | ||||||
|           case 'span': |           case 'span': | ||||||
|             if (counter === 1) { |             if (rounded === 1) { | ||||||
|               switch (i) { // singular (1 day)
 |               switch (i) { // singular (1 day)
 | ||||||
|                 case 'year': return $localize`:@@time-span:After ${dateStrings.i18nYear}:DATE:`; break; |                 case 'year': return $localize`:@@time-span:After ${dateStrings.i18nYear}:DATE:`; break; | ||||||
|                 case 'month': return $localize`:@@time-span:After ${dateStrings.i18nMonth}:DATE:`; break; |                 case 'month': return $localize`:@@time-span:After ${dateStrings.i18nMonth}:DATE:`; break; | ||||||
| @ -167,7 +162,7 @@ export class TimeComponent implements OnInit, OnChanges, OnDestroy { | |||||||
|             } |             } | ||||||
|             break; |             break; | ||||||
|           default: |           default: | ||||||
|             if (counter === 1) { |             if (rounded === 1) { | ||||||
|               switch (i) { // singular (1 day)
 |               switch (i) { // singular (1 day)
 | ||||||
|                 case 'year': return dateStrings.i18nYear; break; |                 case 'year': return dateStrings.i18nYear; break; | ||||||
|                 case 'month': return dateStrings.i18nMonth; break; |                 case 'month': return dateStrings.i18nMonth; break; | ||||||
|  | |||||||
| @ -109,10 +109,10 @@ | |||||||
|                       </ng-template> |                       </ng-template> | ||||||
|                       <ng-template #belowBlockLimit> |                       <ng-template #belowBlockLimit> | ||||||
|                         <ng-template [ngIf]="network === 'liquid' || network === 'liquidtestnet'" [ngIfElse]="timeEstimateDefault"> |                         <ng-template [ngIf]="network === 'liquid' || network === 'liquidtestnet'" [ngIfElse]="timeEstimateDefault"> | ||||||
|                           <app-time kind="until" [time]="(60 * 1000 * this.mempoolPosition.block) + now" [fastRender]="false" [fixedRender]="true" [forceFloorOnTimeIntervals]="['hour']"></app-time> |                           <app-time kind="until" [time]="(60 * 1000 * this.mempoolPosition.block) + now" [fastRender]="false" [fixedRender]="true"></app-time> | ||||||
|                         </ng-template> |                         </ng-template> | ||||||
|                         <ng-template #timeEstimateDefault> |                         <ng-template #timeEstimateDefault> | ||||||
|                           <app-time kind="until" *ngIf="(timeAvg$ | async) as timeAvg;" [time]="(timeAvg * this.mempoolPosition.block) + now + timeAvg" [fastRender]="false" [fixedRender]="true" [forceFloorOnTimeIntervals]="['hour']"></app-time> |                           <app-time kind="until" *ngIf="(timeAvg$ | async) as timeAvg;" [time]="(timeAvg * this.mempoolPosition.block) + now + timeAvg" [fastRender]="false" [fixedRender]="true"></app-time> | ||||||
|                         </ng-template> |                         </ng-template> | ||||||
|                       </ng-template> |                       </ng-template> | ||||||
|                     </ng-template> |                     </ng-template> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user