demo-date-time-format
Demonstration of date time format using UTC ISO in many coding languages
https://github.com/joelparkerhenderson/demo-date-time-format
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (5.7%) to scientific vocabulary
Repository
Demonstration of date time format using UTC ISO in many coding languages
Basic Info
Statistics
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Demo date time format using UTC ISO
This demo shows:
- How to print a date-time string.
- Using the UTC time zone, also known as +00:00, or GMT, or Zulu time.
- Using the ISO 8601 extended format, because it's easy to read.
Example:
2020-01-01T00:00:00.000+00:00
Meaning:
YYYY-MM-DDmeans the year, month, and day.Tis the ISO standard separator character between the date and time.HH:MM:SS.sssmeans the hour, minute, second, and millisecond.+00:00means zero offset from UTC, in other words, actual UTC time.
Preferences:
- We like the extended format because it's easy for a person to skim.
- We prefer using a
Tseparator over a blank because of machine parsing. - We prefer using fractional seconds over just seconds because of precision.
- We prefer
+00.00overZbecause our logs contain many time zones.
Coding conventions in this repo:
tis the time.fis the format string.sis the output string.
Examples:
Bash
Bash shell with GNU date and nanoseconds:
date -u +"%Y-%m-%dT%H:%M:%S.%N+00:00"
Bash shell with BSD date and seconds:
date -u +"%Y-%m-%dT%H:%M:%S+00:00"
Bash shell with BSD date to convert Unix epoch seconds:
date -r 1000000000 -u +"%Y-%m-%dT%H:%M:%S+00:00"
Bash shell with BSD date to find files and print times:
find . -type f -print0 |
xargs -0 stat -f"%m␟%N" |
awk -F ␟ '{ ("date -r " $1 " -u +\"%Y-%m-%dT%H:%M:%S+00:00\"" | getline t); $1=t; print}' |
sort -n
C
C with ANSI C:
#include <stdio.h>
#include <time.h>
int main()
{
time_t timer;
char s[30];
struct tm* tm_info;
time(&timer);
tm_info = localtime(&timer);
strftime(s, 30, "%Y-%m-%d %H:%M:%S.000000000+00:00", tm_info);
puts(s);
return 0;
}
C with struct timeval:
#include <stdio.h>
#include <sys/time.h>
int main(void)
{
struct timeval t;
gettimeofday(&t,NULL);
printf("%ld.%09ld+00:00\n", (long int)t.tv_sec, (long int)t.tv_usec);
return 0;
}
C++
C++:
#include <iostream>
int main() {
time_t t;
time(&t);
char buf[sizeof "2011-10-08T07:07:09+00:00"];
strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%S+00:00", gmtime(&t));
// Prefer this line if your compiler supports %F or %T formats:
//strftime(buf, sizeof buf, "%FT%TZ", gmtime(&nt));
std::cout << buf << "\n";
}
C++ with Boost:
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
using namespace boost::posix_time;
ptime t = microsec_clock::universal_time();
std::cout << to_iso_extended_string(t) << "Z\n";
}
Elixir
Elixir with the Timex library:
use Timex
f = "{ISO:Extended}"
t = Timex.now
Timex.format(t, f)
Go
Go:
package main
import "fmt"
import "time"
func main() {
const f = "2006-01-02T15:04:05.999999999+00:00"
t := time.Now().UTC()
s := t.Format(format)
fmt.Println(s)
}
Java
Java with seconds:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateTimeFormat {
public static void main(String[] args) {
String iso = "yyyy-MM-dd'T'HH:mm:ss'.000000000+00:00'";
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat(iso);
df.setTimeZone(tz);
String s = df.format(new Date());
System.out.println(s);
}
}
Java with Joda:
DateTime dt = new DateTime();
DateTimeFormatter iso = ISODateTimeFormat.dateTime();
String str = fmt.print(dt);
JavaScript
JavaScript with milliseconds:
var t = new Date();
var s = now.toISOString().slice(0, -1) + "+00:00"
console.log(s);
Perl
Perl with POSIX and seconds:
use POSIX;
my $F = "%Y-%m-%dT%H:%M:%S+00:00"
my $t = time();
print strftime($F, gmtime($t), "\n";
Perl with CPAN:
use DateTime;
my $t = DateTime->now()
$now->iso8601().'+00:00';
Python
Python with microseconds:
import datetime
F = "%Y-%m-%dT%H:%M:%S.%f+00:00"
t = datetime.datetime.utcnow()
t.strftime(F)
Ruby
Ruby:
F = "%Y-%m-%dT%H:%M:%S.%N+00:00"
t = Time.now.utc
puts t.strftime(F)
Owner
- Name: Joel Parker Henderson
- Login: joelparkerhenderson
- Kind: user
- Location: California
- Website: http://www.joelparkerhenderson.com
- Repositories: 319
- Profile: https://github.com/joelparkerhenderson
Software developer. Technology consultant. Creator of GitAlias.com, NumCommand.com, SixArm.com, and many open source projects.
Citation (CITATION.cff)
cff-version: 1.2.0
title: Demo date time format using UTC ISO
message: >-
If you use this work and you want to cite it,
then you can use the metadata from this file.
type: software
authors:
- given-names: Joel Parker
family-names: Henderson
email: joel@joelparkerhenderson.com
affiliation: joelparkerhenderson.com
orcid: 'https://orcid.org/0009-0000-4681-282X'
identifiers:
- type: url
value: 'https://github.com/joelparkerhenderson/demo-date-time-format/'
description: Demo date time format using UTC ISO
repository-code: 'https://github.com/joelparkerhenderson/demo-date-time-format/'
abstract: >-
Demo date time format using UTC ISO
license: See license file
GitHub Events
Total
- Push event: 1
Last Year
- Push event: 1
Committers
Last synced: over 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Joel Parker Henderson | j****l@j****m | 7 |
| atkawa7 | a****7@y****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 0
- Total pull requests: 1
- Average time to close issues: N/A
- Average time to close pull requests: 4 minutes
- Total issue authors: 0
- Total pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 0.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
- atkawa7 (1)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- timex ~> 3.0
- certifi 0.7.0
- combine 0.9.6
- gettext 0.13.1
- hackney 1.6.5
- idna 1.2.0
- metrics 1.0.1
- mimerl 1.0.2
- ssl_verify_fun 1.1.1
- timex 3.1.13
- tzdata 0.5.10