Padding day / time values to ensure 3 digit length
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a script which renders timestamps in the format YYYY'DDD'TTT where Y = year, D = day out of 365 and T = time in 1000th's of the day:
#!/bin/bash
clear
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432))
d=$(date +%j)
y=$(date +%Y)
printf "$y`$d`$t" "$y" "$d" "$t"
but I need to modify it so that D and T will always produce a three-character value (ie, '001' instead of '1' for Jan 1, '001' instead of '1' for the first minute of the day) - and I have no idea how.
Any help is hugely appreciated
date time
add a comment |
I have a script which renders timestamps in the format YYYY'DDD'TTT where Y = year, D = day out of 365 and T = time in 1000th's of the day:
#!/bin/bash
clear
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432))
d=$(date +%j)
y=$(date +%Y)
printf "$y`$d`$t" "$y" "$d" "$t"
but I need to modify it so that D and T will always produce a three-character value (ie, '001' instead of '1' for Jan 1, '001' instead of '1' for the first minute of the day) - and I have no idea how.
Any help is hugely appreciated
date time
add a comment |
I have a script which renders timestamps in the format YYYY'DDD'TTT where Y = year, D = day out of 365 and T = time in 1000th's of the day:
#!/bin/bash
clear
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432))
d=$(date +%j)
y=$(date +%Y)
printf "$y`$d`$t" "$y" "$d" "$t"
but I need to modify it so that D and T will always produce a three-character value (ie, '001' instead of '1' for Jan 1, '001' instead of '1' for the first minute of the day) - and I have no idea how.
Any help is hugely appreciated
date time
I have a script which renders timestamps in the format YYYY'DDD'TTT where Y = year, D = day out of 365 and T = time in 1000th's of the day:
#!/bin/bash
clear
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432))
d=$(date +%j)
y=$(date +%Y)
printf "$y`$d`$t" "$y" "$d" "$t"
but I need to modify it so that D and T will always produce a three-character value (ie, '001' instead of '1' for Jan 1, '001' instead of '1' for the first minute of the day) - and I have no idea how.
Any help is hugely appreciated
date time
date time
edited Sep 29 '11 at 15:33
manatwork
22.2k38386
22.2k38386
asked Sep 29 '11 at 14:52
Jack AndersJack Anders
262
262
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Perhaps you could take a look at the printf format string. The first parameter of printf should be a format string which includes placeholders for each of the arguments that follow.
You can include %d to represent an argument in signed decimal format, and you can prefix d by 0n for n characters of zero padding.
printf "%d/%d/%d" 2011 2 3
Will output 2011/2/3
printf "%04d/%03d/%05d" 2011 2 45
Should output 2011/002/00045
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
1
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
add a comment |
Let date do the work!
date +%3j
Then, for the milliday part, a simple trick is to compute 1000 plus the number of millidays and strip away the leading 1.
So for your script:
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432 + 1000))
date "+%Y'%3j'${t#1}"
For using date alone, +1, but as shown,2011-01-01 00:02produces2011'001'1without the zero-padding .... Here is a tweaked variant:t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"
– Peter.O
Sep 30 '11 at 3:35
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f21710%2fpadding-day-time-values-to-ensure-3-digit-length%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Perhaps you could take a look at the printf format string. The first parameter of printf should be a format string which includes placeholders for each of the arguments that follow.
You can include %d to represent an argument in signed decimal format, and you can prefix d by 0n for n characters of zero padding.
printf "%d/%d/%d" 2011 2 3
Will output 2011/2/3
printf "%04d/%03d/%05d" 2011 2 45
Should output 2011/002/00045
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
1
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
add a comment |
Perhaps you could take a look at the printf format string. The first parameter of printf should be a format string which includes placeholders for each of the arguments that follow.
You can include %d to represent an argument in signed decimal format, and you can prefix d by 0n for n characters of zero padding.
printf "%d/%d/%d" 2011 2 3
Will output 2011/2/3
printf "%04d/%03d/%05d" 2011 2 45
Should output 2011/002/00045
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
1
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
add a comment |
Perhaps you could take a look at the printf format string. The first parameter of printf should be a format string which includes placeholders for each of the arguments that follow.
You can include %d to represent an argument in signed decimal format, and you can prefix d by 0n for n characters of zero padding.
printf "%d/%d/%d" 2011 2 3
Will output 2011/2/3
printf "%04d/%03d/%05d" 2011 2 45
Should output 2011/002/00045
Perhaps you could take a look at the printf format string. The first parameter of printf should be a format string which includes placeholders for each of the arguments that follow.
You can include %d to represent an argument in signed decimal format, and you can prefix d by 0n for n characters of zero padding.
printf "%d/%d/%d" 2011 2 3
Will output 2011/2/3
printf "%04d/%03d/%05d" 2011 2 45
Should output 2011/002/00045
edited 1 hour ago
Rui F Ribeiro
41.9k1483142
41.9k1483142
answered Sep 29 '11 at 15:14
digitalseandigitalsean
16113
16113
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
1
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
add a comment |
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
1
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
Thanks! That does help, but my real problem is that I can't just insert additional zeros, since after the first 99 values (days, ect), the value gains a third character on it's own. My script needs to pad values < 100 with one zero, and values < 10 with two zeros, if that makes sense...
– Jack Anders
Sep 29 '11 at 15:19
1
1
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
That is what the printf "%03d" is doing. The '3' is 'minimum' width and the '0' means to pad with a zero character ('0') to fill the minimum width. See the examples that digitalsean gave. Notice that the "2011" did not have any zeros added, because it met the minimum width.
– Arcege
Sep 29 '11 at 16:15
add a comment |
Let date do the work!
date +%3j
Then, for the milliday part, a simple trick is to compute 1000 plus the number of millidays and strip away the leading 1.
So for your script:
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432 + 1000))
date "+%Y'%3j'${t#1}"
For using date alone, +1, but as shown,2011-01-01 00:02produces2011'001'1without the zero-padding .... Here is a tweaked variant:t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"
– Peter.O
Sep 30 '11 at 3:35
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
add a comment |
Let date do the work!
date +%3j
Then, for the milliday part, a simple trick is to compute 1000 plus the number of millidays and strip away the leading 1.
So for your script:
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432 + 1000))
date "+%Y'%3j'${t#1}"
For using date alone, +1, but as shown,2011-01-01 00:02produces2011'001'1without the zero-padding .... Here is a tweaked variant:t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"
– Peter.O
Sep 30 '11 at 3:35
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
add a comment |
Let date do the work!
date +%3j
Then, for the milliday part, a simple trick is to compute 1000 plus the number of millidays and strip away the leading 1.
So for your script:
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432 + 1000))
date "+%Y'%3j'${t#1}"
Let date do the work!
date +%3j
Then, for the milliday part, a simple trick is to compute 1000 plus the number of millidays and strip away the leading 1.
So for your script:
s=$(($(date +"%H*3600+%M*60+%S")))
t=$(($s * 5 / 432 + 1000))
date "+%Y'%3j'${t#1}"
edited Sep 30 '11 at 7:20
answered Sep 29 '11 at 22:40
GillesGilles
546k12911101624
546k12911101624
For using date alone, +1, but as shown,2011-01-01 00:02produces2011'001'1without the zero-padding .... Here is a tweaked variant:t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"
– Peter.O
Sep 30 '11 at 3:35
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
add a comment |
For using date alone, +1, but as shown,2011-01-01 00:02produces2011'001'1without the zero-padding .... Here is a tweaked variant:t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"
– Peter.O
Sep 30 '11 at 3:35
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
For using date alone, +1, but as shown,
2011-01-01 00:02 produces 2011'001'1 without the zero-padding .... Here is a tweaked variant: t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"– Peter.O
Sep 30 '11 at 3:35
For using date alone, +1, but as shown,
2011-01-01 00:02 produces 2011'001'1 without the zero-padding .... Here is a tweaked variant: t=00$(($(($(date +"%H*3600+%M*60+%S"))) * 5 / 432)); date "+%Y'%3j'${t:(-3)}"– Peter.O
Sep 30 '11 at 3:35
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
@fred Oops, good spot. I've edited to a more portable way of padding the number.
– Gilles
Sep 30 '11 at 7:20
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f21710%2fpadding-day-time-values-to-ensure-3-digit-length%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown