Swift timeInterval formatter - macos

I can get the date in de date picker, calculate the time left and put in a label. This gives seconds:
let timeLeft = datePicker.dateValue.timeIntervalSinceNow
countingLabel.stringValue = "seconds left: \(timeLeft)"
How to format "timeLeft"?
I tried this and it does not work:
var formatter = NSDateFormatter()
formatter.dateFormat = "EEEE, yyyy-MM-dd hh:mm:ss"
let timeLeft2 = formatter.stringFromDate(timeLeft)

You can use NSDateComponentsFormatter. From the
documentation:
An NSDateComponentsFormatter object takes quantities of time and
formats them as a user-readable string. Use a date components
formatter to create strings for your app’s interface.
Example:
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full
formatter.allowedUnits = .CalendarUnitSecond | .CalendarUnitMinute | .CalendarUnitHour
let pickedDate = datePicker.dateValue
let now = NSDate()
let formattedTimeLeft = formatter.stringFromDate(now, toDate: pickedDate)!
// Example result : 1:06:50
There are some more output formats available, e.g.
.Abbreviated : 1h 6m 50s
.Short : 1 hr, 6 min, 50 secs
.Full : 1 hour, 6 minutes and 50 seconds
Update for Swift 4 and later:
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full // or .short or .abbreviated
formatter.allowedUnits = [.second, .minute, .hour]
let pickedDate = datePicker.date
let now = Date()
let formattedTimeLeft = formatter.string(from: now, to: pickedDate)!

To get timeLeft you should use two time components and get their differnece as follow:
let todayComponents = NSCalendar.currentCalendar().components(.DayCalendarUnit | .MonthCalendarUnit | .YearCalendarUnit | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | , fromDate : NSDate())
var pickedDateComponents = NSCalendar.currentCalendar().components(.DayCalendarUnit | .MonthCalendarUnit | .YearCalendarUnit | .CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | , fromDate : datePicker.dateValue)
pickedDateComponents.second = pickedDateComponents.second - todayComponents.second
pickedDateComponents.minute = pickedDateComponents.minute - todayComponents.minute
pickedDateComponents.hour = pickedDateComponents.hour - todayComponents.hour
pickedDateComponents.day =pickedDateComponents.day - todayComponents.day
pickedDateComponents.month = pickedDateComponents.month - todayComponents.month
pickedDateComponents.year = pickedDateComponents.year - todayComponents.year
let timeLeft = NSCalendar.currentCalendar().dateFromComponents(pickedDateComponents)!

Related

Get HRIS hours worked data to calculate hours worked - hours on lunch, to get a total of hours worked in a day, in Power Bi

I am trying to get my power bi data from my HRIS platform to calculate hours worked - hours on lunch, to get a total of hours worked in a day.
I have tried grouping, pivots, formulas, and because my HRIS platform send the "check-in" time at the start of a shift and the "check-in" time when they clock back in from lunch, as the same "check-in" classification, I cannot associate it with one or the other, it picks the most recent one, if I say return "check-in" (My data set is intraday and updates every hour so I can get all Clock events for the current day)
I have included an example of what my data looks like right from the HRIS into Power Bi
Sample Data Set
Full Data Sheet Columns
Formula's Used in Order:
Check In =
IF(AND(Merge1[Current Day]=Merge1[Day of the
Week],Merge1[Clock_Event_Type]="Check-in"),
Merge1[Clock_Event_Time_-_No_Time_Zone])
Check Out (Lunch) =
IF(Merge1[Day of the Week]= Merge1[Current Day],
IF(Merge1[Clock_Event_Type]= "Check-out
(meal)",Merge1[Clock_Event_Time_-_No_Time_Zone],BLANK()))
Check in (Lunch) = IF(Merge1[Current Day]=Merge1[Day of the
Week],
VAR checkin = Merge1[Check In]
VAR outlunch = Merge1[Check Out (Lunch)]
VAR timeinfromlunch = IF(checkin>outlunch,checkin)
return timeinfromlunch)
Check Out = IF(Merge1[Day of the Week]= Merge1[Current Day],
IF(Merge1[Clock_Event_Type] = "Check-out",
Merge1[Clock_Event_Time_-_No_Time_Zone]),BLANK())
Current Time = IF(AND(Merge1[Current Day]=Merge1[Day of the
Week],Merge1[Clock_Event_Type]="Check-in"),
NOW()
)
CPH Calc (Intraday Shift) = IF(Merge1[Current Time]<Merge1[Shift
End Today],Merge1[Current Time],Merge1[Check Out])
Hours Worked =
IF(AND(Merge1[Current Day]=Merge1[Day of the
Week],Merge1[Clock_Event_Type]="Check-in"),
VAR totalsecondsinbreak = DATEDIFF(Merge1[Check Out
(Lunch)],Merge1[Check in (Lunch)],SECOND)
VAR total_secondsworking = DATEDIFF(Merge1[Check In],Merge1[CPH
Calc (Intraday Shift)],SECOND)
VAR totalseconds = total_secondsworking-totalsecondsinbreak
VAR DAXHours = (totalseconds/60)/60
RETURN IF(Merge1[Clock_Event_Time_-
_No_Time_Zone]=0,BLANK(),DAXHours))
Shift Start Today = IF(Merge1[Current Day]=Merge1[Day of the
Week],TODAY() + Merge1[Start Shift])
Shift End Today = IF(Merge1[Current Day]=Merge1[Day of the
Week],TODAY() + Merge1[End Shift])
On Queue (Milliseconds) = IF(AND(Merge1[Current Day]=Merge1[Day
of the Week],Merge1[Clock_Event_Type]="Check-in"),
VAR onqueueseconds = RELATED('Table (2)'[On Queue])
VAR milliseconds = onqueueseconds
VAR total_second = milliseconds/1000
VAR minutes = total_second/60
VAR hours_decimal = minutes/60
VAR hours = INT(hours_decimal)
return onqueueseconds)
Utilization =
IF(AND(Merge1[Current Day]=Merge1[Day of the
Week],Merge1[Clock_Event_Type]="Check-in"),
VAR productivetime = Merge1[Hours Productive Time]
VAR workedtime = Merge1[Hours Worked]
VAR Ult = IFERROR(productivetime/workedtime,BLANK())
return Ult
)
Hours Productive Time =
IF(AND(Merge1[Current Day]=Merge1[Day of the
Week],Merge1[Clock_Event_Type]="Check-in"),
VAR milliseconds = Merge1[On Queue (Milliseconds)]
VAR total_second = milliseconds/1000
//VAR minutes = total_second/60
//VAR hours_decimal = minutes/60
//VAR hours = INT(hours_decimal)
VAR DAXHours = (total_second/60)/60
return DAXHours+.5)

WooCommerce Bookings AJAX codes for selection of dates

My current project need to customisely showing some extra information base on calculations of the dates that has been selected. I have writen a function, and I can't find the AJAX code that the booking system returns the data and calculations base on the valid date to trigger my function.
My function is easy:
function calculate_date_duration() {
var year_1 = document.querySelector('.selection-start-date').getAttribute('data-year');
var month_1 = document.querySelector('.selection-start-date').getAttribute('data-month');
var date_1 = document.querySelector('.selection-start-date a').textContent;
var year_2 = document.querySelector('.selection-end-date').getAttribute('data-year');
var month_2 = document.querySelector('.selection-end-date').getAttribute('data-month');
var date_2 = document.querySelector('.selection-end-date a').textContent;
var day_1 = new Date(year_1, month_1, date_1);
var day_2 = new Date(year_2, month_2, date_2);
console.log(day_1);
console.log(day_2);
var day_selected = day_2.getTime() - day_1.getTime();
var date_duration = parseInt(day_selected / (1000 * 60 * 60 * 24)) +1;
console.log(date_duration);
var display_pane = document.querySelector('.wc-bookings-booking-cost');
display_pane.innerHTML = display_pane.innerHTML + '<br>Total booking cost:<strong><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">$</span>' + (date_duration*120) + '</bdi></span></strong>';}
The date select duration ajax is in wp-content/plugins/woocommerce-bookings/dist/frontend.js, search success, the last one in this file is the codes after validating the selected duration.

Typoscript image change every 15 days

I would like to make a change of image say every 15 days. Or twice a month. When the date is between the 1 and 15 is an image, and if it is from 16 to 30 another, so on, are 24 images in the year. I would like it to be the typoscript that manages the change of the image.
I took the following typosript:
lib.headerlogo1 = COA
lib.headerlogo1 {
10 = LOAD_REGISTER
10 {
divSem.cObject = TEXT
divSem.cObject {
data = date:U
strftime = %U
current = 1
setCurrent.data = date:U
setCurrent.wrap = |/2
prioriCalc = 1
}
}
20 = FILES
20 {
references {
data = levelmedia: -1, slide
}
renderObj = IMAGE
renderObj {
file.import.dataWrap = {file:current:storage}:{file:current:identifier}
#file.import.listNum = 0
altText.data = file:current:title
# Affiche bien la valeur de : divSem
#stdWrap.insertData = 1
#stdWrap.wrap = <div class="banner{register:divSem}">|</div>
}
# insertData = 1
insertData = 1
# IT'S FAILLED !!
begin = {register:divSem}
maxItems = 1
}
30 = TEXT
30 {
stdWrap.insertData = 1
stdWrap.wrap = <div class="{register:divSem}">|</div>
}
}
The problem is that I can not start the value of the registry begin = {register:divSem} ... It always starts at 0! Do you have an idea ? The display of the registers in 30 = TEXT is correct.
Do you have a good idea to modify the typoscript?
I just found the solution, instead of begin = {register: divSem}, I did this:
begin.cObject = TEXT
begin.cObject {
value = 0
value.override.cObject = CASE
value.override.cObject {
key.data = register:divSem
1 = TEXT
1.value = 1
2 = TEXT
2.value = 2
...
24 = TEXT
24.value = 24
default = TEXT
default.value = 2
}
}
Maybe there is more simple, if you have an idea, I'm interested.
Best regards.
You found the important detail: you need a .cObject to fill in any data in the simple property.
Why so complicated with a CASE that outputs the same as the key?
So the simplest way would be:
begin.cObject = TEXT
begin.cObject.data = register:divSem
maybe this also worked like you do in .30:
begin = {register:divSem}
begin.insertData = 1
and a more direct way of your .30:
instead of an .insertData for a .wrap use .dataWrap
begin.stdWrap.dataWrap = {register:divSem}
:
30 = TEXT
30.dataWrap = <div class="{register:divSem}">|</div>

Converting German Time Format to English Format

Good morning together,
I have an app with iOS 9 and Swift 2
At the moment, my app is in German language.
My goal is: I will translate it in English.
This works good, but I have a problem with my time format.
At the moment, I use a 24 time format like this:
09:00 Uhr
17:00 Uhr
23:00 Uhr
...
Is there a quick way to convert this in the English 12h format, like this:
09:00 Uhr => 09:00 AM
17:00 Uhr => 05:00 PM
23:00 Uhr => 11:00 PM
...
Thank you very much.
You could use NSDateFormatter to modify
let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm"
let date = formatter.dateFromString(yourString)
formatter.dateFormat = "hh:mm AA"
let result = formatter.stringFromDate(date)
You my propably need to get rid of the "Uhr" before (you can replace " Uhr" with "")
Try this one, it should do exactly what you want:
let date = NSDate()
let dateFormetter = NSDateFormatter()
dateFormetter.dateFormat = "h:mm a"
dateFormetter.timeZone = NSTimeZone(name: "UTC")
dateFormetter.locale = NSLocale(localeIdentifier: "en_GB")
let timeString = dateFormetter.stringFromDate(date) // 7:34 a.m.
As per the other two answers, use NSDateFormatter:
let dateFormatIn = NSDateFormatter()
let dateFormatOut = NSDateFormatter()
dateFormatIn.dateFormat = "HH:mm 'Uhr'"
dateFormatOut.dateFormat = "hh:mm a"
dateFormatOut.locale = NSLocale(localeIdentifier: "en_US")
var str = "07:00 Uhr"
func convertTimeRepresentation(str: String) -> String? {
if let n = dateFormatIn.dateFromString(str) {
return dateFormatOut.stringFromDate(n)
}
return nil
}
convertTimeRepresentation(str)

Xpages view string type ratio

I have a view with 2 columns, both type of String. One is always "08:00" and one depends on the user input. It could be 02:30 or 13:45, etc...
I want to convert all the column of 08:00's and all the other column of values, add them together and then divide the total...
Example:
Column 1 | Column 2
02:50 | 08:00
05:15 | 08:00
15:25 | 08:00
03:15 | 08:00
It would be something like:(~26)/(8*4)=~0.8%.
It's a timekeeping application. I have the normal-time of 8 hours and the time the employee worked, and I want to add a button that will calculate the ratio between the 2 columns.
Thank you,
Florin
This is the code I used to calculate the ration between 2 columns of strings:
var vec:NotesViewEntryCollection = viewSearch.getAllEntries();
var c = vec.getCount()
var data = #DbColumn(#DbName(),"vwSearch",4);
var totalH = 0;
var totalM = 0;
for(var i=0;i<c;i++){
var str = data[i].toString();
var a = str.split(":",2)[0];
var b = str.split(":",2)[1];
var hour = parseInt(a);
var minutes = parseInt(b);
totalH += hour;
totalM += minutes;
if(totalM >= 60)
{
totalH++;
totalM-=60;
}
}
var h_necesare = c*8;
getComponent("h_lucrate").setValue(totalH+" ore, si "+totalM+" minute.");
getComponent("h_necesare").setValue(h_necesare);
getComponent("raport").setValue(totalH-h_necesare);

Resources