Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have checked the other suggestions for fixing this problem and they don't work.
The current code seems to work until you enter a different date and then I get random failures like below.
The code is as follows:
yy, mm, dd = 11, 27, 2019
s_yy, s_mm, s_dd = 11, 1, 2019
e_yy, e_mm, e_dd = 1, 1, 2020
input := fmt.Sprintf("%d-%d-%d", yy, mm, dd)
input += "T15:04:05.000-07:00"
t, _ := time.Parse("2006-01-02T15:04:05.000-07:00", input)
input_s := fmt.Sprintf("%d-%d-%d", s_yy, s_mm, s_dd)
input_s += "T15:04:05.000-07:00"
t_s, _ := time.Parse("2006-01-02T15:04:05.000-07:00", input_s)
input_e := fmt.Sprintf("%d-%d-%d", e_yy, e_mm, e_dd)
input_e += "T15:04:05.000-07:00"
t_e, _ := time.Parse("2006-01-02T15:04:05.000-07:00", input_e)
fmt.Println("t = ", t, " t_s = ", t_s, " t_e", t_e)
The result is the following:
t = 2019-12-27 15:04:05 -0700 -0700 t_s = 0001-01-01 00:00:00 +0000 UTC t_e 0001-01-01 00:00:00 +0000 UTC
Any Help would be a help
Thanks in advance.
You got problems in your code.
The order of your variable is wrong.
yy, mm, dd = 11, 27, 2019 should be yy, mm, dd = 2019, 11, 27.
Don't ignore the error. If you got problem, just print it will be a lot of help (or better is writing a test)
Your format is wrong. It should in form like fmt.Sprintf("%d-%02d-%02d", yy, mm, dd)
You can check the result here
Related
So, basically a person has a shift, with a start time and duration, and I need to know how many minutes to go.
I retrieve from the database (mySQL) a standard string of datetime for the start of the shift (eg. 2022-01-01 09:00:00)
I also retrieve the number of hours of the shift (eg. 8)
I can then determine current time, using Golang's Time.time, but this format is different.
Please see code below, which explains the problem better, and gives the pieces missing.
Thanks!
var shiftStartDB string // For example 2022-01-01 09:00:00
var shiftStartUnix int // For example, start time converted to minutes since epoch
var offTimer int // Number of hours for the shift
var shiftEndDB string // For example 2022-01-01 17:00:00
var shiftEndUnix int // For example, end time converted to minutes since epoch
var nowTime Time.time // Golangs version of time for now
var nowUnix int // Golang now converted to Unix time
var templateID string
var minsToGo int
_ = db.QueryRow("SELECT LastSignedOn, OffTimer, TemplateID FROM assets WHERE ID = ?", assetid).Scan(&shiftStartDB, &offTimer, &templateID)
shiftStartUnix = <convert database time into unix time>
if offTimerTemp == 0 {
_ = db.QueryRow("OffTimer FROM templates WHERE ID = ?", templateID).Scan(&offTimer)
}
shiftEndUnix = shiftStartUnix + (offTimer * 60)
nowTime = time.Now()
nowUnix = <convert golang time into unix time>
minsToGo = shiftEndInt - nowInt
You can use nowUnix = nowTime.UNIX()/60, as Time.UNIX returns the number of seconds elapsed since epoch. that said, this might be easier to deal with if you parsed the times and used go library time functions directly:
shiftStartTime,err := time.Parse("2006-02-01 15:04:05",dbTime)
shiftEnd=shiftStartTime.Add(time.Hour*offTimer)
minsToGo:=shiftEnd.Sub(time.Now()).Minutes()
How do i convert millisecond (uint64) into Time Format RFC3999 with millisecond (string) in GO?
For example:
var milleSecond int64
milleSecond = 1645286399999 //My Local Time : Sat Feb 19 2022 23:59:59
var loc = time.FixedZone("UTC-4", -4*3600)
string1 := time.UnixMilli(end).In(loc).Format(time.RFC3339)
Actual Result: 2022-02-19T11:59:59-04:00
Expected Result(should be): 2022-02-19T11:59:59.999-04:00
You are asking for an RFC3339 formatted string, with seconds reported to the nearest millisecond. There's no format string in the time package for this (only with whole seconds and nanosecond accuracy), but you can make your own.
Here's the string for seconds to the nearest nanosecond, copied from the standard library:
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
You can make a millisecond version of this easily enough by removing the .999999999 (report time to the nearest nanosecond, removing trailing zeros) to .000 (report time to the nearest millisecond, don't remove trailing zeros). This format is documented under time.Layout in the package docs https://pkg.go.dev/time#pkg-constants:
RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
Code (playground link):
package main
import (
"fmt"
"time"
)
const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
func main() {
ms := int64(1645286399999) //My Local Time : Sat Feb 19 2022 23:59:59
var loc = time.FixedZone("UTC-4", -4*3600)
fmt.Println(time.UnixMilli(ms).In(loc).Format(RFC3339Milli))
}
Output:
2022-02-19T11:59:59.999-04:00
I want to add several months or years to a given date in milliseconds in JSONata, so that it can do something like today plus three months. I didn't find any function to do that job in JSONata, so I implemented a function on my own. I calculated one month in milliseconds and multiply it with the amount of months I want to add. The following code shows the function in action.
(
$addMonths := function($time, $months) {
2628000000 * $months + $time
};
{
"datePlus3Month": $addMonths($millis(), 3)
}
)
This seems to work, but I don't know how accurate it is, because every month hasn't the same amount of days. So is there a better solution to achieve a more accurate result?
I just had the same problem. My solution seems rather clunky to me, but it works. Most likely there is a much easier way.
$addMonths := function($dateString, $addMonths) {(
$date := $dateString ~> $toMillis();
$newYear := ($date ~> $fromMillis("[Y,4]") ~> $number());
$newMonth := ($date ~> $fromMillis("[M]") ~> $number()) + $addMonths;
$newDay := ($date ~> $fromMillis("[D]") ~> $number());
$newMonth > 12
? $newYear := $newYear + 1;
$newMonth > 12
? $newMonth := $newMonth - 12;
$lastDay := (
$newMonth in [1,3,5,7,8,10,12]
? 31
: $newMonth in [4,6,9,11]
? 30
: ( $newYear%4=0
and
($newYear%100 != 0 or $newYear%400 = 0)
? 29
: 28)
);
$newDay > $lastDay
? $newDay := $lastDay;
( $newYear
& "-"
& $newMonth
& "-"
& $newDay
) ~> $toMillis("[Y]-[M]-[D]")
~> $fromMillis()
)};
You could try to transform into milliseconds, add what you need and then transform back.
$fromMillis($toMillis("2023-02-02") + 6.307e+10);
This results in: "2025-01-31T23:26:40.000Z"
Go noob here, and all I want to do is use the time format constants list https://golang.org/src/time/format.go that are mentioned in 3 posts here on SO (https://stackoverflow.com/a/20234207 https://stackoverflow.com/a/14106561 https://stackoverflow.com/a/20234207). None of which including the docs (at least that I can tell) have an example of how to use them.
I would expect this to work (but it clearly does not):
t := time.Now()
log.Println(t.stdHour12())
Can you please tell me how to get only the hour (in 12 hour time) for a given time t (ex: 10 from 2021-03-09 22:45:04.009063861 -0500 EST)?
const (
stdLongMonth = "January"
stdMonth = "Jan"
stdNumMonth = "1"
stdZeroMonth = "01"
stdLongWeekDay = "Monday"
stdWeekDay = "Mon"
stdDay = "2"
stdUnderDay = "_2"
stdZeroDay = "02"
stdHour = "15"
stdHour12 = "3"
stdZeroHour12 = "03"
stdMinute = "4"
stdZeroMinute = "04"
stdSecond = "5"
stdZeroSecond = "05"
stdLongYear = "2006"
stdYear = "06"
stdPM = "PM"
stdpm = "pm"
stdTZ = "MST"
stdISO8601TZ = "Z0700" // prints Z for UTC
stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
stdNumTZ = "-0700" // always numeric
stdNumShortTZ = "-07" // always numeric
stdNumColonTZ = "-07:00" // always numeric
)
Thanks in advance!
EDIT: From the answers received so far, I see that I cannot use the constants above to achieve what I want so I changed the wording of this question to specifically ask to return the hour (and just the hour) for a given time.
The Time object specifies the full date and time. You can extract just the time, if you like:
func main() {
t := time.Now()
fmt.Println(t.Format(time.Kitchen))
}
time.Kitchen is defined in the time package as Kitchen = "3:04PM"
If you want to understand how the format is interpreted, read this piece of documentation carefully
If you just need the hour, call the Hour() method on a Time object. If you want it in 12-hour format, you can just do modulo 12:
func main() {
t := time.Now()
fmt.Println(t.Hour())
fmt.Println(t.Hour() % 12)
}
These are constants representing tokens used internally by formatting code in time package (note they start with lower letter so they aren't exported and you can't even use them outside time package).
If you want to come up with your own format in Go (for both parsing and output) you simply define it using these tokens "as example" and Format() will parse it and apply that format (if valid) to itself.
const (
MyLayout = "3"
)
func main() {
t := time.Now()
fmt.Println(t.Format(MyLayout))
}
Available tokens are listed for example here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I am trying to replace the deprecated API PBHCopyFileSync with PBFSCopyFileSync as recommend in Files.h header.
Surprisingly, Apple only says several lines for this new API:
PBFSCopyFileSync
Duplicates a file and optionally renames it.
OSStatus PBFSCopyFileSync (
FSRefParamPtr paramBlock
);
Availability
Available in Mac OS X v10.5 and later.
Declared In
Files.h
And I couldn't find more about how to use this function.
Specially, what should be filled into the parameter FSRefParamPtr? I tried code below, but keeps getting an error of -50.
paramErr -50
Invalid value passed in a parameter. Your application
passed an invalid parameter for dialog options.
Here is the code:
OSStatus res = noErr;
FSRefParam param;
FSRef srcFile, dstDir, newFile;
const char *src = "$PATH_TO_A_EXISTING_FILE";
const char *dst = "/tmp";
res = FSPathMakeRef((const UInt8 *)src, &srcFile, NULL);
assert(res == noErr);
res = FSPathMakeRef((const UInt8 *)dst, &dstDir, NULL);
assert(res == noErr);
memset(¶m, 0, sizeof(FSRefParam));
param.ioCompletion = NULL;
param.ref = &srcFile;
param.parentRef = &dstDir;
param.newRef = &newFile;
res = PBFSCopyFileSync(¶m);
if (res == noErr) {
printf("SUCCESS!!!\n");
} else {
printf("FAILED!!! %d\n", res);
}
So, does anyone know some detailed documentation or sample codes about this API? Or is there any more popular/documented C++ API for copying files?
Thanks.
Quinn “The Eskimo!” says:
Always use FSCopyObjectSync. PBFSCopyFileSync/PBHCopyFileSync are low-level routines that are exported for legacy reasons. FSCopyObjectSync is a proper API that takes care of all of the nittygritty details of copying.
Notably, both PBFSCopyFileSync and PBHCopyFileSync are parameter block routines, with no nice wrappers. You should consider that a hint (-: